blob: eb898a771a4f4c6a1c0092ccec1556624e05ff47 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Frédéric Lécaillea41d5312018-01-29 12:05:07 +010029#include <common/cfgparse.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010043#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020044#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <types/global.h>
William Lallemand71bd11a2017-11-20 19:13:14 +010046#include <types/cache.h>
William Lallemand9ed62032016-11-21 17:49:11 +010047#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048
Willy Tarreau8797c062007-05-07 00:55:35 +020049#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020050#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020051#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010052#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020054#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010055#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010056#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020057#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010058#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020060#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020061#include <proto/frontend.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020062#include <proto/h1.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010064#include <proto/hdr_idx.h>
Patrick Hemmere3faf022018-08-22 10:02:00 -040065#include <proto/hlua.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010066#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020067#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010069#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020070#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020071#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010072#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020073#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010074#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020075#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020076#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020077#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020078
Cyril Bonté19979e12012-04-04 12:57:21 +020079/* status codes available for the stats admin page (strictly 4 chars length) */
80const char *stat_status_codes[STAT_STATUS_SIZE] = {
81 [STAT_STATUS_DENY] = "DENY",
82 [STAT_STATUS_DONE] = "DONE",
83 [STAT_STATUS_ERRP] = "ERRP",
84 [STAT_STATUS_EXCD] = "EXCD",
85 [STAT_STATUS_NONE] = "NONE",
86 [STAT_STATUS_PART] = "PART",
87 [STAT_STATUS_UNKN] = "UNKN",
88};
89
Willy Tarreau53b6c742006-12-17 13:37:46 +010090/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +010091 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +020092 * the last CRLF. <len> bytes are copied, not counting the CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +010093 * The header is also automatically added to the index <hdr_idx>, and the end
94 * of headers is automatically adjusted. The number of bytes added is returned
95 * on success, otherwise <0 is returned indicating an error.
96 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +010097int http_header_add_tail2(struct http_msg *msg,
98 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +010099{
100 int bytes;
101
Willy Tarreau4d893d42018-07-12 15:43:32 +0200102 bytes = ci_insert_line2(msg->chn, msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100103 if (!bytes)
104 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100105 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100106 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
107}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200109/* Find the first or next occurrence of header <name> in message buffer <sol>
110 * using headers index <idx>, and return it in the <ctx> structure. This
111 * structure holds everything necessary to use the header and find next
112 * occurrence. If its <idx> member is 0, the header is searched from the
113 * beginning. Otherwise, the next occurrence is returned. The function returns
114 * 1 when it finds a value, and 0 when there is no more. It is very similar to
115 * http_find_header2() except that it is designed to work with full-line headers
116 * whose comma is not a delimiter but is part of the syntax. As a special case,
117 * if ctx->val is NULL when searching for a new values of a header, the current
118 * header is rescanned. This allows rescanning after a header deletion.
119 */
120int http_find_full_header2(const char *name, int len,
121 char *sol, struct hdr_idx *idx,
122 struct hdr_ctx *ctx)
123{
124 char *eol, *sov;
125 int cur_idx, old_idx;
126
127 cur_idx = ctx->idx;
128 if (cur_idx) {
129 /* We have previously returned a header, let's search another one */
130 sol = ctx->line;
131 eol = sol + idx->v[cur_idx].len;
132 goto next_hdr;
133 }
134
135 /* first request for this header */
136 sol += hdr_idx_first_pos(idx);
137 old_idx = 0;
138 cur_idx = hdr_idx_first_idx(idx);
139 while (cur_idx) {
140 eol = sol + idx->v[cur_idx].len;
141
142 if (len == 0) {
143 /* No argument was passed, we want any header.
144 * To achieve this, we simply build a fake request. */
145 while (sol + len < eol && sol[len] != ':')
146 len++;
147 name = sol;
148 }
149
150 if ((len < eol - sol) &&
151 (sol[len] == ':') &&
152 (strncasecmp(sol, name, len) == 0)) {
153 ctx->del = len;
154 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100155 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200156 sov++;
157
158 ctx->line = sol;
159 ctx->prev = old_idx;
160 ctx->idx = cur_idx;
161 ctx->val = sov - sol;
162 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100163 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200164 eol--;
165 ctx->tws++;
166 }
167 ctx->vlen = eol - sov;
168 return 1;
169 }
170 next_hdr:
171 sol = eol + idx->v[cur_idx].cr + 1;
172 old_idx = cur_idx;
173 cur_idx = idx->v[cur_idx].next;
174 }
175 return 0;
176}
177
Willy Tarreauc90dc232015-02-20 13:51:36 +0100178/* Find the first or next header field in message buffer <sol> using headers
179 * index <idx>, and return it in the <ctx> structure. This structure holds
180 * everything necessary to use the header and find next occurrence. If its
181 * <idx> member is 0, the first header is retrieved. Otherwise, the next
182 * occurrence is returned. The function returns 1 when it finds a value, and
183 * 0 when there is no more. It is equivalent to http_find_full_header2() with
184 * no header name.
185 */
186int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
187{
188 char *eol, *sov;
189 int cur_idx, old_idx;
190 int len;
191
192 cur_idx = ctx->idx;
193 if (cur_idx) {
194 /* We have previously returned a header, let's search another one */
195 sol = ctx->line;
196 eol = sol + idx->v[cur_idx].len;
197 goto next_hdr;
198 }
199
200 /* first request for this header */
201 sol += hdr_idx_first_pos(idx);
202 old_idx = 0;
203 cur_idx = hdr_idx_first_idx(idx);
204 while (cur_idx) {
205 eol = sol + idx->v[cur_idx].len;
206
207 len = 0;
208 while (1) {
209 if (len >= eol - sol)
210 goto next_hdr;
211 if (sol[len] == ':')
212 break;
213 len++;
214 }
215
216 ctx->del = len;
217 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100218 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100219 sov++;
220
221 ctx->line = sol;
222 ctx->prev = old_idx;
223 ctx->idx = cur_idx;
224 ctx->val = sov - sol;
225 ctx->tws = 0;
226
Willy Tarreau2235b262016-11-05 15:50:20 +0100227 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100228 eol--;
229 ctx->tws++;
230 }
231 ctx->vlen = eol - sov;
232 return 1;
233
234 next_hdr:
235 sol = eol + idx->v[cur_idx].cr + 1;
236 old_idx = cur_idx;
237 cur_idx = idx->v[cur_idx].next;
238 }
239 return 0;
240}
241
Willy Tarreau33a7e692007-06-10 19:45:56 +0200242/* Find the first or next occurrence of header <name> in message buffer <sol>
243 * using headers index <idx>, and return it in the <ctx> structure. This
244 * structure holds everything necessary to use the header and find next
245 * occurrence. If its <idx> member is 0, the header is searched from the
246 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100247 * 1 when it finds a value, and 0 when there is no more. It is designed to work
248 * with headers defined as comma-separated lists. As a special case, if ctx->val
249 * is NULL when searching for a new values of a header, the current header is
250 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200251 */
252int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100253 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200254 struct hdr_ctx *ctx)
255{
Willy Tarreau68085d82010-01-18 14:54:04 +0100256 char *eol, *sov;
257 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200258
Willy Tarreau68085d82010-01-18 14:54:04 +0100259 cur_idx = ctx->idx;
260 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200261 /* We have previously returned a value, let's search
262 * another one on the same line.
263 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200264 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200265 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100266 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200267 eol = sol + idx->v[cur_idx].len;
268
269 if (sov >= eol)
270 /* no more values in this header */
271 goto next_hdr;
272
Willy Tarreau68085d82010-01-18 14:54:04 +0100273 /* values remaining for this header, skip the comma but save it
274 * for later use (eg: for header deletion).
275 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200276 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100277 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200278 sov++;
279
280 goto return_hdr;
281 }
282
283 /* first request for this header */
284 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100285 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200286 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200287 while (cur_idx) {
288 eol = sol + idx->v[cur_idx].len;
289
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200290 if (len == 0) {
291 /* No argument was passed, we want any header.
292 * To achieve this, we simply build a fake request. */
293 while (sol + len < eol && sol[len] != ':')
294 len++;
295 name = sol;
296 }
297
Willy Tarreau33a7e692007-06-10 19:45:56 +0200298 if ((len < eol - sol) &&
299 (sol[len] == ':') &&
300 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100301 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200302 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100303 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200304 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100305
Willy Tarreau33a7e692007-06-10 19:45:56 +0200306 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100307 ctx->prev = old_idx;
308 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200309 ctx->idx = cur_idx;
310 ctx->val = sov - sol;
311
Willy Tarreauab813a42018-09-10 18:41:28 +0200312 eol = http_find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200313 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100314 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200315 eol--;
316 ctx->tws++;
317 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200318 ctx->vlen = eol - sov;
319 return 1;
320 }
321 next_hdr:
322 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100323 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200324 cur_idx = idx->v[cur_idx].next;
325 }
326 return 0;
327}
328
Willy Tarreau68085d82010-01-18 14:54:04 +0100329/* Remove one value of a header. This only works on a <ctx> returned by one of
330 * the http_find_header functions. The value is removed, as well as surrounding
331 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100332 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100333 * message <msg>. The new index is returned. If it is zero, it means there is
334 * no more header, so any processing may stop. The ctx is always left in a form
335 * that can be handled by http_find_header2() to find next occurrence.
336 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100337int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100338{
339 int cur_idx = ctx->idx;
340 char *sol = ctx->line;
341 struct hdr_idx_elem *hdr;
342 int delta, skip_comma;
343
344 if (!cur_idx)
345 return 0;
346
347 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200348 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100349 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200350 delta = b_rep_blk(&msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100351 http_msg_move_end(msg, delta);
352 idx->used--;
353 hdr->len = 0; /* unused entry */
354 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100355 if (idx->tail == ctx->idx)
356 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100357 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100358 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100359 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200360 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100361 return ctx->idx;
362 }
363
364 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200365 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
366 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100367 */
368
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200369 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200370 delta = b_rep_blk(&msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200371 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100372 NULL, 0);
373 hdr->len += delta;
374 http_msg_move_end(msg, delta);
375 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200376 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100377 return ctx->idx;
378}
379
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100380/* This function handles a server error at the stream interface level. The
381 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100382 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100383 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100384 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200385 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200386static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau83061a82018-07-13 11:56:34 +0200387 int err, int finst, const struct buffer *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200388{
Christopher Faulet9768c262018-10-22 09:34:31 +0200389 if (IS_HTX_STRM(s))
390 return htx_server_error(s, si, err, finst, msg);
391
Willy Tarreau2019f952017-03-14 11:07:31 +0100392 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100393 channel_auto_read(si_oc(si));
394 channel_abort(si_oc(si));
395 channel_auto_close(si_oc(si));
396 channel_erase(si_oc(si));
397 channel_auto_close(si_ic(si));
398 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200399 if (msg)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200400 co_inject(si_ic(si), msg->area, msg->data);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200401 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200402 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200403 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200404 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200405}
406
Willy Tarreau87b09662015-04-03 00:22:06 +0200407/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100408 * and message.
409 */
410
Willy Tarreau83061a82018-07-13 11:56:34 +0200411struct buffer *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100412{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200413 const int msgnum = http_get_status_idx(s->txn->status);
414
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200415 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200416 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200417 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200418 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100419 else
420 return &http_err_chunks[msgnum];
421}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200422
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100423void
Willy Tarreau83061a82018-07-13 11:56:34 +0200424http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100425{
Christopher Faulet9768c262018-10-22 09:34:31 +0200426 if (IS_HTX_STRM(s))
427 return htx_reply_and_close(s, status, msg);
428
Christopher Fauletd7c91962015-04-30 11:48:27 +0200429 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100430 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100431 stream_int_retnclose(&s->si[0], msg);
432}
433
Willy Tarreau21d2af32008-02-14 20:25:24 +0100434/* Parse the URI from the given transaction (which is assumed to be in request
435 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
436 * It is returned otherwise.
437 */
Willy Tarreau6b952c82018-09-10 17:45:34 +0200438char *http_txn_get_path(const struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100439{
Willy Tarreau6b952c82018-09-10 17:45:34 +0200440 struct ist ret;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100441
Willy Tarreau9d9ccdb2018-10-28 20:13:12 +0100442 if (!txn->req.chn->buf.size)
443 return NULL;
444
Willy Tarreau6b952c82018-09-10 17:45:34 +0200445 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 +0100446
Willy Tarreau6b952c82018-09-10 17:45:34 +0200447 return ret.ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +0100448}
449
Willy Tarreau71241ab2012-12-27 11:30:54 +0100450/* Returns a 302 for a redirectable request that reaches a server working in
451 * in redirect mode. This may only be called just after the stream interface
452 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
453 * follow normal proxy processing. NOTE: this function is designed to support
454 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100455 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200456void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100457{
458 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100459 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100460 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200461 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100462
Christopher Fauletfefc73d2018-10-24 21:18:04 +0200463 if (IS_HTX_STRM(s))
464 return htx_perform_server_redirect(s, si);
465
Willy Tarreauefb453c2008-10-26 20:49:47 +0100466 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200467 trash.data = strlen(HTTP_302);
468 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100469
Willy Tarreaub05e48a2018-09-20 11:12:58 +0200470 srv = __objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100471
Willy Tarreauefb453c2008-10-26 20:49:47 +0100472 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200473 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100474 return;
475
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100476 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100477 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200478 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
479 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100480 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100481
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200482 /* 3: add the request URI. Since it was already forwarded, we need
483 * to temporarily rewind the buffer.
484 */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200485 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200486 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200487
Willy Tarreau6b952c82018-09-10 17:45:34 +0200488 path = http_txn_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200489 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 +0200490
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200491 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200492
Willy Tarreauefb453c2008-10-26 20:49:47 +0100493 if (!path)
494 return;
495
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200496 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100497 return;
498
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200499 memcpy(trash.area + trash.data, path, len);
500 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100501
502 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200503 memcpy(trash.area + trash.data,
504 "\r\nProxy-Connection: close\r\n\r\n", 29);
505 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100506 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200507 memcpy(trash.area + trash.data,
508 "\r\nConnection: close\r\n\r\n", 23);
509 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100510 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100511
512 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200513 si_shutr(si);
514 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100515 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100516 si->state = SI_ST_CLO;
517
518 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +0100519 txn->status = 302;
520 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100521
522 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100523 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500524 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100525}
526
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100527/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100528 * that the server side is closed. Note that err_type is actually a
529 * bitmask, where almost only aborts may be cumulated with other
530 * values. We consider that aborted operations are more important
531 * than timeouts or errors due to the fact that nobody else in the
532 * logs might explain incomplete retries. All others should avoid
533 * being cumulated. It should normally not be possible to have multiple
534 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100535 * Note that connection errors appearing on the second request of a keep-alive
536 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100537 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200538void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100539{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100540 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100541
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200542 /* set s->txn->status for http_error_message(s) */
543 s->txn->status = 503;
544
Willy Tarreauefb453c2008-10-26 20:49:47 +0100545 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200546 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100547 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100548 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200549 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100550 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200551 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100552 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200553 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100554 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100555 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200556 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100557 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100558 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200559 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100560 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200561 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100562 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200563 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100564 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200565 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200566 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200567 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100568 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200569 http_error_message(s));
570 else { /* SI_ET_CONN_OTHER and others */
571 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200572 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100573 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200574 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100575}
576
Willy Tarreau42250582007-04-01 01:30:43 +0200577extern const char sess_term_cond[8];
578extern const char sess_fin_state[8];
579extern const char *monthname[12];
Willy Tarreau8ceae722018-11-26 11:58:30 +0100580
581DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
582DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
583
584struct pool_head *pool_head_requri = NULL;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100585struct pool_head *pool_head_capture = NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100586
Willy Tarreau117f59e2007-03-04 18:17:17 +0100587/*
588 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +0200589 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +0100590 */
Christopher Faulet10079f52018-10-03 15:17:28 +0200591void http_capture_headers(char *som, struct hdr_idx *idx,
592 char **cap, struct cap_hdr *cap_hdr)
Willy Tarreau117f59e2007-03-04 18:17:17 +0100593{
594 char *eol, *sol, *col, *sov;
595 int cur_idx;
596 struct cap_hdr *h;
597 int len;
598
599 sol = som + hdr_idx_first_pos(idx);
600 cur_idx = hdr_idx_first_idx(idx);
601
602 while (cur_idx) {
603 eol = sol + idx->v[cur_idx].len;
604
605 col = sol;
606 while (col < eol && *col != ':')
607 col++;
608
609 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100610 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +0100611 sov++;
612
613 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +0200614 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +0100615 (strncasecmp(sol, h->name, h->namelen) == 0)) {
616 if (cap[h->index] == NULL)
617 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +0100618 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100619
620 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100621 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +0100622 continue;
623 }
624
625 len = eol - sov;
626 if (len > h->len)
627 len = h->len;
628
629 memcpy(cap[h->index], sov, len);
630 cap[h->index][len]=0;
631 }
632 }
633 sol = eol + idx->v[cur_idx].cr + 1;
634 cur_idx = idx->v[cur_idx].next;
635 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100636}
637
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200638/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
639 * conversion succeeded, 0 in case of error. If the request was already 1.X,
640 * nothing is done and 1 is returned.
641 */
Willy Tarreau79e57332018-10-02 16:01:16 +0200642int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200643{
644 int delta;
645 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100646 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200647
648 if (msg->sl.rq.v_l != 0)
649 return 1;
650
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300651 /* RFC 1945 allows only GET for HTTP/0.9 requests */
652 if (txn->meth != HTTP_METH_GET)
653 return 0;
654
Willy Tarreauf37954d2018-06-15 18:31:02 +0200655 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200656
657 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300658 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
659 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200660 }
661 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200662 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +0100663 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200664 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200665 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200666 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +0200667 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200668 NULL, NULL);
669 if (unlikely(!cur_end))
670 return 0;
671
672 /* we have a full HTTP/1.0 request now and we know that
673 * we have either a CR or an LF at <ptr>.
674 */
675 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
676 return 1;
677}
678
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100679/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100680 * and "keep-alive" values. If we already know that some headers may safely
681 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100682 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
683 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +0100684 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100685 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
686 * found, and TX_CON_*_SET is adjusted depending on what is left so only
687 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100688 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +0100689 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100690void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +0100691{
Willy Tarreau5b154472009-12-21 20:11:07 +0100692 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100693 const char *hdr_val = "Connection";
694 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +0100695
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100696 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +0100697 return;
698
Willy Tarreau88d349d2010-01-25 12:15:43 +0100699 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
700 hdr_val = "Proxy-Connection";
701 hdr_len = 16;
702 }
703
Willy Tarreau5b154472009-12-21 20:11:07 +0100704 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100705 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200706 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100707 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
708 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100709 if (to_del & 2)
710 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100711 else
712 txn->flags |= TX_CON_KAL_SET;
713 }
714 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
715 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100716 if (to_del & 1)
717 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100718 else
719 txn->flags |= TX_CON_CLO_SET;
720 }
Willy Tarreau50fc7772012-11-11 22:19:57 +0100721 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
722 txn->flags |= TX_HDR_CONN_UPG;
723 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100724 }
725
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100726 txn->flags |= TX_HDR_CONN_PRS;
727 return;
728}
Willy Tarreau5b154472009-12-21 20:11:07 +0100729
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100730/* Apply desired changes on the Connection: header. Values may be removed and/or
731 * added depending on the <wanted> flags, which are exclusively composed of
732 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
733 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
734 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100735void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100736{
737 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100738 const char *hdr_val = "Connection";
739 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100740
741 ctx.idx = 0;
742
Willy Tarreau88d349d2010-01-25 12:15:43 +0100743
744 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
745 hdr_val = "Proxy-Connection";
746 hdr_len = 16;
747 }
748
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100749 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200750 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100751 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
752 if (wanted & TX_CON_KAL_SET)
753 txn->flags |= TX_CON_KAL_SET;
754 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100755 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +0100756 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100757 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
758 if (wanted & TX_CON_CLO_SET)
759 txn->flags |= TX_CON_CLO_SET;
760 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100761 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +0100762 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100763 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100764
765 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
766 return;
767
768 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
769 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100770 hdr_val = "Connection: close";
771 hdr_len = 17;
772 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
773 hdr_val = "Proxy-Connection: close";
774 hdr_len = 23;
775 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100776 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100777 }
778
779 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
780 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100781 hdr_val = "Connection: keep-alive";
782 hdr_len = 22;
783 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
784 hdr_val = "Proxy-Connection: keep-alive";
785 hdr_len = 28;
786 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100787 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100788 }
789 return;
Willy Tarreau5b154472009-12-21 20:11:07 +0100790}
791
Willy Tarreau87b09662015-04-03 00:22:06 +0200792void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200793{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200794 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200795 int tmp = TX_CON_WANT_KAL;
796
Christopher Fauletf2824e62018-10-01 12:12:37 +0200797 if (IS_HTX_STRM(s))
Christopher Faulet0f226952018-10-22 09:29:56 +0200798 return htx_adjust_conn_mode(s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200799
Christopher Faulet315b39c2018-09-21 16:26:19 +0200800 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
801 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
802 tmp = TX_CON_WANT_TUN;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200803
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200804 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Faulet315b39c2018-09-21 16:26:19 +0200805 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200806 tmp = TX_CON_WANT_SCL;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200807
Christopher Faulet315b39c2018-09-21 16:26:19 +0200808 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
809 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200810 tmp = TX_CON_WANT_CLO;
811
812 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
813 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
814
815 if (!(txn->flags & TX_HDR_CONN_PRS) &&
816 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
817 /* parse the Connection header and possibly clean it */
818 int to_del = 0;
819 if ((msg->flags & HTTP_MSGF_VER_11) ||
820 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200821 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200822 to_del |= 2; /* remove "keep-alive" */
823 if (!(msg->flags & HTTP_MSGF_VER_11))
824 to_del |= 1; /* remove "close" */
825 http_parse_connection_header(txn, msg, to_del);
826 }
827
828 /* check if client or config asks for explicit close in KAL/SCL */
829 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
830 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
831 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
832 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
833 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200834 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200835 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
836}
William Lallemand82fe75c2012-10-23 10:25:10 +0200837
Willy Tarreaud787e662009-07-07 10:14:51 +0200838/* This stream analyser waits for a complete HTTP request. It returns 1 if the
839 * processing can continue on next analysers, or zero if it either needs more
840 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100841 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200842 * when it has nothing left to do, and may remove any analyser when it wants to
843 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100844 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200845int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100846{
Willy Tarreau59234e92008-11-30 23:51:27 +0100847 /*
848 * We will parse the partial (or complete) lines.
849 * We will check the request syntax, and also join multi-line
850 * headers. An index of all the lines will be elaborated while
851 * parsing.
852 *
853 * For the parsing, we use a 28 states FSM.
854 *
855 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200856 * ci_head(req) = beginning of request
857 * ci_head(req) + msg->eoh = end of processed headers / start of current one
858 * ci_tail(req) = end of input data
859 * msg->eol = end of current header or line (LF or CRLF)
860 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200861 *
862 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200863 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200864 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
865 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100866 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100867
Willy Tarreau59234e92008-11-30 23:51:27 +0100868 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200869 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200870 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100871 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200872 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100873
Christopher Faulete0768eb2018-10-03 16:38:02 +0200874 if (IS_HTX_STRM(s))
875 return htx_wait_for_request(s, req, an_bit);
876
Christopher Faulet45073512018-07-20 10:16:29 +0200877 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 +0100878 now_ms, __FUNCTION__,
879 s,
880 req,
881 req->rex, req->wex,
882 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200883 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100884 req->analysers);
885
Willy Tarreau52a0c602009-08-16 22:45:38 +0200886 /* we're speaking HTTP here, so let's speak HTTP to the client */
887 s->srv_error = http_return_srv_error;
888
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300889 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200890 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300891 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
892
Willy Tarreau83e3af02009-12-28 17:39:57 +0100893 /* There's a protected area at the end of the buffer for rewriting
894 * purposes. We don't want to start to parse the request if the
895 * protected area is affected, because we may have to move processed
896 * data later, which is much more complicated.
897 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200898 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200899 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100900 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200901 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100902 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100903 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200904 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200905 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100906 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100907 return 0;
908 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200909 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200910 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200911 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100912 }
913
Willy Tarreauf37954d2018-06-15 18:31:02 +0200914 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100915 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100916 }
917
Willy Tarreau59234e92008-11-30 23:51:27 +0100918 /* 1: we might have to print this header in debug mode */
919 if (unlikely((global.mode & MODE_DEBUG) &&
920 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200921 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100922 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100923
Willy Tarreauf37954d2018-06-15 18:31:02 +0200924 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200925 /* this is a bit complex : in case of error on the request line,
926 * we know that rq.l is still zero, so we display only the part
927 * up to the end of the line (truncated by debug_hdr).
928 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200929 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100930 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100931
Willy Tarreau59234e92008-11-30 23:51:27 +0100932 sol += hdr_idx_first_pos(&txn->hdr_idx);
933 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100934
Willy Tarreau59234e92008-11-30 23:51:27 +0100935 while (cur_idx) {
936 eol = sol + txn->hdr_idx.v[cur_idx].len;
937 debug_hdr("clihdr", s, sol, eol);
938 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
939 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100940 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100941 }
942
Willy Tarreau58f10d72006-12-04 02:26:12 +0100943
Willy Tarreau59234e92008-11-30 23:51:27 +0100944 /*
945 * Now we quickly check if we have found a full valid request.
946 * If not so, we check the FD and buffer states before leaving.
947 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100948 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100949 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200950 * on a keep-alive stream, if we encounter and error, close, t/o,
951 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100952 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200953 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200954 * Last, we may increase some tracked counters' http request errors on
955 * the cases that are deliberately the client's fault. For instance,
956 * a timeout or connection reset is not counted as an error. However
957 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100958 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100959
Willy Tarreau655dce92009-11-08 13:10:58 +0100960 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100961 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100962 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100963 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100964 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200965 stream_inc_http_req_ctr(s);
966 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200967 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100968 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100969 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100970
Willy Tarreau59234e92008-11-30 23:51:27 +0100971 /* 1: Since we are in header mode, if there's no space
972 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200973 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100974 * must terminate it now.
975 */
Willy Tarreau23752332018-06-15 14:54:53 +0200976 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100977 /* FIXME: check if URI is set and return Status
978 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100979 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200980 stream_inc_http_req_ctr(s);
981 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200982 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +0200983 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +0200984 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +0100985 goto return_bad_req;
986 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100987
Willy Tarreau59234e92008-11-30 23:51:27 +0100988 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200989 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200990 if (!(s->flags & SF_ERR_MASK))
991 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100992
Willy Tarreaufcffa692010-01-10 14:21:19 +0100993 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100994 goto failed_keep_alive;
995
Willy Tarreau0f228a02015-05-01 15:37:53 +0200996 if (sess->fe->options & PR_O_IGNORE_PRB)
997 goto failed_keep_alive;
998
Willy Tarreau59234e92008-11-30 23:51:27 +0100999 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001000 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001001 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001002 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001003 }
1004
Willy Tarreaudc979f22012-12-04 10:39:01 +01001005 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001006 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001007 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001008 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001009 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001010 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001011 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001012 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001013 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001014 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001015
Willy Tarreaue7dff022015-04-03 01:14:29 +02001016 if (!(s->flags & SF_FINST_MASK))
1017 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001018 return 0;
1019 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001020
Willy Tarreau59234e92008-11-30 23:51:27 +01001021 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001022 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001023 if (!(s->flags & SF_ERR_MASK))
1024 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001025
Willy Tarreaufcffa692010-01-10 14:21:19 +01001026 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001027 goto failed_keep_alive;
1028
Willy Tarreau0f228a02015-05-01 15:37:53 +02001029 if (sess->fe->options & PR_O_IGNORE_PRB)
1030 goto failed_keep_alive;
1031
Willy Tarreau59234e92008-11-30 23:51:27 +01001032 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001033 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001034 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001035 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001036 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001037 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001038 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001039 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001040 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001041 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001042
Willy Tarreau87b09662015-04-03 00:22:06 +02001043 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001044 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001045 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001046 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001047 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001048
Willy Tarreaue7dff022015-04-03 01:14:29 +02001049 if (!(s->flags & SF_FINST_MASK))
1050 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001051 return 0;
1052 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001053
Willy Tarreau59234e92008-11-30 23:51:27 +01001054 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001055 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001056 if (!(s->flags & SF_ERR_MASK))
1057 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001058
Willy Tarreaufcffa692010-01-10 14:21:19 +01001059 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001060 goto failed_keep_alive;
1061
Willy Tarreau0f228a02015-05-01 15:37:53 +02001062 if (sess->fe->options & PR_O_IGNORE_PRB)
1063 goto failed_keep_alive;
1064
Willy Tarreau4076a152009-04-02 15:18:36 +02001065 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001066 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001067 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001068 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001069 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001070 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001071 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001072 stream_inc_http_err_ctr(s);
1073 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001074 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001075 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001076 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001077 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001078
Willy Tarreaue7dff022015-04-03 01:14:29 +02001079 if (!(s->flags & SF_FINST_MASK))
1080 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001081 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001082 }
1083
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001084 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001085 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001086 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001087#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001088 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001089 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001090 /* We need more data, we have to re-enable quick-ack in case we
1091 * previously disabled it, otherwise we might cause the client
1092 * to delay next data.
1093 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001094 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001095 }
1096#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001097
Willy Tarreaufcffa692010-01-10 14:21:19 +01001098 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1099 /* If the client starts to talk, let's fall back to
1100 * request timeout processing.
1101 */
1102 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001103 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001104 }
1105
Willy Tarreau59234e92008-11-30 23:51:27 +01001106 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001107 if (!tick_isset(req->analyse_exp)) {
1108 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1109 (txn->flags & TX_WAIT_NEXT_RQ) &&
1110 tick_isset(s->be->timeout.httpka))
1111 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1112 else
1113 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1114 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001115
Willy Tarreau59234e92008-11-30 23:51:27 +01001116 /* we're not ready yet */
1117 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001118
1119 failed_keep_alive:
1120 /* Here we process low-level errors for keep-alive requests. In
1121 * short, if the request is not the first one and it experiences
1122 * a timeout, read error or shutdown, we just silently close so
1123 * that the client can try again.
1124 */
1125 txn->status = 0;
1126 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001127 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001128 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001129 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001130 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001131 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001132 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001133 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001134
Willy Tarreaud787e662009-07-07 10:14:51 +02001135 /* OK now we have a complete HTTP request with indexed headers. Let's
1136 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001137 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001138 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001139 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001140 * byte after the last LF. msg->sov points to the first byte of data.
1141 * msg->eol cannot be trusted because it may have been left uninitialized
1142 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001143 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001144
Willy Tarreau87b09662015-04-03 00:22:06 +02001145 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001146 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001147
Willy Tarreaub16a5742010-01-10 14:46:16 +01001148 if (txn->flags & TX_WAIT_NEXT_RQ) {
1149 /* kill the pending keep-alive timeout */
1150 txn->flags &= ~TX_WAIT_NEXT_RQ;
1151 req->analyse_exp = TICK_ETERNITY;
1152 }
1153
1154
Willy Tarreaud787e662009-07-07 10:14:51 +02001155 /* Maybe we found in invalid header name while we were configured not
1156 * to block on that, so we have to capture it now.
1157 */
1158 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001159 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001160
Willy Tarreau59234e92008-11-30 23:51:27 +01001161 /*
1162 * 1: identify the method
1163 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001164 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001165
1166 /* we can make use of server redirect on GET and HEAD */
1167 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001168 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001169 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001170 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001171 /* PRI is reserved for the HTTP/2 preface */
1172 msg->err_pos = 0;
1173 goto return_bad_req;
1174 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001175
Willy Tarreau59234e92008-11-30 23:51:27 +01001176 /*
1177 * 2: check if the URI matches the monitor_uri.
1178 * We have to do this for every request which gets in, because
1179 * the monitor-uri is defined by the frontend.
1180 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001181 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1182 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001183 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001184 sess->fe->monitor_uri,
1185 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001186 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001187 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001188 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001189 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001190
Willy Tarreaue7dff022015-04-03 01:14:29 +02001191 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001192 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001193
Willy Tarreau59234e92008-11-30 23:51:27 +01001194 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001195 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001196 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001197
Willy Tarreau59234e92008-11-30 23:51:27 +01001198 ret = acl_pass(ret);
1199 if (cond->pol == ACL_COND_UNLESS)
1200 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001201
Willy Tarreau59234e92008-11-30 23:51:27 +01001202 if (ret) {
1203 /* we fail this request, let's return 503 service unavail */
1204 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001205 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001206 if (!(s->flags & SF_ERR_MASK))
1207 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001208 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001209 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001210 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001211
Joseph Herlant5ba80252018-11-15 09:25:36 -08001212 /* nothing to fail, let's reply normally */
Willy Tarreau59234e92008-11-30 23:51:27 +01001213 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001214 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001215 if (!(s->flags & SF_ERR_MASK))
1216 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001217 goto return_prx_cond;
1218 }
1219
1220 /*
1221 * 3: Maybe we have to copy the original REQURI for the logs ?
1222 * Note: we cannot log anymore if the request has been
1223 * classified as invalid.
1224 */
1225 if (unlikely(s->logs.logwait & LW_REQ)) {
1226 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001227 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001228 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001229
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001230 if (urilen >= global.tune.requri_len )
1231 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001232 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001233 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001234
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001235 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001236 s->do_log(s);
1237 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001238 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001239 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001240 }
Willy Tarreau06619262006-12-17 08:37:22 +01001241
Willy Tarreau91852eb2015-05-01 13:26:00 +02001242 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1243 * exactly one digit "." one digit. This check may be disabled using
1244 * option accept-invalid-http-request.
1245 */
1246 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1247 if (msg->sl.rq.v_l != 8) {
1248 msg->err_pos = msg->sl.rq.v;
1249 goto return_bad_req;
1250 }
1251
Willy Tarreauf37954d2018-06-15 18:31:02 +02001252 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1253 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1254 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1255 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001256 msg->err_pos = msg->sl.rq.v + 4;
1257 goto return_bad_req;
1258 }
1259 }
Willy Tarreau13317662015-05-01 13:47:08 +02001260 else {
1261 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1262 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1263 goto return_bad_req;
1264 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001265
Willy Tarreau5b154472009-12-21 20:11:07 +01001266 /* ... and check if the request is HTTP/1.1 or above */
1267 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001268 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
1269 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
1270 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001271 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001272
1273 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001274 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 +01001275
Willy Tarreau88d349d2010-01-25 12:15:43 +01001276 /* if the frontend has "option http-use-proxy-header", we'll check if
1277 * we have what looks like a proxied connection instead of a connection,
1278 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1279 * Note that this is *not* RFC-compliant, however browsers and proxies
1280 * happen to do that despite being non-standard :-(
1281 * We consider that a request not beginning with either '/' or '*' is
1282 * a proxied connection, which covers both "scheme://location" and
1283 * CONNECT ip:port.
1284 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001285 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001286 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01001287 txn->flags |= TX_USE_PX_CONN;
1288
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001289 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001290 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001291
Willy Tarreau59234e92008-11-30 23:51:27 +01001292 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001293 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02001294 http_capture_headers(ci_head(req), &txn->hdr_idx,
1295 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001296
Willy Tarreau557f1992015-05-01 10:05:17 +02001297 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1298 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001299 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001300 * The length of a message body is determined by one of the following
1301 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001302 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001303 * 1. Any response to a HEAD request and any response with a 1xx
1304 * (Informational), 204 (No Content), or 304 (Not Modified) status
1305 * code is always terminated by the first empty line after the
1306 * header fields, regardless of the header fields present in the
1307 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001308 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001309 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1310 * the connection will become a tunnel immediately after the empty
1311 * line that concludes the header fields. A client MUST ignore any
1312 * Content-Length or Transfer-Encoding header fields received in
1313 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001314 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001315 * 3. If a Transfer-Encoding header field is present and the chunked
1316 * transfer coding (Section 4.1) is the final encoding, the message
1317 * body length is determined by reading and decoding the chunked
1318 * data until the transfer coding indicates the data is complete.
1319 *
1320 * If a Transfer-Encoding header field is present in a response and
1321 * the chunked transfer coding is not the final encoding, the
1322 * message body length is determined by reading the connection until
1323 * it is closed by the server. If a Transfer-Encoding header field
1324 * is present in a request and the chunked transfer coding is not
1325 * the final encoding, the message body length cannot be determined
1326 * reliably; the server MUST respond with the 400 (Bad Request)
1327 * status code and then close the connection.
1328 *
1329 * If a message is received with both a Transfer-Encoding and a
1330 * Content-Length header field, the Transfer-Encoding overrides the
1331 * Content-Length. Such a message might indicate an attempt to
1332 * perform request smuggling (Section 9.5) or response splitting
1333 * (Section 9.4) and ought to be handled as an error. A sender MUST
1334 * remove the received Content-Length field prior to forwarding such
1335 * a message downstream.
1336 *
1337 * 4. If a message is received without Transfer-Encoding and with
1338 * either multiple Content-Length header fields having differing
1339 * field-values or a single Content-Length header field having an
1340 * invalid value, then the message framing is invalid and the
1341 * recipient MUST treat it as an unrecoverable error. If this is a
1342 * request message, the server MUST respond with a 400 (Bad Request)
1343 * status code and then close the connection. If this is a response
1344 * message received by a proxy, the proxy MUST close the connection
1345 * to the server, discard the received response, and send a 502 (Bad
1346 * Gateway) response to the client. If this is a response message
1347 * received by a user agent, the user agent MUST close the
1348 * connection to the server and discard the received response.
1349 *
1350 * 5. If a valid Content-Length header field is present without
1351 * Transfer-Encoding, its decimal value defines the expected message
1352 * body length in octets. If the sender closes the connection or
1353 * the recipient times out before the indicated number of octets are
1354 * received, the recipient MUST consider the message to be
1355 * incomplete and close the connection.
1356 *
1357 * 6. If this is a request message and none of the above are true, then
1358 * the message body length is zero (no message body is present).
1359 *
1360 * 7. Otherwise, this is a response message without a declared message
1361 * body length, so the message body length is determined by the
1362 * number of octets received prior to the server closing the
1363 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001364 */
1365
Willy Tarreau32b47f42009-10-18 20:55:02 +02001366 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001367 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001368 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001369 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001370 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001371 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001372 /* chunked not last, return badreq */
1373 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001374 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001375 }
1376
Willy Tarreau1c913912015-04-30 10:57:51 +02001377 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001378 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001379 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001380 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001381 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1382 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001383 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001384 signed long long cl;
1385
Willy Tarreauad14f752011-09-02 20:33:27 +02001386 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001387 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001388 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001389 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001390
Willy Tarreauad14f752011-09-02 20:33:27 +02001391 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001392 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001393 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001394 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001395
Willy Tarreauad14f752011-09-02 20:33:27 +02001396 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001397 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001398 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001399 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001400
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001401 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001402 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001403 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001404 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001405
Christopher Fauletbe821b92017-03-30 11:21:53 +02001406 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001407 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001408 }
1409
Willy Tarreau34dfc602015-05-01 10:09:49 +02001410 /* even bodyless requests have a known length */
1411 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001412
Willy Tarreau179085c2014-04-28 16:48:56 +02001413 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1414 * only change if both the request and the config reference something else.
1415 * Option httpclose by itself sets tunnel mode where headers are mangled.
1416 * However, if another mode is set, it will affect it (eg: server-close/
1417 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1418 * if FE and BE have the same settings (common). The method consists in
1419 * checking if options changed between the two calls (implying that either
1420 * one is non-null, or one of them is non-null and we are there for the first
1421 * time.
1422 */
1423 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001424 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001425 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001426
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001427 /* we may have to wait for the request's body */
1428 if ((s->be->options & PR_O_WREQ_BODY) &&
1429 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1430 req->analysers |= AN_REQ_HTTP_BODY;
1431
Willy Tarreau83ece462017-12-21 15:13:09 +01001432 /*
1433 * RFC7234#4:
1434 * A cache MUST write through requests with methods
1435 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1436 * the origin server; i.e., a cache is not allowed
1437 * to generate a reply to such a request before
1438 * having forwarded the request and having received
1439 * a corresponding response.
1440 *
1441 * RFC7231#4.2.1:
1442 * Of the request methods defined by this
1443 * specification, the GET, HEAD, OPTIONS, and TRACE
1444 * methods are defined to be safe.
1445 */
1446 if (likely(txn->meth == HTTP_METH_GET ||
1447 txn->meth == HTTP_METH_HEAD ||
1448 txn->meth == HTTP_METH_OPTIONS ||
1449 txn->meth == HTTP_METH_TRACE))
1450 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1451
Willy Tarreaud787e662009-07-07 10:14:51 +02001452 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001453 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001454 req->analyse_exp = TICK_ETERNITY;
1455 return 1;
1456
1457 return_bad_req:
1458 /* We centralize bad requests processing here */
1459 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1460 /* we detected a parsing error. We want to archive this request
1461 * in the dedicated proxy area for later troubleshooting.
1462 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001463 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001464 }
1465
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001466 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001467 txn->req.msg_state = HTTP_MSG_ERROR;
1468 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001469 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001470
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001471 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001472 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001473 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001474
1475 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001476 if (!(s->flags & SF_ERR_MASK))
1477 s->flags |= SF_ERR_PRXCOND;
1478 if (!(s->flags & SF_FINST_MASK))
1479 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001480
Christopher Faulet0184ea72017-01-05 14:06:34 +01001481 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001482 req->analyse_exp = TICK_ETERNITY;
1483 return 0;
1484}
1485
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001486
Willy Tarreau347a35d2013-11-22 17:51:09 +01001487/* This function prepares an applet to handle the stats. It can deal with the
1488 * "100-continue" expectation, check that admin rules are met for POST requests,
1489 * and program a response message if something was unexpected. It cannot fail
1490 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001491 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001492 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001493 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001494 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001495int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001496{
1497 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001498 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001499 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001500 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001501 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001502 struct uri_auth *uri_auth = s->be->uri_auth;
1503 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001504 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001505
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001506 appctx = si_appctx(si);
1507 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1508 appctx->st1 = appctx->st2 = 0;
1509 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1510 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001511 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001512 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001513
Willy Tarreauf37954d2018-06-15 18:31:02 +02001514 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001515 lookup = uri + uri_auth->uri_len;
1516
1517 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1518 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001519 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001520 break;
1521 }
1522 }
1523
1524 if (uri_auth->refresh) {
1525 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1526 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001527 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001528 break;
1529 }
1530 }
1531 }
1532
1533 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1534 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001535 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001536 break;
1537 }
1538 }
1539
Willy Tarreau1e62df92016-01-11 18:57:53 +01001540 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1541 if (memcmp(h, ";typed", 6) == 0) {
1542 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1543 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1544 break;
1545 }
1546 }
1547
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001548 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1549 if (memcmp(h, ";st=", 4) == 0) {
1550 int i;
1551 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001552 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001553 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1554 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001555 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001556 break;
1557 }
1558 }
1559 break;
1560 }
1561 }
1562
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001563 appctx->ctx.stats.scope_str = 0;
1564 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001565 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1566 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1567 int itx = 0;
1568 const char *h2;
1569 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1570 const char *err;
1571
1572 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1573 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001574 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001575 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1576 itx++;
1577 h++;
1578 }
1579
1580 if (itx > STAT_SCOPE_TXT_MAXLEN)
1581 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001582 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001583
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001584 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001585 memcpy(scope_txt, h2, itx);
1586 scope_txt[itx] = '\0';
1587 err = invalid_char(scope_txt);
1588 if (err) {
1589 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001590 appctx->ctx.stats.scope_str = 0;
1591 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001592 }
1593 break;
1594 }
1595 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001596
1597 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001598 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001599 int ret = 1;
1600
1601 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001602 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 +01001603 ret = acl_pass(ret);
1604 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1605 ret = !ret;
1606 }
1607
1608 if (ret) {
1609 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001610 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001611 break;
1612 }
1613 }
1614
1615 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001616 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001617 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001618 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001619 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1620 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001621 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001622 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001623 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001624 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1625 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001626 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001627 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001628 else {
1629 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001630 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001631 }
1632
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001633 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001634 return 1;
1635}
1636
Lukas Tribus67db8df2013-06-23 17:37:13 +02001637/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
1638 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
1639 */
Vincent Bernat6e615892016-05-18 16:17:44 +02001640void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001641{
1642#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02001643 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001644 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1645#endif
1646#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02001647 if (from->ss_family == AF_INET6) {
1648 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02001649 /* v4-mapped addresses need IP_TOS */
1650 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1651 else
1652 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
1653 }
1654#endif
1655}
1656
Willy Tarreau87b09662015-04-03 00:22:06 +02001657int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001658 const char* name, unsigned int name_len,
1659 const char *str, struct my_regex *re,
1660 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001661{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001662 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001663 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02001664 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001665 int (*http_find_hdr_func)(const char *name, int len, char *sol,
1666 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02001667 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001668
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001669 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06001670
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001671 /* Choose the header browsing function. */
1672 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001673 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001674 http_find_hdr_func = http_find_header2;
1675 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001676 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001677 http_find_hdr_func = http_find_full_header2;
1678 break;
1679 default: /* impossible */
1680 return -1;
1681 }
1682
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001683 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
1684 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02001685 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001686 char *val = ctx.line + ctx.val;
1687 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06001688
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001689 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
1690 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06001691
Willy Tarreau6e27be12018-08-22 04:46:47 +02001692 len = exp_replace(output->area, output->size, val, str, pmatch);
1693 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06001694 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06001695
Willy Tarreau6e27be12018-08-22 04:46:47 +02001696 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06001697
1698 hdr->len += delta;
1699 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001700
1701 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001702 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06001703 }
1704
1705 return 0;
1706}
1707
Willy Tarreau87b09662015-04-03 00:22:06 +02001708static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001709 const char* name, unsigned int name_len,
1710 struct list *fmt, struct my_regex *re,
1711 int action)
1712{
Willy Tarreau83061a82018-07-13 11:56:34 +02001713 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001714 int ret = -1;
1715
1716 replace = alloc_trash_chunk();
1717 if (!replace)
1718 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001719
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001720 replace->data = build_logline(s, replace->area, replace->size, fmt);
1721 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001722 goto leave;
1723
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001724 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1725 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001726
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001727 leave:
1728 free_trash_chunk(replace);
1729 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001730}
1731
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001732/*
1733 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
1734 * built according to <fmt> log line format.
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001735 * If <early_hints> is NULL, it is allocated and the HTTP 103 response first
1736 * line is inserted before the header. If an error occurred <early_hints> is
1737 * released and NULL is returned. On success the updated buffer is returned.
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001738 */
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001739static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints,
1740 const char* name, unsigned int name_len,
1741 struct list *fmt)
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001742{
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001743 if (!early_hints) {
1744 early_hints = alloc_trash_chunk();
1745 if (!early_hints)
1746 goto fail;
1747 if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len))
1748 goto fail;
1749 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001750
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001751 if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2))
1752 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001753
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001754 early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt);
1755 if (!chunk_memcat(early_hints, "\r\n", 2))
1756 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001757
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001758 return early_hints;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001759
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001760 fail:
1761 free_trash_chunk(early_hints);
1762 return NULL;
1763}
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001764
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001765/* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the
1766 * response is added. If an error occurred or if another response was already
1767 * sent, this function does nothing.
1768 */
1769static void http_send_early_hints(struct stream *s, struct buffer *early_hints)
1770{
1771 struct channel *chn = s->txn->rsp.chn;
1772 char *cur_ptr = ci_head(chn);
1773 int ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001774
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001775 /* If a response was already sent, skip early hints */
1776 if (s->txn->status > 0)
1777 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001778
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001779 if (!chunk_memcat(early_hints, "\r\n", 2))
1780 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001781
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001782 ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints));
1783 c_adv(chn, ret);
1784 chn->total += ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001785}
1786
Willy Tarreau87b09662015-04-03 00:22:06 +02001787/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001788 * transaction <txn>. Returns the verdict of the first rule that prevents
1789 * further processing of the request (auth, deny, ...), and defaults to
1790 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1791 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001792 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1793 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1794 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001795 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001796enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001797http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001798{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001799 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001800 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001801 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001802 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001803 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001804 const char *auth_realm;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001805 struct buffer *early_hints = NULL;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001806 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001807 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001808 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001809
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001810 /* If "the current_rule_list" match the executed rule list, we are in
1811 * resume condition. If a resume is needed it is always in the action
1812 * and never in the ACL or converters. In this case, we initialise the
1813 * current rule, and go to the action execution point.
1814 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001815 if (s->current_rule) {
1816 rule = s->current_rule;
1817 s->current_rule = NULL;
1818 if (s->current_rule_list == rules)
1819 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001820 }
1821 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001822
Willy Tarreauff011f22011-01-06 17:51:27 +01001823 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001824
Willy Tarreau96257ec2012-12-27 10:46:37 +01001825 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001826 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001827 int ret;
1828
Willy Tarreau192252e2015-04-04 01:47:55 +02001829 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001830 ret = acl_pass(ret);
1831
Willy Tarreauff011f22011-01-06 17:51:27 +01001832 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001833 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001834
1835 if (!ret) /* condition not matched */
1836 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001837 }
1838
Willy Tarreauacc98002015-09-27 23:34:39 +02001839 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001840resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001841 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001842 case ACT_ACTION_ALLOW:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001843 rule_ret = HTTP_RULE_RES_STOP;
1844 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001845
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001846 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001847 if (deny_status)
1848 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001849 rule_ret = HTTP_RULE_RES_DENY;
1850 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001851
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001852 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001853 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001854 if (deny_status)
1855 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001856 rule_ret = HTTP_RULE_RES_DENY;
1857 goto end;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001858
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001859 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001860 /* Be sure to send any pending HTTP 103 response first */
1861 if (early_hints) {
1862 http_send_early_hints(s, early_hints);
1863 free_trash_chunk(early_hints);
1864 early_hints = NULL;
1865 }
Willy Tarreauae3c0102014-04-28 23:22:08 +02001866 /* Auth might be performed on regular http-req rules as well as on stats */
1867 auth_realm = rule->arg.auth.realm;
1868 if (!auth_realm) {
1869 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1870 auth_realm = STATS_DEFAULT_REALM;
1871 else
1872 auth_realm = px->id;
1873 }
1874 /* send 401/407 depending on whether we use a proxy or not. We still
1875 * count one error, because normal browsing won't significantly
1876 * increase the counter but brute force attempts will.
1877 */
1878 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1879 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001880 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001881 stream_inc_http_err_ctr(s);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001882 rule_ret = HTTP_RULE_RES_ABRT;
1883 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001884
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001885 case ACT_HTTP_REDIR:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001886 /* Be sure to send any pending HTTP 103 response first */
1887 if (early_hints) {
1888 http_send_early_hints(s, early_hints);
1889 free_trash_chunk(early_hints);
1890 early_hints = NULL;
1891 }
Christopher Fauletea827bd2018-11-15 15:34:11 +01001892 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau0b748332014-04-29 00:13:29 +02001893 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Fauletea827bd2018-11-15 15:34:11 +01001894 rule_ret = HTTP_RULE_RES_BADREQ;
1895 goto end;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001896
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001897 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001898 s->task->nice = rule->arg.nice;
1899 break;
1900
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001901 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001902 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001903 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001904 break;
1905
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001906 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02001907#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001908 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001909 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02001910#endif
1911 break;
1912
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001913 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001914 s->logs.level = rule->arg.loglevel;
1915 break;
1916
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001917 case ACT_HTTP_REPLACE_HDR:
1918 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001919 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1920 rule->arg.hdr_add.name_len,
1921 &rule->arg.hdr_add.fmt,
Christopher Fauletea827bd2018-11-15 15:34:11 +01001922 &rule->arg.hdr_add.re, rule->action)) {
1923 rule_ret = HTTP_RULE_RES_BADREQ;
1924 goto end;
1925 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001926 break;
1927
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001928 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001929 ctx.idx = 0;
1930 /* remove all occurrences of the header */
1931 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001932 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001933 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001934 }
Willy Tarreau85603282015-01-21 20:39:27 +01001935 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001936
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001937 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001938 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001939 /* The scope of the trash buffer must be limited to this function. The
1940 * build_logline() function can execute a lot of other function which
1941 * can use the trash buffer. So for limiting the scope of this global
1942 * buffer, we build first the header value using build_logline, and
1943 * after we store the header name.
1944 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001945 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001946
1947 replace = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001948 if (!replace) {
1949 rule_ret = HTTP_RULE_RES_BADREQ;
1950 goto end;
1951 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001952
Thierry Fournier4b788f72016-06-01 13:35:36 +02001953 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001954 len += build_logline(s, replace->area + len,
1955 replace->size - len,
1956 &rule->arg.hdr_add.fmt);
1957 memcpy(replace->area, rule->arg.hdr_add.name,
1958 rule->arg.hdr_add.name_len);
1959 replace->area[rule->arg.hdr_add.name_len] = ':';
1960 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1961 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001962
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001963 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001964 /* remove all occurrences of the header */
1965 ctx.idx = 0;
1966 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001967 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001968 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1969 }
1970 }
1971
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001972 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001973 static unsigned char rate_limit = 0;
1974
1975 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001976 replace->area[rule->arg.hdr_add.name_len] = 0;
1977 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,
1978 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001979 }
1980
1981 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1982 if (sess->fe != s->be)
1983 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1984 if (sess->listener->counters)
1985 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1986 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001987
1988 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001989 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001990 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001991
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001992 case ACT_HTTP_DEL_ACL:
1993 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001994 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001995 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001996
1997 /* collect reference */
1998 ref = pat_ref_lookup(rule->arg.map.ref);
1999 if (!ref)
2000 continue;
2001
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002002 /* allocate key */
2003 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002004 if (!key) {
2005 rule_ret = HTTP_RULE_RES_BADREQ;
2006 goto end;
2007 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002008
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002009 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002010 key->data = build_logline(s, key->area, key->size,
2011 &rule->arg.map.key);
2012 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002013
2014 /* perform update */
2015 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002016 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002017 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002018 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002019
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002020 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002021 break;
2022 }
2023
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002024 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002025 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002026 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002027
2028 /* collect reference */
2029 ref = pat_ref_lookup(rule->arg.map.ref);
2030 if (!ref)
2031 continue;
2032
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002033 /* allocate key */
2034 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002035 if (!key) {
2036 rule_ret = HTTP_RULE_RES_BADREQ;
2037 goto end;
2038 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002039
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002040 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002041 key->data = build_logline(s, key->area, key->size,
2042 &rule->arg.map.key);
2043 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002044
2045 /* perform update */
2046 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002047 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002048 if (pat_ref_find_elt(ref, key->area) == NULL)
2049 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002050 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002051
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002052 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002053 break;
2054 }
2055
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002056 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002057 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002058 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002059
2060 /* collect reference */
2061 ref = pat_ref_lookup(rule->arg.map.ref);
2062 if (!ref)
2063 continue;
2064
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002065 /* allocate key */
2066 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002067 if (!key) {
2068 rule_ret = HTTP_RULE_RES_BADREQ;
2069 goto end;
2070 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002071
2072 /* allocate value */
2073 value = alloc_trash_chunk();
2074 if (!value) {
2075 free_trash_chunk(key);
Christopher Fauletea827bd2018-11-15 15:34:11 +01002076 rule_ret = HTTP_RULE_RES_BADREQ;
2077 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002078 }
2079
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002080 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002081 key->data = build_logline(s, key->area, key->size,
2082 &rule->arg.map.key);
2083 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002084
2085 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002086 value->data = build_logline(s, value->area,
2087 value->size,
2088 &rule->arg.map.value);
2089 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002090
2091 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002092 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002093 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002094 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002095 else
2096 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002097 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002098
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002099 free_trash_chunk(key);
2100 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002101 break;
2102 }
William Lallemand73025dd2014-04-24 14:38:37 +02002103
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002104 case ACT_HTTP_EARLY_HINT:
2105 if (!(txn->req.flags & HTTP_MSGF_VER_11))
2106 break;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002107 early_hints = http_apply_early_hint_rule(s, early_hints,
2108 rule->arg.early_hint.name,
2109 rule->arg.early_hint.name_len,
2110 &rule->arg.early_hint.fmt);
2111 if (!early_hints) {
Christopher Fauletea827bd2018-11-15 15:34:11 +01002112 rule_ret = HTTP_RULE_RES_DONE;
2113 goto end;
2114 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002115 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002116 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002117 if ((s->req.flags & CF_READ_ERROR) ||
2118 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2119 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2120 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002121 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002122
Willy Tarreauacc98002015-09-27 23:34:39 +02002123 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002124 case ACT_RET_ERR:
2125 case ACT_RET_CONT:
2126 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002127 case ACT_RET_STOP:
Christopher Fauletea827bd2018-11-15 15:34:11 +01002128 rule_ret = HTTP_RULE_RES_DONE;
2129 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002130 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002131 s->current_rule = rule;
Christopher Fauletea827bd2018-11-15 15:34:11 +01002132 rule_ret = HTTP_RULE_RES_YIELD;
2133 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002134 }
William Lallemand73025dd2014-04-24 14:38:37 +02002135 break;
2136
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002137 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002138 /* Note: only the first valid tracking parameter of each
2139 * applies.
2140 */
2141
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002142 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002143 struct stktable *t;
2144 struct stksess *ts;
2145 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002146 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002147
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002148 t = rule->arg.trk_ctr.table.t;
2149 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 +02002150
2151 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002152 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002153
2154 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002155 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2156 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2157 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002158 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002159
Emeric Brun819fc6f2017-06-13 19:37:32 +02002160 if (ptr1)
2161 stktable_data_cast(ptr1, http_req_cnt)++;
2162
2163 if (ptr2)
2164 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2165 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2166
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002167 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002168
2169 /* If data was modified, we need to touch to re-schedule sync */
2170 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002171 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002172
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002173 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002174 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002175 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002176 }
2177 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002178 break;
2179
Joseph Herlant5ba80252018-11-15 09:25:36 -08002180 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002181 default:
2182 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002183 }
2184 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002185
Christopher Fauletea827bd2018-11-15 15:34:11 +01002186 end:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002187 if (early_hints) {
2188 http_send_early_hints(s, early_hints);
2189 free_trash_chunk(early_hints);
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002190 }
2191
Willy Tarreau96257ec2012-12-27 10:46:37 +01002192 /* we reached the end of the rules, nothing to report */
Christopher Fauletea827bd2018-11-15 15:34:11 +01002193 return rule_ret;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002194}
2195
Willy Tarreau71241ab2012-12-27 11:30:54 +01002196
Willy Tarreau51d861a2015-05-22 17:30:48 +02002197/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2198 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2199 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2200 * is returned, the process can continue the evaluation of next rule list. If
2201 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2202 * is returned, it means the operation could not be processed and a server error
2203 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2204 * deny rule. If *YIELD is returned, the caller must call again the function
2205 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002206 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002207enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002208http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002209{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002210 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002211 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002212 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002213 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002214 struct hdr_ctx ctx;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002215 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02002216 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002217
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002218 /* If "the current_rule_list" match the executed rule list, we are in
2219 * resume condition. If a resume is needed it is always in the action
2220 * and never in the ACL or converters. In this case, we initialise the
2221 * current rule, and go to the action execution point.
2222 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002223 if (s->current_rule) {
2224 rule = s->current_rule;
2225 s->current_rule = NULL;
2226 if (s->current_rule_list == rules)
2227 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002228 }
2229 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002230
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002231 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002232
2233 /* check optional condition */
2234 if (rule->cond) {
2235 int ret;
2236
Willy Tarreau192252e2015-04-04 01:47:55 +02002237 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002238 ret = acl_pass(ret);
2239
2240 if (rule->cond->pol == ACL_COND_UNLESS)
2241 ret = !ret;
2242
2243 if (!ret) /* condition not matched */
2244 continue;
2245 }
2246
Willy Tarreauacc98002015-09-27 23:34:39 +02002247 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002248resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002249 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002250 case ACT_ACTION_ALLOW:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002251 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2252 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002253
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002254 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002255 txn->flags |= TX_SVDENY;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002256 rule_ret = HTTP_RULE_RES_STOP;
2257 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002258
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002259 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002260 s->task->nice = rule->arg.nice;
2261 break;
2262
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002263 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002264 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002265 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002266 break;
2267
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002268 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002269#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002270 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002271 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002272#endif
2273 break;
2274
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002275 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002276 s->logs.level = rule->arg.loglevel;
2277 break;
2278
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002279 case ACT_HTTP_REPLACE_HDR:
2280 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002281 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2282 rule->arg.hdr_add.name_len,
2283 &rule->arg.hdr_add.fmt,
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002284 &rule->arg.hdr_add.re, rule->action)) {
2285 rule_ret = HTTP_RULE_RES_BADREQ;
2286 goto end;
2287 }
Sasha Pachev218f0642014-06-16 12:05:59 -06002288 break;
2289
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002290 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002291 ctx.idx = 0;
2292 /* remove all occurrences of the header */
2293 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002294 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002295 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2296 }
Willy Tarreau85603282015-01-21 20:39:27 +01002297 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002298
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002299 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002300 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002301 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002302
2303 replace = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002304 if (!replace) {
2305 rule_ret = HTTP_RULE_RES_BADREQ;
2306 goto end;
2307 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002308
2309 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002310 memcpy(replace->area, rule->arg.hdr_add.name,
2311 rule->arg.hdr_add.name_len);
2312 replace->data = rule->arg.hdr_add.name_len;
2313 replace->area[replace->data++] = ':';
2314 replace->area[replace->data++] = ' ';
2315 replace->data += build_logline(s,
2316 replace->area + replace->data,
2317 replace->size - replace->data,
2318 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002319
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002320 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002321 /* remove all occurrences of the header */
2322 ctx.idx = 0;
2323 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002324 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002325 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2326 }
2327 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002328
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002329 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002330 static unsigned char rate_limit = 0;
2331
2332 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002333 replace->area[rule->arg.hdr_add.name_len] = 0;
2334 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,
2335 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002336 }
2337
2338 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2339 if (sess->fe != s->be)
2340 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2341 if (sess->listener->counters)
2342 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2343 if (objt_server(s->target))
2344 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2345 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002346
2347 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002348 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002349 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002350
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002351 case ACT_HTTP_DEL_ACL:
2352 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002353 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002354 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002355
2356 /* collect reference */
2357 ref = pat_ref_lookup(rule->arg.map.ref);
2358 if (!ref)
2359 continue;
2360
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002361 /* allocate key */
2362 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002363 if (!key) {
2364 rule_ret = HTTP_RULE_RES_BADREQ;
2365 goto end;
2366 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002367
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002368 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002369 key->data = build_logline(s, key->area, key->size,
2370 &rule->arg.map.key);
2371 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002372
2373 /* perform update */
2374 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002375 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002376 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002377 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002378
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002379 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002380 break;
2381 }
2382
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002383 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002384 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002385 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002386
2387 /* collect reference */
2388 ref = pat_ref_lookup(rule->arg.map.ref);
2389 if (!ref)
2390 continue;
2391
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002392 /* allocate key */
2393 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002394 if (!key) {
2395 rule_ret = HTTP_RULE_RES_BADREQ;
2396 goto end;
2397 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002398
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002399 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002400 key->data = build_logline(s, key->area, key->size,
2401 &rule->arg.map.key);
2402 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002403
2404 /* perform update */
2405 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002406 if (pat_ref_find_elt(ref, key->area) == NULL)
2407 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002408
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002409 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002410 break;
2411 }
2412
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002413 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002414 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002415 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002416
2417 /* collect reference */
2418 ref = pat_ref_lookup(rule->arg.map.ref);
2419 if (!ref)
2420 continue;
2421
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002422 /* allocate key */
2423 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002424 if (!key) {
2425 rule_ret = HTTP_RULE_RES_BADREQ;
2426 goto end;
2427 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002428
2429 /* allocate value */
2430 value = alloc_trash_chunk();
2431 if (!value) {
2432 free_trash_chunk(key);
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002433 rule_ret = HTTP_RULE_RES_BADREQ;
2434 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002435 }
2436
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002437 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002438 key->data = build_logline(s, key->area, key->size,
2439 &rule->arg.map.key);
2440 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002441
2442 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002443 value->data = build_logline(s, value->area,
2444 value->size,
2445 &rule->arg.map.value);
2446 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002447
2448 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002449 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002450 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002451 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002452 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002453 else
2454 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002455 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002456 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002457 free_trash_chunk(key);
2458 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002459 break;
2460 }
William Lallemand73025dd2014-04-24 14:38:37 +02002461
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002462 case ACT_HTTP_REDIR:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002463 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002464 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002465 rule_ret = HTTP_RULE_RES_BADREQ;
2466 goto end;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002467
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002468 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2469 /* Note: only the first valid tracking parameter of each
2470 * applies.
2471 */
2472
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002473 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002474 struct stktable *t;
2475 struct stksess *ts;
2476 struct stktable_key *key;
2477 void *ptr;
2478
2479 t = rule->arg.trk_ctr.table.t;
2480 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2481
2482 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002483 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002484
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002485 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002486
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002487 /* let's count a new HTTP request as it's the first time we do it */
2488 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2489 if (ptr)
2490 stktable_data_cast(ptr, http_req_cnt)++;
2491
2492 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2493 if (ptr)
2494 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2495 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2496
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002497 /* When the client triggers a 4xx from the server, it's most often due
2498 * to a missing object or permission. These events should be tracked
2499 * because if they happen often, it may indicate a brute force or a
2500 * vulnerability scan. Normally this is done when receiving the response
2501 * but here we're tracking after this ought to have been done so we have
2502 * to do it on purpose.
2503 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002504 if ((unsigned)(txn->status - 400) < 100) {
2505 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2506 if (ptr)
2507 stktable_data_cast(ptr, http_err_cnt)++;
2508
2509 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2510 if (ptr)
2511 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2512 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2513 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002514
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002515 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002516
Emeric Brun0fed0b02017-11-29 16:15:07 +01002517 /* If data was modified, we need to touch to re-schedule sync */
2518 stktable_touch_local(t, ts, 0);
2519
Emeric Brun819fc6f2017-06-13 19:37:32 +02002520 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2521 if (sess->fe != s->be)
2522 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2523
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002524 }
2525 }
2526 break;
2527
Thierry FOURNIER42148732015-09-02 17:17:33 +02002528 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002529 if ((s->req.flags & CF_READ_ERROR) ||
2530 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2531 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2532 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002533 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002534
Willy Tarreauacc98002015-09-27 23:34:39 +02002535 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002536 case ACT_RET_ERR:
2537 case ACT_RET_CONT:
2538 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002539 case ACT_RET_STOP:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002540 rule_ret = HTTP_RULE_RES_STOP;
2541 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002542 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002543 s->current_rule = rule;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002544 rule_ret = HTTP_RULE_RES_YIELD;
2545 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002546 }
William Lallemand73025dd2014-04-24 14:38:37 +02002547 break;
2548
Joseph Herlant5ba80252018-11-15 09:25:36 -08002549 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002550 default:
2551 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002552 }
2553 }
2554
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002555 end:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002556 /* we reached the end of the rules, nothing to report */
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002557 return rule_ret;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002558}
2559
2560
Willy Tarreau71241ab2012-12-27 11:30:54 +01002561/* Perform an HTTP redirect based on the information in <rule>. The function
2562 * returns non-zero on success, or zero in case of a, irrecoverable error such
2563 * as too large a request to build a valid response.
2564 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002565int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002566{
Willy Tarreaub329a312015-05-22 16:27:37 +02002567 struct http_msg *req = &txn->req;
2568 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002569 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002570 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002571 int ret = 0;
2572
Christopher Fauletf2824e62018-10-01 12:12:37 +02002573 if (IS_HTX_STRM(s))
2574 return htx_apply_redirect_rule(rule, s, txn);
2575
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002576 chunk = alloc_trash_chunk();
2577 if (!chunk)
2578 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002579
2580 /* build redirect message */
2581 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002582 case 308:
2583 msg_fmt = HTTP_308;
2584 break;
2585 case 307:
2586 msg_fmt = HTTP_307;
2587 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002588 case 303:
2589 msg_fmt = HTTP_303;
2590 break;
2591 case 301:
2592 msg_fmt = HTTP_301;
2593 break;
2594 case 302:
2595 default:
2596 msg_fmt = HTTP_302;
2597 break;
2598 }
2599
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002600 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2601 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002602
2603 switch(rule->type) {
2604 case REDIRECT_TYPE_SCHEME: {
2605 const char *path;
2606 const char *host;
2607 struct hdr_ctx ctx;
2608 int pathlen;
2609 int hostlen;
2610
2611 host = "";
2612 hostlen = 0;
2613 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002614 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002615 host = ctx.line + ctx.val;
2616 hostlen = ctx.vlen;
2617 }
2618
Willy Tarreau6b952c82018-09-10 17:45:34 +02002619 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002620 /* build message using path */
2621 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002622 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002623 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2624 int qs = 0;
2625 while (qs < pathlen) {
2626 if (path[qs] == '?') {
2627 pathlen = qs;
2628 break;
2629 }
2630 qs++;
2631 }
2632 }
2633 } else {
2634 path = "/";
2635 pathlen = 1;
2636 }
2637
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002638 if (rule->rdr_str) { /* this is an old "redirect" rule */
2639 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002640 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002641 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002642
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002643 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002644 memcpy(chunk->area + chunk->data, rule->rdr_str,
2645 rule->rdr_len);
2646 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002647 }
2648 else {
2649 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002650 chunk->data += build_logline(s,
2651 chunk->area + chunk->data,
2652 chunk->size - chunk->data,
2653 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002654
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002655 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002656 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002657 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002658 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002659 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002660 memcpy(chunk->area + chunk->data, "://", 3);
2661 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002662
2663 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002664 memcpy(chunk->area + chunk->data, host, hostlen);
2665 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002666
2667 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002668 memcpy(chunk->area + chunk->data, path, pathlen);
2669 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002670
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002671 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002672 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002673 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002674 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002675 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002676 chunk->area[chunk->data] = '/';
2677 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002678 }
2679
2680 break;
2681 }
2682 case REDIRECT_TYPE_PREFIX: {
2683 const char *path;
2684 int pathlen;
2685
Willy Tarreau6b952c82018-09-10 17:45:34 +02002686 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002687 /* build message using path */
2688 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002689 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002690 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2691 int qs = 0;
2692 while (qs < pathlen) {
2693 if (path[qs] == '?') {
2694 pathlen = qs;
2695 break;
2696 }
2697 qs++;
2698 }
2699 }
2700 } else {
2701 path = "/";
2702 pathlen = 1;
2703 }
2704
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002705 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002706 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002707 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002708
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002709 /* add prefix. Note that if prefix == "/", we don't want to
2710 * add anything, otherwise it makes it hard for the user to
2711 * configure a self-redirection.
2712 */
2713 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002714 memcpy(chunk->area + chunk->data,
2715 rule->rdr_str, rule->rdr_len);
2716 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002717 }
2718 }
2719 else {
2720 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002721 chunk->data += build_logline(s,
2722 chunk->area + chunk->data,
2723 chunk->size - chunk->data,
2724 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002725
2726 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002727 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002728 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002729 }
2730
2731 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002732 memcpy(chunk->area + chunk->data, path, pathlen);
2733 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002734
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002735 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002736 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002737 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002738 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002739 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002740 chunk->area[chunk->data] = '/';
2741 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002742 }
2743
2744 break;
2745 }
2746 case REDIRECT_TYPE_LOCATION:
2747 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002748 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002749 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002750 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002751
2752 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002753 memcpy(chunk->area + chunk->data, rule->rdr_str,
2754 rule->rdr_len);
2755 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002756 }
2757 else {
2758 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002759 chunk->data += build_logline(s,
2760 chunk->area + chunk->data,
2761 chunk->size - chunk->data,
2762 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002763
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002764 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002765 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002766 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002767 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002768 break;
2769 }
2770
2771 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002772 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2773 chunk->data += 14;
2774 memcpy(chunk->area + chunk->data, rule->cookie_str,
2775 rule->cookie_len);
2776 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002777 }
2778
Willy Tarreau19b14122017-02-28 09:48:11 +01002779 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002780 txn->status = rule->code;
2781 /* let's log the request time */
2782 s->logs.tv_request = now;
2783
Christopher Fauletbe821b92017-03-30 11:21:53 +02002784 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002785 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2786 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2787 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002788 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002789 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002790 memcpy(chunk->area + chunk->data,
2791 "\r\nProxy-Connection: keep-alive", 30);
2792 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002793 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002794 memcpy(chunk->area + chunk->data,
2795 "\r\nConnection: keep-alive", 24);
2796 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002797 }
2798 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002799 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2800 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002801 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002802 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002803 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002804 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002805 req->next -= req->sov;
2806 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002807 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002808 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002809 req->msg_state = HTTP_MSG_CLOSED;
2810 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002811 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002812 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002813 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002814 /* let the server side turn to SI_ST_CLO */
2815 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002816 } else {
2817 /* keep-alive not possible */
2818 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002819 memcpy(chunk->area + chunk->data,
2820 "\r\nProxy-Connection: close\r\n\r\n", 29);
2821 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002822 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002823 memcpy(chunk->area + chunk->data,
2824 "\r\nConnection: close\r\n\r\n", 23);
2825 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002826 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002827 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002828 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002829 }
2830
Willy Tarreaue7dff022015-04-03 01:14:29 +02002831 if (!(s->flags & SF_ERR_MASK))
2832 s->flags |= SF_ERR_LOCAL;
2833 if (!(s->flags & SF_FINST_MASK))
2834 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002835
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002836 ret = 1;
2837 leave:
2838 free_trash_chunk(chunk);
2839 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002840}
2841
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002842/* This stream analyser runs all HTTP request processing which is common to
2843 * frontends and backends, which means blocking ACLs, filters, connection-close,
2844 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002845 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002846 * either needs more data or wants to immediately abort the request (eg: deny,
2847 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002848 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002849int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002850{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002851 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002852 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002853 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002854 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002855 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002856 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002857 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002858 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002859
Christopher Faulete0768eb2018-10-03 16:38:02 +02002860 if (IS_HTX_STRM(s))
2861 return htx_process_req_common(s, req, an_bit, px);
2862
Willy Tarreau655dce92009-11-08 13:10:58 +01002863 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002864 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002865 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002866 }
2867
Christopher Faulet45073512018-07-20 10:16:29 +02002868 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 +02002869 now_ms, __FUNCTION__,
2870 s,
2871 req,
2872 req->rex, req->wex,
2873 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002874 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002875 req->analysers);
2876
Willy Tarreau65410832014-04-28 21:25:43 +02002877 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002878 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002879
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002880 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002881 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002882 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002883
Willy Tarreau0b748332014-04-29 00:13:29 +02002884 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002885 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2886 goto return_prx_yield;
2887
Willy Tarreau0b748332014-04-29 00:13:29 +02002888 case HTTP_RULE_RES_CONT:
2889 case HTTP_RULE_RES_STOP: /* nothing to do */
2890 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002891
Willy Tarreau0b748332014-04-29 00:13:29 +02002892 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2893 if (txn->flags & TX_CLTARPIT)
2894 goto tarpit;
2895 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002896
Willy Tarreau0b748332014-04-29 00:13:29 +02002897 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2898 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002899
Willy Tarreau0b748332014-04-29 00:13:29 +02002900 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002901 goto done;
2902
Willy Tarreau0b748332014-04-29 00:13:29 +02002903 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2904 goto return_bad_req;
2905 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002906 }
2907
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002908 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2909 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002910 struct hdr_ctx ctx;
2911
2912 ctx.idx = 0;
2913 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002914 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002915 if (unlikely(http_header_add_tail2(&txn->req,
2916 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002917 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002918 goto return_bad_req;
2919 }
2920 }
2921
2922 }
2923
Willy Tarreau52542592014-04-28 18:33:26 +02002924 /* OK at this stage, we know that the request was accepted according to
2925 * the http-request rules, we can check for the stats. Note that the
2926 * URI is detected *before* the req* rules in order not to be affected
2927 * by a possible reqrep, while they are processed *after* so that a
2928 * reqdeny can still block them. This clearly needs to change in 1.6!
2929 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002930 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002931 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01002932 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002933 txn->status = 500;
2934 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002935 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002936
Willy Tarreaue7dff022015-04-03 01:14:29 +02002937 if (!(s->flags & SF_ERR_MASK))
2938 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002939 goto return_prx_cond;
2940 }
2941
2942 /* parse the whole stats request and extract the relevant information */
2943 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002944 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002945 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002946
Willy Tarreau0b748332014-04-29 00:13:29 +02002947 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2948 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002949
Willy Tarreau0b748332014-04-29 00:13:29 +02002950 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2951 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002952 }
2953
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002954 /* evaluate the req* rules except reqadd */
2955 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002956 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002957 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002958
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002959 if (txn->flags & TX_CLDENY)
2960 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002961
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002962 if (txn->flags & TX_CLTARPIT) {
2963 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002964 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002965 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002966 }
Willy Tarreau06619262006-12-17 08:37:22 +01002967
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002968 /* add request headers from the rule sets in the same order */
2969 list_for_each_entry(wl, &px->req_add, list) {
2970 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002971 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002972 ret = acl_pass(ret);
2973 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2974 ret = !ret;
2975 if (!ret)
2976 continue;
2977 }
2978
Christopher Faulet10079f52018-10-03 15:17:28 +02002979 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002980 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002981 }
2982
Willy Tarreau52542592014-04-28 18:33:26 +02002983
2984 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002985 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2986 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002987 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002988 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002989 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002990
Willy Tarreaue7dff022015-04-03 01:14:29 +02002991 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2992 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2993 if (!(s->flags & SF_FINST_MASK))
2994 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002995
Willy Tarreau70730dd2014-04-24 18:06:27 +02002996 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002997 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2998 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002999 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003000 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003001 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003002
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003003 /* check whether we have some ACLs set to redirect this request */
3004 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003005 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003006 int ret;
3007
Willy Tarreau192252e2015-04-04 01:47:55 +02003008 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003009 ret = acl_pass(ret);
3010 if (rule->cond->pol == ACL_COND_UNLESS)
3011 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003012 if (!ret)
3013 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003014 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003015 if (!http_apply_redirect_rule(rule, s, txn))
3016 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003017 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003018 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003019
Willy Tarreau2be39392010-01-03 17:24:51 +01003020 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3021 * If this happens, then the data will not come immediately, so we must
3022 * send all what we have without waiting. Note that due to the small gain
3023 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003024 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003025 * itself once used.
3026 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003027 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003028
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003029 done: /* done with this analyser, continue with next ones that the calling
3030 * points will have set, if any.
3031 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003032 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003033 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3034 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003035 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003036
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003037 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003038 /* Allow cookie logging
3039 */
3040 if (s->be->cookie_name || sess->fe->capture_name)
3041 manage_client_side_cookies(s, req);
3042
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003043 /* When a connection is tarpitted, we use the tarpit timeout,
3044 * which may be the same as the connect timeout if unspecified.
3045 * If unset, then set it to zero because we really want it to
3046 * eventually expire. We build the tarpit as an analyser.
3047 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003048 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003049
3050 /* wipe the request out so that we can drop the connection early
3051 * if the client closes first.
3052 */
3053 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003054
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003055 txn->status = http_err_codes[deny_status];
3056
Christopher Faulet0184ea72017-01-05 14:06:34 +01003057 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003058 req->analysers |= AN_REQ_HTTP_TARPIT;
3059 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3060 if (!req->analyse_exp)
3061 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003062 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003063 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003064 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003065 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003066 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003067 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003068 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003069
3070 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003071
3072 /* Allow cookie logging
3073 */
3074 if (s->be->cookie_name || sess->fe->capture_name)
3075 manage_client_side_cookies(s, req);
3076
Willy Tarreau0b748332014-04-29 00:13:29 +02003077 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003078 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003079 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003080 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003081 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003082 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003083 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003084 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003085 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003086 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003087 goto return_prx_cond;
3088
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003089 return_bad_req:
3090 /* We centralize bad requests processing here */
3091 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3092 /* we detected a parsing error. We want to archive this request
3093 * in the dedicated proxy area for later troubleshooting.
3094 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003095 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003096 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003097
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003098 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003099 txn->req.msg_state = HTTP_MSG_ERROR;
3100 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003101 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003102
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003103 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003104 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003105 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003106
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003107 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003108 if (!(s->flags & SF_ERR_MASK))
3109 s->flags |= SF_ERR_PRXCOND;
3110 if (!(s->flags & SF_FINST_MASK))
3111 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003112
Christopher Faulet0184ea72017-01-05 14:06:34 +01003113 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003114 req->analyse_exp = TICK_ETERNITY;
3115 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003116
3117 return_prx_yield:
3118 channel_dont_connect(req);
3119 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003120}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003121
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003122/* This function performs all the processing enabled for the current request.
3123 * It returns 1 if the processing can continue on next analysers, or zero if it
3124 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003125 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003126 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003127int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003128{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003129 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003130 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003131 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003132 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003133
Christopher Faulete0768eb2018-10-03 16:38:02 +02003134 if (IS_HTX_STRM(s))
3135 return htx_process_request(s, req, an_bit);
3136
Willy Tarreau655dce92009-11-08 13:10:58 +01003137 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003138 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003139 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003140 return 0;
3141 }
3142
Christopher Faulet45073512018-07-20 10:16:29 +02003143 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 +02003144 now_ms, __FUNCTION__,
3145 s,
3146 req,
3147 req->rex, req->wex,
3148 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003149 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003150 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003151
Willy Tarreau59234e92008-11-30 23:51:27 +01003152 /*
3153 * Right now, we know that we have processed the entire headers
3154 * and that unwanted requests have been filtered out. We can do
3155 * whatever we want with the remaining request. Also, now we
3156 * may have separate values for ->fe, ->be.
3157 */
Willy Tarreau06619262006-12-17 08:37:22 +01003158
Willy Tarreau59234e92008-11-30 23:51:27 +01003159 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003160 * If HTTP PROXY is set we simply get remote server address parsing
3161 * incoming request. Note that this requires that a connection is
3162 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003163 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003164 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003165 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003166 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003167
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003168 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003169 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003170 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003171 txn->req.msg_state = HTTP_MSG_ERROR;
3172 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003173 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003174 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003175
Willy Tarreaue7dff022015-04-03 01:14:29 +02003176 if (!(s->flags & SF_ERR_MASK))
3177 s->flags |= SF_ERR_RESOURCE;
3178 if (!(s->flags & SF_FINST_MASK))
3179 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003180
3181 return 0;
3182 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003183
Willy Tarreau6b952c82018-09-10 17:45:34 +02003184 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003185 if (url2sa(ci_head(req) + msg->sl.rq.u,
3186 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003187 &conn->addr.to, NULL) == -1)
3188 goto return_bad_req;
3189
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003190 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003191 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3192 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003193 * u_l characters by a single "/".
3194 */
3195 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003196 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003197 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3198 int delta;
3199
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003200 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003201 http_msg_move_end(&txn->req, delta);
3202 cur_end += delta;
3203 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3204 goto return_bad_req;
3205 }
3206 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003207 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003208 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3209 int delta;
3210
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003211 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003212 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003213 http_msg_move_end(&txn->req, delta);
3214 cur_end += delta;
3215 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3216 goto return_bad_req;
3217 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003218 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003219
Willy Tarreau59234e92008-11-30 23:51:27 +01003220 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003221 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003222 * Note that doing so might move headers in the request, but
3223 * the fields will stay coherent and the URI will not move.
3224 * This should only be performed in the backend.
3225 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003226 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003227 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003228
William Lallemanda73203e2012-03-12 12:48:57 +01003229 /* add unique-id if "header-unique-id" is specified */
3230
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003231 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003232 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003233 goto return_bad_req;
3234 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003235 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003236 }
William Lallemanda73203e2012-03-12 12:48:57 +01003237
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003238 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003239 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003240 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003241 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003242 goto return_bad_req;
3243 }
3244
Cyril Bontéb21570a2009-11-29 20:04:48 +01003245 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003246 * 9: add X-Forwarded-For if either the frontend or the backend
3247 * asks for it.
3248 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003249 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003250 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003251 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3252 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3253 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003254 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003255 /* The header is set to be added only if none is present
3256 * and we found it, so don't do anything.
3257 */
3258 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003259 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003260 /* Add an X-Forwarded-For header unless the source IP is
3261 * in the 'except' network range.
3262 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003263 if ((!sess->fe->except_mask.s_addr ||
3264 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3265 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003266 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003267 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003268 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003269 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003270 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003271 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003272
3273 /* Note: we rely on the backend to get the header name to be used for
3274 * x-forwarded-for, because the header is really meant for the backends.
3275 * However, if the backend did not specify any option, we have to rely
3276 * on the frontend's header name.
3277 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003278 if (s->be->fwdfor_hdr_len) {
3279 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003280 memcpy(trash.area,
3281 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003282 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003283 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003284 memcpy(trash.area,
3285 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003286 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003287 len += snprintf(trash.area + len,
3288 trash.size - len,
3289 ": %d.%d.%d.%d", pn[0], pn[1],
3290 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003291
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003292 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003293 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003294 }
3295 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003296 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003297 /* FIXME: for the sake of completeness, we should also support
3298 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003299 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003300 int len;
3301 char pn[INET6_ADDRSTRLEN];
3302 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003303 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003304 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003305
Willy Tarreau59234e92008-11-30 23:51:27 +01003306 /* Note: we rely on the backend to get the header name to be used for
3307 * x-forwarded-for, because the header is really meant for the backends.
3308 * However, if the backend did not specify any option, we have to rely
3309 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003310 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003311 if (s->be->fwdfor_hdr_len) {
3312 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003313 memcpy(trash.area, s->be->fwdfor_hdr_name,
3314 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003315 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003316 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003317 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3318 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003319 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003320 len += snprintf(trash.area + len, trash.size - len,
3321 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003322
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003323 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003324 goto return_bad_req;
3325 }
3326 }
3327
3328 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003329 * 10: add X-Original-To if either the frontend or the backend
3330 * asks for it.
3331 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003332 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003333
3334 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003335 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003336 /* Add an X-Original-To header unless the destination IP is
3337 * in the 'except' network range.
3338 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003339 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003340
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003341 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003342 ((!sess->fe->except_mask_to.s_addr ||
3343 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3344 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003345 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003346 (((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 +02003347 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003348 int len;
3349 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003350 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003351
3352 /* Note: we rely on the backend to get the header name to be used for
3353 * x-original-to, because the header is really meant for the backends.
3354 * However, if the backend did not specify any option, we have to rely
3355 * on the frontend's header name.
3356 */
3357 if (s->be->orgto_hdr_len) {
3358 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003359 memcpy(trash.area,
3360 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003361 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003362 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003363 memcpy(trash.area,
3364 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003365 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003366 len += snprintf(trash.area + len,
3367 trash.size - len,
3368 ": %d.%d.%d.%d", pn[0], pn[1],
3369 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003370
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003371 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003372 goto return_bad_req;
3373 }
3374 }
3375 }
3376
Willy Tarreau50fc7772012-11-11 22:19:57 +01003377 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3378 * If an "Upgrade" token is found, the header is left untouched in order not to have
3379 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3380 * "Upgrade" is present in the Connection header.
3381 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003382 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003383 unsigned int want_flags = 0;
3384
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003385 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003386 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003387 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003388 want_flags |= TX_CON_CLO_SET;
3389 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003390 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
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_KAL_SET;
3393 }
3394
3395 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003396 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003397 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003398
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003399
Willy Tarreau522d6c02009-12-06 18:49:18 +01003400 /* If we have no server assigned yet and we're balancing on url_param
3401 * with a POST request, we may be interested in checking the body for
3402 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003403 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003404 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003405 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003406 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003407 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003408 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003409 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003410
Christopher Fauletbe821b92017-03-30 11:21:53 +02003411 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3412 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003413#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003414 /* We expect some data from the client. Unless we know for sure
3415 * we already have a full request, we have to re-enable quick-ack
3416 * in case we previously disabled it, otherwise we might cause
3417 * the client to delay further data.
3418 */
3419 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3420 cli_conn && conn_ctrl_ready(cli_conn) &&
3421 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003422 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003423 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003424#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003425
Willy Tarreau59234e92008-11-30 23:51:27 +01003426 /*************************************************************
3427 * OK, that's finished for the headers. We have done what we *
3428 * could. Let's switch to the DATA state. *
3429 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003430 req->analyse_exp = TICK_ETERNITY;
3431 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003432
Willy Tarreau59234e92008-11-30 23:51:27 +01003433 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003434 /* OK let's go on with the BODY now */
3435 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003436
Willy Tarreau59234e92008-11-30 23:51:27 +01003437 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003438 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003439 /* we detected a parsing error. We want to archive this request
3440 * in the dedicated proxy area for later troubleshooting.
3441 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003442 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003443 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003444
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003445 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003446 txn->req.msg_state = HTTP_MSG_ERROR;
3447 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003448 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003449 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003450
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003451 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003452 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003453 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003454
Willy Tarreaue7dff022015-04-03 01:14:29 +02003455 if (!(s->flags & SF_ERR_MASK))
3456 s->flags |= SF_ERR_PRXCOND;
3457 if (!(s->flags & SF_FINST_MASK))
3458 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003459 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003460}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003461
Willy Tarreau60b85b02008-11-30 23:28:40 +01003462/* This function is an analyser which processes the HTTP tarpit. It always
3463 * returns zero, at the beginning because it prevents any other processing
3464 * from occurring, and at the end because it terminates the request.
3465 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003466int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003467{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003468 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003469
Christopher Faulete0768eb2018-10-03 16:38:02 +02003470 if (IS_HTX_STRM(s))
3471 return htx_process_tarpit(s, req, an_bit);
3472
Willy Tarreau60b85b02008-11-30 23:28:40 +01003473 /* This connection is being tarpitted. The CLIENT side has
3474 * already set the connect expiration date to the right
3475 * timeout. We just have to check that the client is still
3476 * there and that the timeout has not expired.
3477 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003478 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003479 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003480 !tick_is_expired(req->analyse_exp, now_ms))
3481 return 0;
3482
3483 /* We will set the queue timer to the time spent, just for
3484 * logging purposes. We fake a 500 server error, so that the
3485 * attacker will not suspect his connection has been tarpitted.
3486 * It will not cause trouble to the logs because we can exclude
3487 * the tarpitted connections by filtering on the 'PT' status flags.
3488 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003489 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3490
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003491 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003492 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003493
Christopher Faulet0184ea72017-01-05 14:06:34 +01003494 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003495 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003496
Willy Tarreaue7dff022015-04-03 01:14:29 +02003497 if (!(s->flags & SF_ERR_MASK))
3498 s->flags |= SF_ERR_PRXCOND;
3499 if (!(s->flags & SF_FINST_MASK))
3500 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003501 return 0;
3502}
3503
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003504/* This function is an analyser which waits for the HTTP request body. It waits
3505 * for either the buffer to be full, or the full advertised contents to have
3506 * reached the buffer. It must only be called after the standard HTTP request
3507 * processing has occurred, because it expects the request to be parsed and will
3508 * look for the Expect header. It may send a 100-Continue interim response. It
3509 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3510 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3511 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003512 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003513int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003514{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003515 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003516 struct http_txn *txn = s->txn;
3517 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003518
Christopher Faulete0768eb2018-10-03 16:38:02 +02003519 if (IS_HTX_STRM(s))
3520 return htx_wait_for_request_body(s, req, an_bit);
3521
Willy Tarreaud34af782008-11-30 23:36:37 +01003522 /* We have to parse the HTTP request body to find any required data.
3523 * "balance url_param check_post" should have been the only way to get
3524 * into this. We were brought here after HTTP header analysis, so all
3525 * related structures are ready.
3526 */
3527
Willy Tarreau890988f2014-04-10 11:59:33 +02003528 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3529 /* This is the first call */
3530 if (msg->msg_state < HTTP_MSG_BODY)
3531 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003532
Willy Tarreau890988f2014-04-10 11:59:33 +02003533 if (msg->msg_state < HTTP_MSG_100_SENT) {
3534 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3535 * send an HTTP/1.1 100 Continue intermediate response.
3536 */
3537 if (msg->flags & HTTP_MSGF_VER_11) {
3538 struct hdr_ctx ctx;
3539 ctx.idx = 0;
3540 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003541 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003542 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003543 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003544 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003545 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003546 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003547 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003548 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003549
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003550 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003551 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003552 * must save the body in msg->next because it survives buffer
3553 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003554 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003555 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003556
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003557 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003558 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3559 else
3560 msg->msg_state = HTTP_MSG_DATA;
3561 }
3562
Willy Tarreau890988f2014-04-10 11:59:33 +02003563 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3564 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003565 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003566 goto missing_data;
3567
3568 /* OK we have everything we need now */
3569 goto http_end;
3570 }
3571
3572 /* OK here we're parsing a chunked-encoded message */
3573
Willy Tarreau522d6c02009-12-06 18:49:18 +01003574 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003575 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003576 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003577 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003578 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003579 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003580 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003581
Willy Tarreau115acb92009-12-26 13:56:06 +01003582 if (!ret)
3583 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003584 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003585 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003586 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003587 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003588 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003589 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003590 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003591
3592 msg->chunk_len = chunk;
3593 msg->body_len += chunk;
3594
3595 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003596 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003597 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003598 }
3599
Willy Tarreaud98cf932009-12-27 22:54:55 +01003600 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003601 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3602 * for at least a whole chunk or the whole content length bytes after
3603 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003604 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003605 if (msg->msg_state == HTTP_MSG_TRAILERS)
3606 goto http_end;
3607
Willy Tarreaue115b492015-05-01 23:05:14 +02003608 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003609 goto http_end;
3610
3611 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003612 /* we get here if we need to wait for more data. If the buffer is full,
3613 * we have the maximum we can expect.
3614 */
Willy Tarreau23752332018-06-15 14:54:53 +02003615 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003616 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003617
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003618 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003619 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003620 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003621
Willy Tarreaue7dff022015-04-03 01:14:29 +02003622 if (!(s->flags & SF_ERR_MASK))
3623 s->flags |= SF_ERR_CLITO;
3624 if (!(s->flags & SF_FINST_MASK))
3625 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003626 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003627 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003628
3629 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003630 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003631 /* Not enough data. We'll re-use the http-request
3632 * timeout here. Ideally, we should set the timeout
3633 * relative to the accept() date. We just set the
3634 * request timeout once at the beginning of the
3635 * request.
3636 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003637 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003638 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003639 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003640 return 0;
3641 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003642
3643 http_end:
3644 /* The situation will not evolve, so let's give up on the analysis. */
3645 s->logs.tv_request = now; /* update the request timer to reflect full request */
3646 req->analysers &= ~an_bit;
3647 req->analyse_exp = TICK_ETERNITY;
3648 return 1;
3649
3650 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003651 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003652 txn->req.msg_state = HTTP_MSG_ERROR;
3653 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003654 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003655
Willy Tarreaue7dff022015-04-03 01:14:29 +02003656 if (!(s->flags & SF_ERR_MASK))
3657 s->flags |= SF_ERR_PRXCOND;
3658 if (!(s->flags & SF_FINST_MASK))
3659 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003660
Willy Tarreau522d6c02009-12-06 18:49:18 +01003661 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003662 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003663 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003664 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003665 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003666 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003667}
3668
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003669/* send a server's name with an outgoing request over an established connection.
3670 * Note: this function is designed to be called once the request has been scheduled
3671 * for being forwarded. This is the reason why it rewinds the buffer before
3672 * proceeding.
3673 */
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003674int http_send_name_header(struct stream *s, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003675
3676 struct hdr_ctx ctx;
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003677 struct http_txn *txn = s->txn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003678 char *hdr_name = be->server_id_hdr_name;
3679 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003680 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003681 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003682 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003683
Christopher Faulet64159df2018-10-24 21:15:35 +02003684 if (IS_HTX_STRM(s))
3685 return htx_send_name_header(s, be, srv_name);
William Lallemandd9e90662012-01-30 17:27:17 +01003686 ctx.idx = 0;
3687
Willy Tarreau211cdec2014-04-17 20:18:08 +02003688 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003689 if (old_o) {
3690 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003691 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003692 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003693 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003694 }
3695
Willy Tarreauf37954d2018-06-15 18:31:02 +02003696 old_i = ci_data(chn);
3697 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 -05003698 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003699 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003700 }
3701
3702 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003703 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003704 memcpy(hdr_val, hdr_name, hdr_name_len);
3705 hdr_val += hdr_name_len;
3706 *hdr_val++ = ':';
3707 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003708 hdr_val += strlcpy2(hdr_val, srv_name,
3709 trash.area + trash.size - hdr_val);
3710 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3711 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003712
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003713 if (old_o) {
3714 /* If this was a forwarded request, we must readjust the amount of
3715 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003716 * variations. Note that the current state is >= HTTP_MSG_BODY,
3717 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003718 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003719 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003720 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003721 txn->req.next -= old_o;
3722 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003723 }
3724
Mark Lamourinec2247f02012-01-04 13:02:01 -05003725 return 0;
3726}
3727
Willy Tarreau610ecce2010-01-04 21:15:02 +01003728/* Terminate current transaction and prepare a new one. This is very tricky
3729 * right now but it works.
3730 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003731void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003732{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003733 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003734 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003735 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003736 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003737 struct connection *srv_conn;
3738 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003739 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003740
Willy Tarreau610ecce2010-01-04 21:15:02 +01003741 /* FIXME: We need a more portable way of releasing a backend's and a
3742 * server's connections. We need a safer way to reinitialize buffer
3743 * flags. We also need a more accurate method for computing per-request
3744 * data.
3745 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003746 cs = objt_cs(s->si[1].end);
3747 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003748
Willy Tarreau4213a112013-12-15 10:25:42 +01003749 /* unless we're doing keep-alive, we want to quickly close the connection
3750 * to the server.
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003751 * XXX cognet: If the connection doesn't have a owner then it may not
3752 * be referenced anywhere, just kill it now, even if it could be reused.
3753 * To be revisited later when revisited later when we handle connection
3754 * pools properly.
Willy Tarreau4213a112013-12-15 10:25:42 +01003755 */
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;
Christopher Faulet29f77e82017-06-08 14:04:45 +02003831 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 Houchard7c6f8b12018-11-13 16:48:36 +01003845 !si_conn_ready(&s->si[1]) || !srv_conn->owner)
Willy Tarreau323a2d92015-08-04 19:00:17 +02003846 srv_conn = NULL;
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003847 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003848
Willy Tarreau350f4872014-11-28 14:42:25 +01003849 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3850 s->si[1].err_type = SI_ET_NONE;
3851 s->si[1].conn_retries = 0; /* used for logging too */
3852 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003853 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 +01003854 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 +02003855 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 +02003856 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3857 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3858 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003859
Patrick Hemmere3faf022018-08-22 10:02:00 -04003860 hlua_ctx_destroy(s->hlua);
3861 s->hlua = NULL;
3862
Willy Tarreaueee5b512015-04-03 23:46:31 +02003863 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003864 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003865 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003866
3867 if (prev_status == 401 || prev_status == 407) {
3868 /* In HTTP keep-alive mode, if we receive a 401, we still have
3869 * a chance of being able to send the visitor again to the same
3870 * server over the same connection. This is required by some
3871 * broken protocols such as NTLM, and anyway whenever there is
3872 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003873 * it's better to do it (at least it helps with debugging), at
3874 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003875 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003876 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003877 }
3878
Willy Tarreau53f96852016-02-02 18:50:47 +01003879 /* Never ever allow to reuse a connection from a non-reuse backend */
3880 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3881 srv_conn->flags |= CO_FL_PRIVATE;
3882
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003883 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003884 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003885
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003886 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003887 s->req.flags |= CF_NEVER_WAIT;
3888 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003889 }
3890
Willy Tarreau714ea782015-11-25 20:11:11 +01003891 /* we're removing the analysers, we MUST re-enable events detection.
3892 * We don't enable close on the response channel since it's either
3893 * already closed, or in keep-alive with an idle connection handler.
3894 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003895 channel_auto_read(&s->req);
3896 channel_auto_close(&s->req);
3897 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003898
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003899 /* we're in keep-alive with an idle connection, monitor it if not already done */
3900 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003901 srv = objt_server(srv_conn->target);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003902 if (srv) {
3903 if (srv_conn->flags & CO_FL_PRIVATE)
3904 LIST_ADD(&srv->priv_conns[tid], &srv_conn->list);
3905 else if (prev_flags & TX_NOT_FIRST)
3906 /* note: we check the request, not the connection, but
3907 * this is valid for strategies SAFE and AGGR, and in
3908 * case of ALWS, we don't care anyway.
3909 */
3910 LIST_ADD(&srv->safe_conns[tid], &srv_conn->list);
3911 else
3912 LIST_ADD(&srv->idle_conns[tid], &srv_conn->list);
3913 }
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003914 }
Christopher Faulete6006242017-03-10 11:52:44 +01003915 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3916 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003917}
3918
3919
3920/* This function updates the request state machine according to the response
3921 * state machine and buffer flags. It returns 1 if it changes anything (flag
3922 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3923 * it is only used to find when a request/response couple is complete. Both
3924 * this function and its equivalent should loop until both return zero. It
3925 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3926 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003927int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003928{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003929 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003930 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003931 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003932 unsigned int old_state = txn->req.msg_state;
3933
Christopher Faulet4be98032017-07-18 10:48:24 +02003934 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003935 return 0;
3936
3937 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003938 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003939 * We can shut the read side unless we want to abort_on_close,
3940 * or we have a POST request. The issue with POST requests is
3941 * that some browsers still send a CRLF after the request, and
3942 * this CRLF must be read so that it does not remain in the kernel
3943 * buffers, otherwise a close could cause an RST on some systems
3944 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003945 * Note that if we're using keep-alive on the client side, we'd
3946 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003947 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003948 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003949 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003950 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3951 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003952 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3953 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003954 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003955 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003956
Willy Tarreau40f151a2012-12-20 12:10:09 +01003957 /* if the server closes the connection, we want to immediately react
3958 * and close the socket to save packets and syscalls.
3959 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003960 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003961
Willy Tarreau7f876a12015-11-18 11:59:55 +01003962 /* In any case we've finished parsing the request so we must
3963 * disable Nagle when sending data because 1) we're not going
3964 * to shut this side, and 2) the server is waiting for us to
3965 * send pending data.
3966 */
3967 chn->flags |= CF_NEVER_WAIT;
3968
Willy Tarreau610ecce2010-01-04 21:15:02 +01003969 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3970 goto wait_other_side;
3971
3972 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3973 /* The server has not finished to respond, so we
3974 * don't want to move in order not to upset it.
3975 */
3976 goto wait_other_side;
3977 }
3978
Willy Tarreau610ecce2010-01-04 21:15:02 +01003979 /* When we get here, it means that both the request and the
3980 * response have finished receiving. Depending on the connection
3981 * mode, we'll have to wait for the last bytes to leave in either
3982 * direction, and sometimes for a close to be effective.
3983 */
3984
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003985 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3986 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003987 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3988 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003989 }
3990 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3991 /* Option forceclose is set, or either side wants to close,
3992 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003993 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003994 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003995 *
3996 * However, there is an exception if the response
3997 * length is undefined. In this case, we need to wait
3998 * the close from the server. The response will be
3999 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004000 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004001 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4002 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4003 goto check_channel_flags;
4004
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004005 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4006 channel_shutr_now(chn);
4007 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004008 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004009 }
4010 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004011 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4012 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004013 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004014 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4015 channel_auto_read(chn);
4016 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004017 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004018 }
4019
Christopher Faulet4be98032017-07-18 10:48:24 +02004020 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004021 }
4022
4023 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4024 http_msg_closing:
4025 /* nothing else to forward, just waiting for the output buffer
4026 * to be empty and for the shutw_now to take effect.
4027 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004028 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004029 txn->req.msg_state = HTTP_MSG_CLOSED;
4030 goto http_msg_closed;
4031 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004032 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004033 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004034 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004035 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004036 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004037 }
4038
4039 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4040 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004041 /* if we don't know whether the server will close, we need to hard close */
4042 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4043 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4044
Willy Tarreau3988d932013-12-27 23:03:08 +01004045 /* see above in MSG_DONE why we only do this in these states */
4046 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4047 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004048 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4049 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004050 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004051 goto wait_other_side;
4052 }
4053
Christopher Faulet4be98032017-07-18 10:48:24 +02004054 check_channel_flags:
4055 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4056 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4057 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004058 txn->req.msg_state = HTTP_MSG_CLOSING;
4059 goto http_msg_closing;
4060 }
4061
4062
Willy Tarreau610ecce2010-01-04 21:15:02 +01004063 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004064 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004065}
4066
4067
4068/* This function updates the response state machine according to the request
4069 * state machine and buffer flags. It returns 1 if it changes anything (flag
4070 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4071 * it is only used to find when a request/response couple is complete. Both
4072 * this function and its equivalent should loop until both return zero. It
4073 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4074 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004075int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004076{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004077 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004078 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004079 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004080 unsigned int old_state = txn->rsp.msg_state;
4081
Christopher Faulet4be98032017-07-18 10:48:24 +02004082 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004083 return 0;
4084
4085 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4086 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004087 * still monitor the server connection for a possible close
4088 * while the request is being uploaded, so we don't disable
4089 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004090 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004091 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004092
4093 if (txn->req.msg_state == HTTP_MSG_ERROR)
4094 goto wait_other_side;
4095
4096 if (txn->req.msg_state < HTTP_MSG_DONE) {
4097 /* The client seems to still be sending data, probably
4098 * because we got an error response during an upload.
4099 * We have the choice of either breaking the connection
4100 * or letting it pass through. Let's do the later.
4101 */
4102 goto wait_other_side;
4103 }
4104
Willy Tarreau610ecce2010-01-04 21:15:02 +01004105 /* When we get here, it means that both the request and the
4106 * response have finished receiving. Depending on the connection
4107 * mode, we'll have to wait for the last bytes to leave in either
4108 * direction, and sometimes for a close to be effective.
4109 */
4110
4111 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4112 /* Server-close mode : shut read and wait for the request
4113 * side to close its output buffer. The caller will detect
4114 * when we're in DONE and the other is in CLOSED and will
4115 * catch that for the final cleanup.
4116 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004117 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4118 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004119 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004120 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4121 /* Option forceclose is set, or either side wants to close,
4122 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004123 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004124 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004125 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004126 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004127 channel_shutr_now(chn);
4128 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004129 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004130 }
4131 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004132 /* The last possible modes are keep-alive and tunnel. Tunnel will
4133 * need to forward remaining data. Keep-alive will need to monitor
4134 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004135 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004136 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004137 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004138 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4139 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004140 }
4141
Christopher Faulet4be98032017-07-18 10:48:24 +02004142 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004143 }
4144
4145 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4146 http_msg_closing:
4147 /* nothing else to forward, just waiting for the output buffer
4148 * to be empty and for the shutw_now to take effect.
4149 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004150 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004151 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4152 goto http_msg_closed;
4153 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004154 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004155 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004156 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004157 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004158 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004159 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004160 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004161 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004162 }
4163
4164 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4165 http_msg_closed:
4166 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004167 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004168 channel_auto_close(chn);
4169 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004170 goto wait_other_side;
4171 }
4172
Christopher Faulet4be98032017-07-18 10:48:24 +02004173 check_channel_flags:
4174 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4175 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4176 /* if we've just closed an output, let's switch */
4177 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4178 goto http_msg_closing;
4179 }
4180
Willy Tarreau610ecce2010-01-04 21:15:02 +01004181 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004182 /* We force the response to leave immediately if we're waiting for the
4183 * other side, since there is no pending shutdown to push it out.
4184 */
4185 if (!channel_is_empty(chn))
4186 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004187 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004188}
4189
4190
Christopher Faulet894da4c2017-07-18 11:29:07 +02004191/* Resync the request and response state machines. */
4192void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004193{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004194 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004195#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004196 int old_req_state = txn->req.msg_state;
4197 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004198#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004199
Willy Tarreau610ecce2010-01-04 21:15:02 +01004200 http_sync_req_state(s);
4201 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004202 if (!http_sync_res_state(s))
4203 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004204 if (!http_sync_req_state(s))
4205 break;
4206 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004207
Christopher Faulet894da4c2017-07-18 11:29:07 +02004208 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4209 "req->analysers=0x%08x res->analysers=0x%08x\n",
4210 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004211 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4212 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004213 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004214
4215
Willy Tarreau610ecce2010-01-04 21:15:02 +01004216 /* OK, both state machines agree on a compatible state.
4217 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004218 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4219 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004220 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4221 * means we must abort the request.
4222 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4223 * corresponding channel.
4224 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4225 * on the response with server-close mode means we've completed one
4226 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004227 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004228 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4229 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004230 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004231 channel_auto_close(&s->req);
4232 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004233 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004234 channel_auto_close(&s->res);
4235 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004236 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004237 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4238 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004239 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004240 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004241 channel_auto_close(&s->res);
4242 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004243 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004244 channel_abort(&s->req);
4245 channel_auto_close(&s->req);
4246 channel_auto_read(&s->req);
4247 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004248 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004249 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4250 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4251 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4252 s->req.analysers &= AN_REQ_FLT_END;
4253 if (HAS_REQ_DATA_FILTERS(s))
4254 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4255 }
4256 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4257 s->res.analysers &= AN_RES_FLT_END;
4258 if (HAS_RSP_DATA_FILTERS(s))
4259 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4260 }
4261 channel_auto_close(&s->req);
4262 channel_auto_read(&s->req);
4263 channel_auto_close(&s->res);
4264 channel_auto_read(&s->res);
4265 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004266 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4267 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004268 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004269 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4270 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4271 /* server-close/keep-alive: terminate this transaction,
4272 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004273 * a fresh-new transaction, but only once we're sure there's
4274 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004275 * another request. They must not hold any pending output data
4276 * and the response buffer must realigned
4277 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004278 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004279 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004280 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004281 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004282 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004283 else {
4284 s->req.analysers = AN_REQ_FLT_END;
4285 s->res.analysers = AN_RES_FLT_END;
4286 txn->flags |= TX_WAIT_CLEANUP;
4287 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004288 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004289}
4290
Willy Tarreaud98cf932009-12-27 22:54:55 +01004291/* This function is an analyser which forwards request body (including chunk
4292 * sizes if any). It is called as soon as we must forward, even if we forward
4293 * zero byte. The only situation where it must not be called is when we're in
4294 * tunnel mode and we want to forward till the close. It's used both to forward
4295 * remaining data and to resync after end of body. It expects the msg_state to
4296 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004297 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004298 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004299 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004300 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004301int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004302{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004303 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004304 struct http_txn *txn = s->txn;
4305 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004306 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004307
Christopher Faulete0768eb2018-10-03 16:38:02 +02004308 if (IS_HTX_STRM(s))
4309 return htx_request_forward_body(s, req, an_bit);
4310
Christopher Faulet45073512018-07-20 10:16:29 +02004311 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 +02004312 now_ms, __FUNCTION__,
4313 s,
4314 req,
4315 req->rex, req->wex,
4316 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004317 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004318 req->analysers);
4319
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004320 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4321 return 0;
4322
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004323 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004324 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004325 /* Output closed while we were sending data. We must abort and
4326 * wake the other side up.
4327 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004328 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004329 msg->msg_state = HTTP_MSG_ERROR;
4330 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004331 return 1;
4332 }
4333
Willy Tarreaud98cf932009-12-27 22:54:55 +01004334 /* Note that we don't have to send 100-continue back because we don't
4335 * need the data to complete our job, and it's up to the server to
4336 * decide whether to return 100, 417 or anything else in return of
4337 * an "Expect: 100-continue" header.
4338 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004339 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004340 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4341 ? HTTP_MSG_CHUNK_SIZE
4342 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004343
4344 /* TODO/filters: when http-buffer-request option is set or if a
4345 * rule on url_param exists, the first chunk size could be
4346 * already parsed. In that case, msg->next is after the chunk
4347 * size (including the CRLF after the size). So this case should
4348 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004349 }
4350
Willy Tarreau7ba23542014-04-17 21:50:00 +02004351 /* Some post-connect processing might want us to refrain from starting to
4352 * forward data. Currently, the only reason for this is "balance url_param"
4353 * whichs need to parse/process the request after we've enabled forwarding.
4354 */
4355 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004356 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004357 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004358 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004359 channel_dont_close(req); /* don't fail on early shutr */
4360 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004361 }
4362 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4363 }
4364
Willy Tarreau80a92c02014-03-12 10:41:13 +01004365 /* in most states, we should abort in case of early close */
4366 channel_auto_close(req);
4367
Willy Tarreauefdf0942014-04-24 20:08:57 +02004368 if (req->to_forward) {
4369 /* We can't process the buffer's contents yet */
4370 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004371 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004372 }
4373
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004374 if (msg->msg_state < HTTP_MSG_DONE) {
4375 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4376 ? http_msg_forward_chunked_body(s, msg)
4377 : http_msg_forward_body(s, msg));
4378 if (!ret)
4379 goto missing_data_or_waiting;
4380 if (ret < 0)
4381 goto return_bad_req;
4382 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004383
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004384 /* other states, DONE...TUNNEL */
4385 /* we don't want to forward closes on DONE except in tunnel mode. */
4386 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4387 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004388
Christopher Faulet894da4c2017-07-18 11:29:07 +02004389 http_resync_states(s);
4390 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004391 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4392 if (req->flags & CF_SHUTW) {
4393 /* request errors are most likely due to the
4394 * server aborting the transfer. */
4395 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004396 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004397 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004398 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004399 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004400 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004401 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004402 }
4403
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004404 /* If "option abortonclose" is set on the backend, we want to monitor
4405 * the client's connection and forward any shutdown notification to the
4406 * server, which will decide whether to close or to go on processing the
4407 * request. We only do that in tunnel mode, and not in other modes since
4408 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004409 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004410 channel_auto_read(req);
4411 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4412 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4413 s->si[1].flags |= SI_FL_NOLINGER;
4414 channel_auto_close(req);
4415 }
4416 else if (s->txn->meth == HTTP_METH_POST) {
4417 /* POST requests may require to read extra CRLF sent by broken
4418 * browsers and which could cause an RST to be sent upon close
4419 * on some systems (eg: Linux). */
4420 channel_auto_read(req);
4421 }
4422 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004423
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004424 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004425 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004426 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004427 if (!(s->flags & SF_ERR_MASK))
4428 s->flags |= SF_ERR_CLICL;
4429 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004430 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004431 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004432 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004433 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004434 }
4435
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004436 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4437 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004438 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004439 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004440
4441 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004442 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004443
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004444 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004445 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004446 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004447 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004448
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004449 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004450 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004451 * And when content-length is used, we never want to let the possible
4452 * shutdown be forwarded to the other side, as the state machine will
4453 * take care of it once the client responds. It's also important to
4454 * prevent TIME_WAITs from accumulating on the backend side, and for
4455 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004456 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004457 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004458 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004459
Willy Tarreau5c620922011-05-11 19:56:11 +02004460 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004461 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004462 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004463 * modes are already handled by the stream sock layer. We must not do
4464 * this in content-length mode because it could present the MSG_MORE
4465 * flag with the last block of forwarded data, which would cause an
4466 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004467 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004468 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004469 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004470
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004471 return 0;
4472
Willy Tarreaud98cf932009-12-27 22:54:55 +01004473 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004474 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004475 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004476 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004477
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004478 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004479 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004480 txn->req.msg_state = HTTP_MSG_ERROR;
4481 if (txn->status) {
4482 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004483 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004484 } else {
4485 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004486 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004487 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004488 req->analysers &= AN_REQ_FLT_END;
4489 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 +01004490
Willy Tarreaue7dff022015-04-03 01:14:29 +02004491 if (!(s->flags & SF_ERR_MASK))
4492 s->flags |= SF_ERR_PRXCOND;
4493 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004494 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004495 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004496 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004497 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004498 }
4499 return 0;
4500
4501 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004502 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004503 txn->req.msg_state = HTTP_MSG_ERROR;
4504 if (txn->status) {
4505 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004506 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004507 } else {
4508 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004509 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004510 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004511 req->analysers &= AN_REQ_FLT_END;
4512 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 +01004513
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004514 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4515 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004516 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004517 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004518
Willy Tarreaue7dff022015-04-03 01:14:29 +02004519 if (!(s->flags & SF_ERR_MASK))
4520 s->flags |= SF_ERR_SRVCL;
4521 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004522 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004523 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004524 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004525 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004526 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004527 return 0;
4528}
4529
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004530/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4531 * processing can continue on next analysers, or zero if it either needs more
4532 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004533 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004534 * when it has nothing left to do, and may remove any analyser when it wants to
4535 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004536 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004537int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004538{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004539 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004540 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004541 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004542 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004543 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004544 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004545 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004546 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004547
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004548 srv_conn = cs_conn(objt_cs(s->si[1].end));
4549
Christopher Faulete0768eb2018-10-03 16:38:02 +02004550 if (IS_HTX_STRM(s))
4551 return htx_wait_for_response(s, rep, an_bit);
4552
Christopher Faulet45073512018-07-20 10:16:29 +02004553 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 +02004554 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004555 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004556 rep,
4557 rep->rex, rep->wex,
4558 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004559 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004560 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004561
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004562 /*
4563 * Now parse the partial (or complete) lines.
4564 * We will check the response syntax, and also join multi-line
4565 * headers. An index of all the lines will be elaborated while
4566 * parsing.
4567 *
4568 * For the parsing, we use a 28 states FSM.
4569 *
4570 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004571 * ci_head(rep) = beginning of response
4572 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4573 * ci_tail(rep) = end of input data
4574 * msg->eol = end of current header or line (LF or CRLF)
4575 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004576 */
4577
Willy Tarreau628c40c2014-04-24 19:11:26 +02004578 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004579 /* There's a protected area at the end of the buffer for rewriting
4580 * purposes. We don't want to start to parse the request if the
4581 * protected area is affected, because we may have to move processed
4582 * data later, which is much more complicated.
4583 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004584 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004585 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004586 /* some data has still not left the buffer, wake us once that's done */
4587 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4588 goto abort_response;
4589 channel_dont_close(rep);
4590 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004591 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004592 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004593 }
4594
Willy Tarreau188e2302018-06-15 11:11:53 +02004595 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004596 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004597 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004598
Willy Tarreauf37954d2018-06-15 18:31:02 +02004599 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004600 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004601 }
4602
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004603 /* 1: we might have to print this header in debug mode */
4604 if (unlikely((global.mode & MODE_DEBUG) &&
4605 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004606 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004607 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004608
Willy Tarreauf37954d2018-06-15 18:31:02 +02004609 sol = ci_head(rep);
4610 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004611 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004612
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004613 sol += hdr_idx_first_pos(&txn->hdr_idx);
4614 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004615
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004616 while (cur_idx) {
4617 eol = sol + txn->hdr_idx.v[cur_idx].len;
4618 debug_hdr("srvhdr", s, sol, eol);
4619 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4620 cur_idx = txn->hdr_idx.v[cur_idx].next;
4621 }
4622 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004623
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004624 /*
4625 * Now we quickly check if we have found a full valid response.
4626 * If not so, we check the FD and buffer states before leaving.
4627 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004628 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004629 * responses are checked first.
4630 *
4631 * Depending on whether the client is still there or not, we
4632 * may send an error response back or not. Note that normally
4633 * we should only check for HTTP status there, and check I/O
4634 * errors somewhere else.
4635 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004636
Willy Tarreau655dce92009-11-08 13:10:58 +01004637 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004638 /* Invalid response */
4639 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4640 /* we detected a parsing error. We want to archive this response
4641 * in the dedicated proxy area for later troubleshooting.
4642 */
4643 hdr_response_bad:
4644 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004645 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004646
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004647 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004648 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004649 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004650 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004651 }
Willy Tarreau64648412010-03-05 10:41:54 +01004652 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004653 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004654 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004655 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004656 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004657 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004658 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004659
Willy Tarreaue7dff022015-04-03 01:14:29 +02004660 if (!(s->flags & SF_ERR_MASK))
4661 s->flags |= SF_ERR_PRXCOND;
4662 if (!(s->flags & SF_FINST_MASK))
4663 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004664
4665 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004666 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004667
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004668 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004669 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004670 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004671 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004672 goto hdr_response_bad;
4673 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004674
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004675 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004676 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004677 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004678 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004679 else if (txn->flags & TX_NOT_FIRST)
4680 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004681
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004682 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004683 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004684 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004685 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004686 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004687
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004688 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004689 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004690 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004691
4692 /* Check to see if the server refused the early data.
4693 * If so, just send a 425
4694 */
4695 if (objt_cs(s->si[1].end)) {
4696 struct connection *conn = objt_cs(s->si[1].end)->conn;
4697
4698 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4699 txn->status = 425;
4700 }
4701
Willy Tarreau350f4872014-11-28 14:42:25 +01004702 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004703 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004704 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004705
Willy Tarreaue7dff022015-04-03 01:14:29 +02004706 if (!(s->flags & SF_ERR_MASK))
4707 s->flags |= SF_ERR_SRVCL;
4708 if (!(s->flags & SF_FINST_MASK))
4709 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004710 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004711 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004712
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004713 /* read timeout : return a 504 to the client. */
4714 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004715 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004716 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004717
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004718 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004719 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004720 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004721 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004722 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004723
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004724 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004725 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004726 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004727 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004728 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004729 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004730
Willy Tarreaue7dff022015-04-03 01:14:29 +02004731 if (!(s->flags & SF_ERR_MASK))
4732 s->flags |= SF_ERR_SRVTO;
4733 if (!(s->flags & SF_FINST_MASK))
4734 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004735 return 0;
4736 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004737
Willy Tarreauf003d372012-11-26 13:35:37 +01004738 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004739 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004740 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4741 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004742 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004743 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004744
Christopher Faulet0184ea72017-01-05 14:06:34 +01004745 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004746 channel_auto_close(rep);
4747
4748 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004749 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004750 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004751
Willy Tarreaue7dff022015-04-03 01:14:29 +02004752 if (!(s->flags & SF_ERR_MASK))
4753 s->flags |= SF_ERR_CLICL;
4754 if (!(s->flags & SF_FINST_MASK))
4755 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004756
Willy Tarreau87b09662015-04-03 00:22:06 +02004757 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004758 return 0;
4759 }
4760
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004761 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004762 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004763 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004764 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004765 else if (txn->flags & TX_NOT_FIRST)
4766 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004767
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004768 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004769 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004770 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004771 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004772 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004773
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004774 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004775 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004776 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004777 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004778 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004779 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004780
Willy Tarreaue7dff022015-04-03 01:14:29 +02004781 if (!(s->flags & SF_ERR_MASK))
4782 s->flags |= SF_ERR_SRVCL;
4783 if (!(s->flags & SF_FINST_MASK))
4784 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004785 return 0;
4786 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004787
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004788 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004789 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004790 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004791 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004792 else if (txn->flags & TX_NOT_FIRST)
4793 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004794
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004795 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004796 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004797 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004798
Willy Tarreaue7dff022015-04-03 01:14:29 +02004799 if (!(s->flags & SF_ERR_MASK))
4800 s->flags |= SF_ERR_CLICL;
4801 if (!(s->flags & SF_FINST_MASK))
4802 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004803
Willy Tarreau87b09662015-04-03 00:22:06 +02004804 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004805 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004806 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004807
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004808 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004809 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004810 return 0;
4811 }
4812
4813 /* More interesting part now : we know that we have a complete
4814 * response which at least looks like HTTP. We have an indicator
4815 * of each header's length, so we can parse them quickly.
4816 */
4817
4818 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004819 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004820
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004821 /*
4822 * 1: get the status code
4823 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004824 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004825 if (n < 1 || n > 5)
4826 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004827 /* when the client triggers a 4xx from the server, it's most often due
4828 * to a missing object or permission. These events should be tracked
4829 * because if they happen often, it may indicate a brute force or a
4830 * vulnerability scan.
4831 */
4832 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004833 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004834
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004835 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004836 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004837
Willy Tarreau91852eb2015-05-01 13:26:00 +02004838 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4839 * exactly one digit "." one digit. This check may be disabled using
4840 * option accept-invalid-http-response.
4841 */
4842 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4843 if (msg->sl.st.v_l != 8) {
4844 msg->err_pos = 0;
4845 goto hdr_response_bad;
4846 }
4847
Willy Tarreauf37954d2018-06-15 18:31:02 +02004848 if (ci_head(rep)[4] != '/' ||
4849 !isdigit((unsigned char)ci_head(rep)[5]) ||
4850 ci_head(rep)[6] != '.' ||
4851 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004852 msg->err_pos = 4;
4853 goto hdr_response_bad;
4854 }
4855 }
4856
Willy Tarreau5b154472009-12-21 20:11:07 +01004857 /* check if the response is HTTP/1.1 or above */
4858 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004859 ((ci_head(rep)[5] > '1') ||
4860 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004861 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004862
4863 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004864 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 +01004865
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004866 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004867 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004868
Willy Tarreauf37954d2018-06-15 18:31:02 +02004869 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004870
Willy Tarreau39650402010-03-15 19:44:39 +01004871 /* Adjust server's health based on status code. Note: status codes 501
4872 * and 505 are triggered on demand by client request, so we must not
4873 * count them as server failures.
4874 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004875 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004876 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004877 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004878 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004879 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004880 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004881
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004882 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004883 * We may be facing a 100-continue response, or any other informational
4884 * 1xx response which is non-final, in which case this is not the right
4885 * response, and we're waiting for the next one. Let's allow this response
4886 * to go to the client and wait for the next one. There's an exception for
4887 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004888 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004889 if (txn->status < 200 &&
4890 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004891 hdr_idx_init(&txn->hdr_idx);
4892 msg->next -= channel_forward(rep, msg->next);
4893 msg->msg_state = HTTP_MSG_RPBEFORE;
4894 txn->status = 0;
4895 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004896 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004897 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004898 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004899
Willy Tarreaua14ad722017-07-07 11:36:32 +02004900 /*
4901 * 2: check for cacheability.
4902 */
4903
4904 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004905 case 200:
4906 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004907 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004908 case 206:
4909 case 300:
4910 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004911 case 404:
4912 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004913 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004914 case 414:
4915 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004916 break;
4917 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004918 /* RFC7231#6.1:
4919 * Responses with status codes that are defined as
4920 * cacheable by default (e.g., 200, 203, 204, 206,
4921 * 300, 301, 404, 405, 410, 414, and 501 in this
4922 * specification) can be reused by a cache with
4923 * heuristic expiration unless otherwise indicated
4924 * by the method definition or explicit cache
4925 * controls [RFC7234]; all other status codes are
4926 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004927 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004928 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004929 break;
4930 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004931
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004932 /*
4933 * 3: we may need to capture headers
4934 */
4935 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004936 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004937 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4938 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004939
Willy Tarreau557f1992015-05-01 10:05:17 +02004940 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4941 * by RFC7230#3.3.3 :
4942 *
4943 * The length of a message body is determined by one of the following
4944 * (in order of precedence):
4945 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004946 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4947 * the connection will become a tunnel immediately after the empty
4948 * line that concludes the header fields. A client MUST ignore
4949 * any Content-Length or Transfer-Encoding header fields received
4950 * in such a message. Any 101 response (Switching Protocols) is
4951 * managed in the same manner.
4952 *
4953 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004954 * (Informational), 204 (No Content), or 304 (Not Modified) status
4955 * code is always terminated by the first empty line after the
4956 * header fields, regardless of the header fields present in the
4957 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004958 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004959 * 3. If a Transfer-Encoding header field is present and the chunked
4960 * transfer coding (Section 4.1) is the final encoding, the message
4961 * body length is determined by reading and decoding the chunked
4962 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004963 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004964 * If a Transfer-Encoding header field is present in a response and
4965 * the chunked transfer coding is not the final encoding, the
4966 * message body length is determined by reading the connection until
4967 * it is closed by the server. If a Transfer-Encoding header field
4968 * is present in a request and the chunked transfer coding is not
4969 * the final encoding, the message body length cannot be determined
4970 * reliably; the server MUST respond with the 400 (Bad Request)
4971 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004972 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004973 * If a message is received with both a Transfer-Encoding and a
4974 * Content-Length header field, the Transfer-Encoding overrides the
4975 * Content-Length. Such a message might indicate an attempt to
4976 * perform request smuggling (Section 9.5) or response splitting
4977 * (Section 9.4) and ought to be handled as an error. A sender MUST
4978 * remove the received Content-Length field prior to forwarding such
4979 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004980 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004981 * 4. If a message is received without Transfer-Encoding and with
4982 * either multiple Content-Length header fields having differing
4983 * field-values or a single Content-Length header field having an
4984 * invalid value, then the message framing is invalid and the
4985 * recipient MUST treat it as an unrecoverable error. If this is a
4986 * request message, the server MUST respond with a 400 (Bad Request)
4987 * status code and then close the connection. If this is a response
4988 * message received by a proxy, the proxy MUST close the connection
4989 * to the server, discard the received response, and send a 502 (Bad
4990 * Gateway) response to the client. If this is a response message
4991 * received by a user agent, the user agent MUST close the
4992 * connection to the server and discard the received response.
4993 *
4994 * 5. If a valid Content-Length header field is present without
4995 * Transfer-Encoding, its decimal value defines the expected message
4996 * body length in octets. If the sender closes the connection or
4997 * the recipient times out before the indicated number of octets are
4998 * received, the recipient MUST consider the message to be
4999 * incomplete and close the connection.
5000 *
5001 * 6. If this is a request message and none of the above are true, then
5002 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005003 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005004 * 7. Otherwise, this is a response message without a declared message
5005 * body length, so the message body length is determined by the
5006 * number of octets received prior to the server closing the
5007 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005008 */
5009
5010 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005011 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005012 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005013 * FIXME: should we parse anyway and return an error on chunked encoding ?
5014 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005015 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5016 txn->status == 101)) {
5017 /* Either we've established an explicit tunnel, or we're
5018 * switching the protocol. In both cases, we're very unlikely
5019 * to understand the next protocols. We have to switch to tunnel
5020 * mode, so that we transfer the request and responses then let
5021 * this protocol pass unmodified. When we later implement specific
5022 * parsers for such protocols, we'll want to check the Upgrade
5023 * header which contains information about that protocol for
5024 * responses with status 101 (eg: see RFC2817 about TLS).
5025 */
5026 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5027 msg->flags |= HTTP_MSGF_XFER_LEN;
5028 goto end;
5029 }
5030
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005031 if (txn->meth == HTTP_METH_HEAD ||
5032 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005033 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005034 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005035 goto skip_content_length;
5036 }
5037
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005038 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005039 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005040 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005041 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005042 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5043 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005044 /* bad transfer-encoding (chunked followed by something else) */
5045 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005046 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005047 break;
5048 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005049 }
5050
Willy Tarreau1c913912015-04-30 10:57:51 +02005051 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005052 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005053 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005054 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02005055 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5056 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02005057 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005058 signed long long cl;
5059
Willy Tarreauad14f752011-09-02 20:33:27 +02005060 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005061 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005062 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005063 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005064
Willy Tarreauad14f752011-09-02 20:33:27 +02005065 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005066 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005067 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005068 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005069
Willy Tarreauad14f752011-09-02 20:33:27 +02005070 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005071 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005072 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005073 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005074
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005075 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005076 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005077 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005078 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005079
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005080 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005081 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005082 }
5083
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02005084 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
5085 * 407 (Proxy-Authenticate) responses and set the connection to private
5086 */
5087 if (srv_conn && txn->status == 401) {
5088 /* check for Negotiate/NTLM WWW-Authenticate headers */
5089 ctx.idx = 0;
5090 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
5091 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5092 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5093 srv_conn->flags |= CO_FL_PRIVATE;
5094 }
5095 } else if (srv_conn && txn->status == 407) {
5096 /* check for Negotiate/NTLM Proxy-Authenticate headers */
5097 ctx.idx = 0;
5098 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
5099 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5100 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5101 srv_conn->flags |= CO_FL_PRIVATE;
5102 }
5103 }
5104
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005105 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005106 /* Now we have to check if we need to modify the Connection header.
5107 * This is more difficult on the response than it is on the request,
5108 * because we can have two different HTTP versions and we don't know
5109 * how the client will interprete a response. For instance, let's say
5110 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5111 * HTTP/1.1 response without any header. Maybe it will bound itself to
5112 * HTTP/1.0 because it only knows about it, and will consider the lack
5113 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5114 * the lack of header as a keep-alive. Thus we will use two flags
5115 * indicating how a request MAY be understood by the client. In case
5116 * of multiple possibilities, we'll fix the header to be explicit. If
5117 * ambiguous cases such as both close and keepalive are seen, then we
5118 * will fall back to explicit close. Note that we won't take risks with
5119 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005120 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005121 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005122 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005123 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01005124 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005125
Willy Tarreau60466522010-01-18 19:08:45 +01005126 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005127 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01005128 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005129
Willy Tarreau60466522010-01-18 19:08:45 +01005130 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005131 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01005132 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005133 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005134 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005135 }
Willy Tarreau60466522010-01-18 19:08:45 +01005136 else { /* SCL / KAL */
5137 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005138 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005139 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005140 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005141
Willy Tarreau60466522010-01-18 19:08:45 +01005142 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005143 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005144
Willy Tarreau60466522010-01-18 19:08:45 +01005145 /* Some keep-alive responses are converted to Server-close if
5146 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005147 */
Willy Tarreau60466522010-01-18 19:08:45 +01005148 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5149 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005150 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005151 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005152 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005153 }
5154
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005155 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005156 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005157 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005158
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005159 /* end of job, return OK */
5160 rep->analysers &= ~an_bit;
5161 rep->analyse_exp = TICK_ETERNITY;
5162 channel_auto_close(rep);
5163 return 1;
5164
5165 abort_keep_alive:
5166 /* A keep-alive request to the server failed on a network error.
5167 * The client is required to retry. We need to close without returning
5168 * any other information so that the client retries.
5169 */
5170 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005171 rep->analysers &= AN_RES_FLT_END;
5172 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005173 channel_auto_close(rep);
5174 s->logs.logwait = 0;
5175 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005176 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005177 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005178 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005179 return 0;
5180}
5181
5182/* This function performs all the processing enabled for the current response.
5183 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005184 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005185 * other functions. It works like process_request (see indications above).
5186 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005187int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005188{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005189 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005190 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005191 struct http_msg *msg = &txn->rsp;
5192 struct proxy *cur_proxy;
5193 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005194 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005195
Christopher Faulete0768eb2018-10-03 16:38:02 +02005196 if (IS_HTX_STRM(s))
5197 return htx_process_res_common(s, rep, an_bit, px);
5198
Christopher Faulet45073512018-07-20 10:16:29 +02005199 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 +02005200 now_ms, __FUNCTION__,
5201 s,
5202 rep,
5203 rep->rex, rep->wex,
5204 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005205 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005206 rep->analysers);
5207
5208 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5209 return 0;
5210
Willy Tarreau70730dd2014-04-24 18:06:27 +02005211 /* The stats applet needs to adjust the Connection header but we don't
5212 * apply any filter there.
5213 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005214 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5215 rep->analysers &= ~an_bit;
5216 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005217 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005218 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005219
Willy Tarreau58975672014-04-24 21:13:57 +02005220 /*
5221 * We will have to evaluate the filters.
5222 * As opposed to version 1.2, now they will be evaluated in the
5223 * filters order and not in the header order. This means that
5224 * each filter has to be validated among all headers.
5225 *
5226 * Filters are tried with ->be first, then with ->fe if it is
5227 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005228 *
5229 * Maybe we are in resume condiion. In this case I choose the
5230 * "struct proxy" which contains the rule list matching the resume
5231 * pointer. If none of theses "struct proxy" match, I initialise
5232 * the process with the first one.
5233 *
5234 * In fact, I check only correspondance betwwen the current list
5235 * pointer and the ->fe rule list. If it doesn't match, I initialize
5236 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005237 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005238 if (s->current_rule_list == &sess->fe->http_res_rules)
5239 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005240 else
5241 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005242 while (1) {
5243 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005244
Willy Tarreau58975672014-04-24 21:13:57 +02005245 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005246 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005247 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005248
Willy Tarreau51d861a2015-05-22 17:30:48 +02005249 if (ret == HTTP_RULE_RES_BADREQ)
5250 goto return_srv_prx_502;
5251
5252 if (ret == HTTP_RULE_RES_DONE) {
5253 rep->analysers &= ~an_bit;
5254 rep->analyse_exp = TICK_ETERNITY;
5255 return 1;
5256 }
5257 }
5258
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005259 /* we need to be called again. */
5260 if (ret == HTTP_RULE_RES_YIELD) {
5261 channel_dont_close(rep);
5262 return 0;
5263 }
5264
Willy Tarreau58975672014-04-24 21:13:57 +02005265 /* try headers filters */
5266 if (rule_set->rsp_exp != NULL) {
5267 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5268 return_bad_resp:
5269 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005270 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005271 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005272 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005273 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005274 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005275 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005276 txn->status = 502;
5277 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005278 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005279 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005280 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005281 if (!(s->flags & SF_ERR_MASK))
5282 s->flags |= SF_ERR_PRXCOND;
5283 if (!(s->flags & SF_FINST_MASK))
5284 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005285 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005286 }
Willy Tarreau58975672014-04-24 21:13:57 +02005287 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005288
Willy Tarreau58975672014-04-24 21:13:57 +02005289 /* has the response been denied ? */
5290 if (txn->flags & TX_SVDENY) {
5291 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005292 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005293
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005294 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5295 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005296 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005297 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005298
Willy Tarreau58975672014-04-24 21:13:57 +02005299 goto return_srv_prx_502;
5300 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005301
Willy Tarreau58975672014-04-24 21:13:57 +02005302 /* add response headers from the rule sets in the same order */
5303 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005304 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005305 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005306 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005307 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005308 ret = acl_pass(ret);
5309 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5310 ret = !ret;
5311 if (!ret)
5312 continue;
5313 }
Christopher Faulet10079f52018-10-03 15:17:28 +02005314 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005315 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005316 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005317
Willy Tarreau58975672014-04-24 21:13:57 +02005318 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005319 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005320 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005321 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005322 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005323
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005324 /* After this point, this anayzer can't return yield, so we can
5325 * remove the bit corresponding to this analyzer from the list.
5326 *
5327 * Note that the intermediate returns and goto found previously
5328 * reset the analyzers.
5329 */
5330 rep->analysers &= ~an_bit;
5331 rep->analyse_exp = TICK_ETERNITY;
5332
Willy Tarreau58975672014-04-24 21:13:57 +02005333 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005334 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005335 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005336
Willy Tarreau58975672014-04-24 21:13:57 +02005337 /*
5338 * Now check for a server cookie.
5339 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005340 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005341 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005342
Willy Tarreau58975672014-04-24 21:13:57 +02005343 /*
5344 * Check for cache-control or pragma headers if required.
5345 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005346 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005347 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005348
Willy Tarreau58975672014-04-24 21:13:57 +02005349 /*
5350 * Add server cookie in the response if needed
5351 */
5352 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5353 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005354 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005355 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5356 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5357 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5358 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5359 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005360 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005361 /* the server is known, it's not the one the client requested, or the
5362 * cookie's last seen date needs to be refreshed. We have to
5363 * insert a set-cookie here, except if we want to insert only on POST
5364 * requests and this one isn't. Note that servers which don't have cookies
5365 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005366 */
Willy Tarreau58975672014-04-24 21:13:57 +02005367 if (!objt_server(s->target)->cookie) {
5368 chunk_printf(&trash,
5369 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5370 s->be->cookie_name);
5371 }
5372 else {
5373 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005374
Willy Tarreau58975672014-04-24 21:13:57 +02005375 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5376 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005377 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5378 s30tob64((date.tv_sec+3) >> 2,
5379 trash.area + trash.data);
5380 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005381
Willy Tarreau58975672014-04-24 21:13:57 +02005382 if (s->be->cookie_maxlife) {
5383 /* emit first_date, which is either the original one or
5384 * the current date.
5385 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005386 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005387 s30tob64(txn->cookie_first_date ?
5388 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005389 (date.tv_sec+3) >> 2,
5390 trash.area + trash.data);
5391 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005392 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005393 }
Willy Tarreau58975672014-04-24 21:13:57 +02005394 chunk_appendf(&trash, "; path=/");
5395 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005396
Willy Tarreau58975672014-04-24 21:13:57 +02005397 if (s->be->cookie_domain)
5398 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005399
Willy Tarreau58975672014-04-24 21:13:57 +02005400 if (s->be->ck_opts & PR_CK_HTTPONLY)
5401 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005402
Willy Tarreau58975672014-04-24 21:13:57 +02005403 if (s->be->ck_opts & PR_CK_SECURE)
5404 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005405
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005406 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005407 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005408
Willy Tarreau58975672014-04-24 21:13:57 +02005409 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005410 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005411 /* the server did not change, only the date was updated */
5412 txn->flags |= TX_SCK_UPDATED;
5413 else
5414 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005415
Willy Tarreau58975672014-04-24 21:13:57 +02005416 /* Here, we will tell an eventual cache on the client side that we don't
5417 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5418 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5419 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005420 */
Willy Tarreau58975672014-04-24 21:13:57 +02005421 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005422
Willy Tarreau58975672014-04-24 21:13:57 +02005423 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005424
Willy Tarreau58975672014-04-24 21:13:57 +02005425 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5426 "Cache-control: private", 22) < 0))
5427 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005428 }
Willy Tarreau58975672014-04-24 21:13:57 +02005429 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005430
Willy Tarreau58975672014-04-24 21:13:57 +02005431 /*
5432 * Check if result will be cacheable with a cookie.
5433 * We'll block the response if security checks have caught
5434 * nasty things such as a cacheable cookie.
5435 */
5436 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5437 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5438 (s->be->options & PR_O_CHK_CACHE)) {
5439 /* we're in presence of a cacheable response containing
5440 * a set-cookie header. We'll block it as requested by
5441 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005442 */
Willy Tarreau58975672014-04-24 21:13:57 +02005443 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005444 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005445
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005446 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5447 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005448 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005449 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005450
Christopher Faulet767a84b2017-11-24 16:50:31 +01005451 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5452 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005453 send_log(s->be, LOG_ALERT,
5454 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5455 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5456 goto return_srv_prx_502;
5457 }
Willy Tarreau03945942009-12-22 16:50:27 +01005458
Willy Tarreau70730dd2014-04-24 18:06:27 +02005459 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005460 /*
5461 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5462 * If an "Upgrade" token is found, the header is left untouched in order
5463 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005464 * if anything but "Upgrade" is present in the Connection header. We don't
5465 * want to touch any 101 response either since it's switching to another
5466 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005467 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005468 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005469 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005470 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005471
Willy Tarreau58975672014-04-24 21:13:57 +02005472 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5473 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5474 /* we want a keep-alive response here. Keep-alive header
5475 * required if either side is not 1.1.
5476 */
5477 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5478 want_flags |= TX_CON_KAL_SET;
5479 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005480 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005481 /* we want a close response here. Close header required if
5482 * the server is 1.1, regardless of the client.
5483 */
5484 if (msg->flags & HTTP_MSGF_VER_11)
5485 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005486 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005487
Willy Tarreau58975672014-04-24 21:13:57 +02005488 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5489 http_change_connection_header(txn, msg, want_flags);
5490 }
5491
5492 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005493 /* Always enter in the body analyzer */
5494 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5495 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005496
Willy Tarreau58975672014-04-24 21:13:57 +02005497 /* if the user wants to log as soon as possible, without counting
5498 * bytes from the server, then this is the right moment. We have
5499 * to temporarily assign bytes_out to log what we currently have.
5500 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005501 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005502 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5503 s->logs.bytes_out = txn->rsp.eoh;
5504 s->do_log(s);
5505 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005506 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005507 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005508}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005509
Willy Tarreaud98cf932009-12-27 22:54:55 +01005510/* This function is an analyser which forwards response body (including chunk
5511 * sizes if any). It is called as soon as we must forward, even if we forward
5512 * zero byte. The only situation where it must not be called is when we're in
5513 * tunnel mode and we want to forward till the close. It's used both to forward
5514 * remaining data and to resync after end of body. It expects the msg_state to
5515 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005516 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005517 *
5518 * It is capable of compressing response data both in content-length mode and
5519 * in chunked mode. The state machines follows different flows depending on
5520 * whether content-length and chunked modes are used, since there are no
5521 * trailers in content-length :
5522 *
5523 * chk-mode cl-mode
5524 * ,----- BODY -----.
5525 * / \
5526 * V size > 0 V chk-mode
5527 * .--> SIZE -------------> DATA -------------> CRLF
5528 * | | size == 0 | last byte |
5529 * | v final crlf v inspected |
5530 * | TRAILERS -----------> DONE |
5531 * | |
5532 * `----------------------------------------------'
5533 *
5534 * Compression only happens in the DATA state, and must be flushed in final
5535 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5536 * is performed at once on final states for all bytes parsed, or when leaving
5537 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005538 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005539int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005540{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005541 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005542 struct http_txn *txn = s->txn;
5543 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005544 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005545
Christopher Faulete0768eb2018-10-03 16:38:02 +02005546 if (IS_HTX_STRM(s))
5547 return htx_response_forward_body(s, res, an_bit);
5548
Christopher Faulet45073512018-07-20 10:16:29 +02005549 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 +02005550 now_ms, __FUNCTION__,
5551 s,
5552 res,
5553 res->rex, res->wex,
5554 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005555 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005556 res->analysers);
5557
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005558 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5559 return 0;
5560
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005561 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005562 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005563 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005564 /* Output closed while we were sending data. We must abort and
5565 * wake the other side up.
5566 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005567 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005568 msg->msg_state = HTTP_MSG_ERROR;
5569 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005570 return 1;
5571 }
5572
Willy Tarreau4fe41902010-06-07 22:27:41 +02005573 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005574 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005575
Christopher Fauletd7c91962015-04-30 11:48:27 +02005576 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005577 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5578 ? HTTP_MSG_CHUNK_SIZE
5579 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005580 }
5581
Willy Tarreauefdf0942014-04-24 20:08:57 +02005582 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005583 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005584 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005585 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005586 }
5587
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005588 if (msg->msg_state < HTTP_MSG_DONE) {
5589 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5590 ? http_msg_forward_chunked_body(s, msg)
5591 : http_msg_forward_body(s, msg));
5592 if (!ret)
5593 goto missing_data_or_waiting;
5594 if (ret < 0)
5595 goto return_bad_res;
5596 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005597
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005598 /* other states, DONE...TUNNEL */
5599 /* for keep-alive we don't want to forward closes on DONE */
5600 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5601 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5602 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005603
Christopher Faulet894da4c2017-07-18 11:29:07 +02005604 http_resync_states(s);
5605 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005606 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5607 if (res->flags & CF_SHUTW) {
5608 /* response errors are most likely due to the
5609 * client aborting the transfer. */
5610 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005611 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005612 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005613 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005614 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005615 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005616 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005617 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005618 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005619
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005620 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005621 if (res->flags & CF_SHUTW)
5622 goto aborted_xfer;
5623
5624 /* stop waiting for data if the input is closed before the end. If the
5625 * client side was already closed, it means that the client has aborted,
5626 * so we don't want to count this as a server abort. Otherwise it's a
5627 * server abort.
5628 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005629 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005630 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005631 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005632 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005633 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005634 if (!(s->flags & SF_ERR_MASK))
5635 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005636 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005637 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005638 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005639 goto return_bad_res_stats_ok;
5640 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005641 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005642
Willy Tarreau40dba092010-03-04 18:14:51 +01005643 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005644 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005645 goto return_bad_res;
5646
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005647 /* When TE: chunked is used, we need to get there again to parse
5648 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005649 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5650 * or if there are filters registered on the stream, we don't want to
5651 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005652 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005653 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005654 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005655 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5656 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005657 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005658
Willy Tarreau5c620922011-05-11 19:56:11 +02005659 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005660 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005661 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005662 * modes are already handled by the stream sock layer. We must not do
5663 * this in content-length mode because it could present the MSG_MORE
5664 * flag with the last block of forwarded data, which would cause an
5665 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005666 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005667 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005668 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005669
Willy Tarreau87b09662015-04-03 00:22:06 +02005670 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005671 return 0;
5672
Willy Tarreau40dba092010-03-04 18:14:51 +01005673 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005674 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005675 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005676 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005677
5678 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005679 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005680 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005681 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005682 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005683 res->analysers &= AN_RES_FLT_END;
5684 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 +01005685 if (objt_server(s->target))
5686 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005687
Willy Tarreaue7dff022015-04-03 01:14:29 +02005688 if (!(s->flags & SF_ERR_MASK))
5689 s->flags |= SF_ERR_PRXCOND;
5690 if (!(s->flags & SF_FINST_MASK))
5691 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005692 return 0;
5693
5694 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005695 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005696 txn->rsp.msg_state = HTTP_MSG_ERROR;
5697 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005698 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005699 res->analysers &= AN_RES_FLT_END;
5700 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 +01005701
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005702 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5703 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005704 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005705 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005706
Willy Tarreaue7dff022015-04-03 01:14:29 +02005707 if (!(s->flags & SF_ERR_MASK))
5708 s->flags |= SF_ERR_CLICL;
5709 if (!(s->flags & SF_FINST_MASK))
5710 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005711 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005712}
5713
5714
Christopher Faulet10079f52018-10-03 15:17:28 +02005715int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005716{
5717 struct channel *chn = msg->chn;
5718 int ret;
5719
5720 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5721
5722 if (msg->msg_state == HTTP_MSG_ENDING)
5723 goto ending;
5724
5725 /* Neither content-length, nor transfer-encoding was found, so we must
5726 * read the body until the server connection is closed. In that case, we
5727 * eat data as they come. Of course, this happens for response only. */
5728 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005729 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005730 msg->chunk_len += len;
5731 msg->body_len += len;
5732 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005733 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005734 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005735 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005736 msg->next += ret;
5737 msg->chunk_len -= ret;
5738 if (msg->chunk_len) {
5739 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005740 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005741 chn->flags |= CF_WAKE_WRITE;
5742 goto missing_data_or_waiting;
5743 }
5744
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005745 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5746 * always set for a request. */
5747 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5748 /* The server still sending data that should be filtered */
5749 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5750 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005751 msg->msg_state = HTTP_MSG_TUNNEL;
5752 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005753 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005754
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005755 msg->msg_state = HTTP_MSG_ENDING;
5756
5757 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005758 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005759 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005760 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5761 /* default_ret */ msg->next,
5762 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005763 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005764 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005765 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5766 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005767 if (msg->next)
5768 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005769
Christopher Fauletda02e172015-12-04 09:25:05 +01005770 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5771 /* default_ret */ 1,
5772 /* on_error */ goto error,
5773 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005774 if (msg->msg_state == HTTP_MSG_ENDING)
5775 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005776 return 1;
5777
5778 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005779 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005780 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5781 /* default_ret */ msg->next,
5782 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005783 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005784 msg->next -= ret;
5785 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5786 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005787 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005788 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005789 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005790 return 0;
5791 error:
5792 return -1;
5793}
5794
Christopher Faulet10079f52018-10-03 15:17:28 +02005795int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005796{
5797 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005798 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005799 int ret;
5800
5801 /* Here we have the guarantee to be in one of the following state:
5802 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5803 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5804
Christopher Fauletca874b82018-09-20 11:31:01 +02005805 if (msg->msg_state == HTTP_MSG_ENDING)
5806 goto ending;
5807
5808 /* Don't parse chunks if there is no input data */
5809 if (!ci_data(chn))
5810 goto waiting;
5811
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005812 switch_states:
5813 switch (msg->msg_state) {
5814 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005815 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005816 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005817 /* on_error */ goto error);
5818 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005819 msg->chunk_len -= ret;
5820 if (msg->chunk_len) {
5821 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005822 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005823 chn->flags |= CF_WAKE_WRITE;
5824 goto missing_data_or_waiting;
5825 }
5826
5827 /* nothing left to forward for this chunk*/
5828 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5829 /* fall through for HTTP_MSG_CHUNK_CRLF */
5830
5831 case HTTP_MSG_CHUNK_CRLF:
5832 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005833 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005834 if (ret == 0)
5835 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005836 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005837 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005838 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005839 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005840 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005841 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005842 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005843 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5844 /* fall through for HTTP_MSG_CHUNK_SIZE */
5845
5846 case HTTP_MSG_CHUNK_SIZE:
5847 /* read the chunk size and assign it to ->chunk_len,
5848 * then set ->next to point to the body and switch to
5849 * DATA or TRAILERS state.
5850 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005851 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005852 if (ret == 0)
5853 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005854 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005855 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005856 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005857 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005858 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005859 }
5860
5861 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005862 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005863 msg->chunk_len = chunk;
5864 msg->body_len += chunk;
5865
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005866 if (msg->chunk_len) {
5867 msg->msg_state = HTTP_MSG_DATA;
5868 goto switch_states;
5869 }
5870 msg->msg_state = HTTP_MSG_TRAILERS;
5871 /* fall through for HTTP_MSG_TRAILERS */
5872
5873 case HTTP_MSG_TRAILERS:
5874 ret = http_forward_trailers(msg);
5875 if (ret < 0)
5876 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005877 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5878 /* default_ret */ 1,
5879 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005880 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005881 if (!ret)
5882 goto missing_data_or_waiting;
5883 break;
5884
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005885 default:
5886 /* This should no happen in this function */
5887 goto error;
5888 }
5889
5890 msg->msg_state = HTTP_MSG_ENDING;
5891 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005892 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005893 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005894 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005895 /* default_ret */ msg->next,
5896 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005897 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005898 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005899 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5900 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005901 if (msg->next)
5902 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005903
Christopher Fauletda02e172015-12-04 09:25:05 +01005904 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005905 /* default_ret */ 1,
5906 /* on_error */ goto error,
5907 /* on_wait */ goto waiting);
5908 msg->msg_state = HTTP_MSG_DONE;
5909 return 1;
5910
5911 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005912 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005913 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005914 /* default_ret */ msg->next,
5915 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005916 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005917 msg->next -= ret;
5918 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5919 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005920 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005921 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005922 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005923 return 0;
5924
5925 chunk_parsing_error:
5926 if (msg->err_pos >= 0) {
5927 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005928 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005929 msg->msg_state, strm_fe(s));
5930 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005931 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005932 msg, msg->msg_state, s->be);
5933 }
5934 error:
5935 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005936}
5937
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005938
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005939/* Iterate the same filter through all request headers.
5940 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005941 * Since it can manage the switch to another backend, it updates the per-proxy
5942 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005943 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005944int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005945{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005946 char *cur_ptr, *cur_end, *cur_next;
5947 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005948 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005949 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005950 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005951
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005952 last_hdr = 0;
5953
Willy Tarreauf37954d2018-06-15 18:31:02 +02005954 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005955 old_idx = 0;
5956
5957 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005958 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005959 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005960 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005961 (exp->action == ACT_ALLOW ||
5962 exp->action == ACT_DENY ||
5963 exp->action == ACT_TARPIT))
5964 return 0;
5965
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005966 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005967 if (!cur_idx)
5968 break;
5969
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005970 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005971 cur_ptr = cur_next;
5972 cur_end = cur_ptr + cur_hdr->len;
5973 cur_next = cur_end + cur_hdr->cr + 1;
5974
5975 /* Now we have one header between cur_ptr and cur_end,
5976 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005977 */
5978
Willy Tarreau15a53a42015-01-21 13:39:42 +01005979 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005980 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005981 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005982 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005983 last_hdr = 1;
5984 break;
5985
5986 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005987 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005988 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005989 break;
5990
5991 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005992 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005993 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005994 break;
5995
5996 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005997 len = exp_replace(trash.area,
5998 trash.size, cur_ptr,
5999 exp->replace, pmatch);
6000 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006001 return -1;
6002
Willy Tarreau6e27be12018-08-22 04:46:47 +02006003 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6004
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006005 /* FIXME: if the user adds a newline in the replacement, the
6006 * index will not be recalculated for now, and the new line
6007 * will not be counted as a new header.
6008 */
6009
6010 cur_end += delta;
6011 cur_next += delta;
6012 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006013 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006014 break;
6015
6016 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006017 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006018 cur_next += delta;
6019
Willy Tarreaufa355d42009-11-29 18:12:29 +01006020 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006021 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6022 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006023 cur_hdr->len = 0;
6024 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006025 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006026 break;
6027
6028 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006029 }
6030
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006031 /* keep the link from this header to next one in case of later
6032 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006033 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006034 old_idx = cur_idx;
6035 }
6036 return 0;
6037}
6038
6039
6040/* Apply the filter to the request line.
6041 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6042 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006043 * Since it can manage the switch to another backend, it updates the per-proxy
6044 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006045 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006046int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006047{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006048 char *cur_ptr, *cur_end;
6049 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006050 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006051 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006052
Willy Tarreau3d300592007-03-18 18:34:41 +01006053 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006054 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006055 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006056 (exp->action == ACT_ALLOW ||
6057 exp->action == ACT_DENY ||
6058 exp->action == ACT_TARPIT))
6059 return 0;
6060 else if (exp->action == ACT_REMOVE)
6061 return 0;
6062
6063 done = 0;
6064
Willy Tarreauf37954d2018-06-15 18:31:02 +02006065 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006066 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006067
6068 /* Now we have the request line between cur_ptr and cur_end */
6069
Willy Tarreau15a53a42015-01-21 13:39:42 +01006070 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006071 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006072 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006073 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006074 done = 1;
6075 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006076
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006077 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006078 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006079 done = 1;
6080 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006081
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006082 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006083 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006084 done = 1;
6085 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006086
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006087 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006088 len = exp_replace(trash.area, trash.size,
6089 cur_ptr, exp->replace, pmatch);
6090 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006091 return -1;
6092
Willy Tarreau6e27be12018-08-22 04:46:47 +02006093 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6094
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006095 /* FIXME: if the user adds a newline in the replacement, the
6096 * index will not be recalculated for now, and the new line
6097 * will not be counted as a new header.
6098 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006099
Willy Tarreaufa355d42009-11-29 18:12:29 +01006100 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006101 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006102 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006103 HTTP_MSG_RQMETH,
6104 cur_ptr, cur_end + 1,
6105 NULL, NULL);
6106 if (unlikely(!cur_end))
6107 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006108
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006109 /* we have a full request and we know that we have either a CR
6110 * or an LF at <ptr>.
6111 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006112 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6113 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006114 /* there is no point trying this regex on headers */
6115 return 1;
6116 }
6117 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006118 return done;
6119}
Willy Tarreau97de6242006-12-27 17:18:38 +01006120
Willy Tarreau58f10d72006-12-04 02:26:12 +01006121
Willy Tarreau58f10d72006-12-04 02:26:12 +01006122
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006123/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006124 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006125 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006126 * unparsable request. Since it can manage the switch to another backend, it
6127 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006128 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006129int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006130{
Willy Tarreau192252e2015-04-04 01:47:55 +02006131 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006132 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006133 struct hdr_exp *exp;
6134
6135 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006136 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006137
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006138 /*
6139 * The interleaving of transformations and verdicts
6140 * makes it difficult to decide to continue or stop
6141 * the evaluation.
6142 */
6143
Willy Tarreau6c123b12010-01-28 20:22:06 +01006144 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6145 break;
6146
Willy Tarreau3d300592007-03-18 18:34:41 +01006147 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006148 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006149 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006150 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006151
6152 /* if this filter had a condition, evaluate it now and skip to
6153 * next filter if the condition does not match.
6154 */
6155 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006156 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006157 ret = acl_pass(ret);
6158 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6159 ret = !ret;
6160
6161 if (!ret)
6162 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006163 }
6164
6165 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006166 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006167 if (unlikely(ret < 0))
6168 return -1;
6169
6170 if (likely(ret == 0)) {
6171 /* The filter did not match the request, it can be
6172 * iterated through all headers.
6173 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006174 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6175 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006176 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006177 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006178 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006179}
6180
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006181
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006182/* Delete a value in a header between delimiters <from> and <next> in buffer
6183 * <buf>. The number of characters displaced is returned, and the pointer to
6184 * the first delimiter is updated if required. The function tries as much as
6185 * possible to respect the following principles :
6186 * - replace <from> delimiter by the <next> one unless <from> points to a
6187 * colon, in which case <next> is simply removed
6188 * - set exactly one space character after the new first delimiter, unless
6189 * there are not enough characters in the block being moved to do so.
6190 * - remove unneeded spaces before the previous delimiter and after the new
6191 * one.
6192 *
6193 * It is the caller's responsibility to ensure that :
6194 * - <from> points to a valid delimiter or the colon ;
6195 * - <next> points to a valid delimiter or the final CR/LF ;
6196 * - there are non-space chars before <from> ;
6197 * - there is a CR/LF at or after <next>.
6198 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006199int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006200{
6201 char *prev = *from;
6202
6203 if (*prev == ':') {
6204 /* We're removing the first value, preserve the colon and add a
6205 * space if possible.
6206 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006207 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006208 next++;
6209 prev++;
6210 if (prev < next)
6211 *prev++ = ' ';
6212
Willy Tarreau2235b262016-11-05 15:50:20 +01006213 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006214 next++;
6215 } else {
6216 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006217 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006218 prev--;
6219 *from = prev;
6220
6221 /* copy the delimiter and if possible a space if we're
6222 * not at the end of the line.
6223 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006224 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006225 *prev++ = *next++;
6226 if (prev + 1 < next)
6227 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006228 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006229 next++;
6230 }
6231 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006232 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006233}
6234
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006235/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006236 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006237 * desirable to call it only when needed. This code is quite complex because
6238 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6239 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006240 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006241void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006242{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006243 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006244 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006245 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006246 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006247 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6248 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006249
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006250 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006251 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006252 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006253
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006254 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006255 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006256 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006257
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006258 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006259 hdr_beg = hdr_next;
6260 hdr_end = hdr_beg + cur_hdr->len;
6261 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006262
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006263 /* We have one full header between hdr_beg and hdr_end, and the
6264 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006265 * "Cookie:" headers.
6266 */
6267
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006268 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006269 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006270 old_idx = cur_idx;
6271 continue;
6272 }
6273
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006274 del_from = NULL; /* nothing to be deleted */
6275 preserve_hdr = 0; /* assume we may kill the whole header */
6276
Willy Tarreau58f10d72006-12-04 02:26:12 +01006277 /* Now look for cookies. Conforming to RFC2109, we have to support
6278 * attributes whose name begin with a '$', and associate them with
6279 * the right cookie, if we want to delete this cookie.
6280 * So there are 3 cases for each cookie read :
6281 * 1) it's a special attribute, beginning with a '$' : ignore it.
6282 * 2) it's a server id cookie that we *MAY* want to delete : save
6283 * some pointers on it (last semi-colon, beginning of cookie...)
6284 * 3) it's an application cookie : we *MAY* have to delete a previous
6285 * "special" cookie.
6286 * At the end of loop, if a "special" cookie remains, we may have to
6287 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006288 * *MUST* delete it.
6289 *
6290 * Note: RFC2965 is unclear about the processing of spaces around
6291 * the equal sign in the ATTR=VALUE form. A careful inspection of
6292 * the RFC explicitly allows spaces before it, and not within the
6293 * tokens (attrs or values). An inspection of RFC2109 allows that
6294 * too but section 10.1.3 lets one think that spaces may be allowed
6295 * after the equal sign too, resulting in some (rare) buggy
6296 * implementations trying to do that. So let's do what servers do.
6297 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6298 * allowed quoted strings in values, with any possible character
6299 * after a backslash, including control chars and delimitors, which
6300 * causes parsing to become ambiguous. Browsers also allow spaces
6301 * within values even without quotes.
6302 *
6303 * We have to keep multiple pointers in order to support cookie
6304 * removal at the beginning, middle or end of header without
6305 * corrupting the header. All of these headers are valid :
6306 *
6307 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6308 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6309 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6310 * | | | | | | | | |
6311 * | | | | | | | | hdr_end <--+
6312 * | | | | | | | +--> next
6313 * | | | | | | +----> val_end
6314 * | | | | | +-----------> val_beg
6315 * | | | | +--------------> equal
6316 * | | | +----------------> att_end
6317 * | | +---------------------> att_beg
6318 * | +--------------------------> prev
6319 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006320 */
6321
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006322 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6323 /* Iterate through all cookies on this line */
6324
6325 /* find att_beg */
6326 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006327 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006328 att_beg++;
6329
6330 /* find att_end : this is the first character after the last non
6331 * space before the equal. It may be equal to hdr_end.
6332 */
6333 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006334
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006335 while (equal < hdr_end) {
6336 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006337 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006338 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006339 continue;
6340 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006341 }
6342
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006343 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6344 * is between <att_beg> and <equal>, both may be identical.
6345 */
6346
6347 /* look for end of cookie if there is an equal sign */
6348 if (equal < hdr_end && *equal == '=') {
6349 /* look for the beginning of the value */
6350 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006351 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006352 val_beg++;
6353
6354 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006355 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006356
6357 /* make val_end point to the first white space or delimitor after the value */
6358 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006359 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006360 val_end--;
6361 } else {
6362 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006363 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006364
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006365 /* We have nothing to do with attributes beginning with '$'. However,
6366 * they will automatically be removed if a header before them is removed,
6367 * since they're supposed to be linked together.
6368 */
6369 if (*att_beg == '$')
6370 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006371
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006372 /* Ignore cookies with no equal sign */
6373 if (equal == next) {
6374 /* This is not our cookie, so we must preserve it. But if we already
6375 * scheduled another cookie for removal, we cannot remove the
6376 * complete header, but we can remove the previous block itself.
6377 */
6378 preserve_hdr = 1;
6379 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006380 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006381 val_end += delta;
6382 next += delta;
6383 hdr_end += delta;
6384 hdr_next += delta;
6385 cur_hdr->len += delta;
6386 http_msg_move_end(&txn->req, delta);
6387 prev = del_from;
6388 del_from = NULL;
6389 }
6390 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006391 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006392
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006393 /* if there are spaces around the equal sign, we need to
6394 * strip them otherwise we'll get trouble for cookie captures,
6395 * or even for rewrites. Since this happens extremely rarely,
6396 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006397 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006398 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6399 int stripped_before = 0;
6400 int stripped_after = 0;
6401
6402 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006403 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006404 equal += stripped_before;
6405 val_beg += stripped_before;
6406 }
6407
6408 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006409 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006410 val_beg += stripped_after;
6411 stripped_before += stripped_after;
6412 }
6413
6414 val_end += stripped_before;
6415 next += stripped_before;
6416 hdr_end += stripped_before;
6417 hdr_next += stripped_before;
6418 cur_hdr->len += stripped_before;
6419 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006420 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006421 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006422
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006423 /* First, let's see if we want to capture this cookie. We check
6424 * that we don't already have a client side cookie, because we
6425 * can only capture one. Also as an optimisation, we ignore
6426 * cookies shorter than the declared name.
6427 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006428 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6429 (val_end - att_beg >= sess->fe->capture_namelen) &&
6430 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006431 int log_len = val_end - att_beg;
6432
Willy Tarreaubafbe012017-11-24 17:34:44 +01006433 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006434 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006435 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006436 if (log_len > sess->fe->capture_len)
6437 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006438 memcpy(txn->cli_cookie, att_beg, log_len);
6439 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006440 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006441 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006442
Willy Tarreaubca99692010-10-06 19:25:55 +02006443 /* Persistence cookies in passive, rewrite or insert mode have the
6444 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006445 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006446 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006447 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006448 * For cookies in prefix mode, the form is :
6449 *
6450 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006451 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006452 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6453 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6454 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006455 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006456
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006457 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6458 * have the server ID between val_beg and delim, and the original cookie between
6459 * delim+1 and val_end. Otherwise, delim==val_end :
6460 *
6461 * Cookie: NAME=SRV; # in all but prefix modes
6462 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6463 * | || || | |+-> next
6464 * | || || | +--> val_end
6465 * | || || +---------> delim
6466 * | || |+------------> val_beg
6467 * | || +-------------> att_end = equal
6468 * | |+-----------------> att_beg
6469 * | +------------------> prev
6470 * +-------------------------> hdr_beg
6471 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006472
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006473 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006474 for (delim = val_beg; delim < val_end; delim++)
6475 if (*delim == COOKIE_DELIM)
6476 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006477 } else {
6478 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006479 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006480 /* Now check if the cookie contains a date field, which would
6481 * appear after a vertical bar ('|') just after the server name
6482 * and before the delimiter.
6483 */
6484 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6485 if (vbar1) {
6486 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006487 * right is the last seen date. It is a base64 encoded
6488 * 30-bit value representing the UNIX date since the
6489 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006490 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006491 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006492 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006493 if (val_end - vbar1 >= 5) {
6494 val = b64tos30(vbar1);
6495 if (val > 0)
6496 txn->cookie_last_date = val << 2;
6497 }
6498 /* look for a second vertical bar */
6499 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6500 if (vbar1 && (val_end - vbar1 > 5)) {
6501 val = b64tos30(vbar1 + 1);
6502 if (val > 0)
6503 txn->cookie_first_date = val << 2;
6504 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006505 }
6506 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006507
Willy Tarreauf64d1412010-10-07 20:06:11 +02006508 /* if the cookie has an expiration date and the proxy wants to check
6509 * it, then we do that now. We first check if the cookie is too old,
6510 * then only if it has expired. We detect strict overflow because the
6511 * time resolution here is not great (4 seconds). Cookies with dates
6512 * in the future are ignored if their offset is beyond one day. This
6513 * allows an admin to fix timezone issues without expiring everyone
6514 * and at the same time avoids keeping unwanted side effects for too
6515 * long.
6516 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006517 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6518 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006519 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006520 txn->flags &= ~TX_CK_MASK;
6521 txn->flags |= TX_CK_OLD;
6522 delim = val_beg; // let's pretend we have not found the cookie
6523 txn->cookie_first_date = 0;
6524 txn->cookie_last_date = 0;
6525 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006526 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6527 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006528 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006529 txn->flags &= ~TX_CK_MASK;
6530 txn->flags |= TX_CK_EXPIRED;
6531 delim = val_beg; // let's pretend we have not found the cookie
6532 txn->cookie_first_date = 0;
6533 txn->cookie_last_date = 0;
6534 }
6535
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006536 /* Here, we'll look for the first running server which supports the cookie.
6537 * This allows to share a same cookie between several servers, for example
6538 * to dedicate backup servers to specific servers only.
6539 * However, to prevent clients from sticking to cookie-less backup server
6540 * when they have incidentely learned an empty cookie, we simply ignore
6541 * empty cookies and mark them as invalid.
6542 * The same behaviour is applied when persistence must be ignored.
6543 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006544 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006545 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006546
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006547 while (srv) {
6548 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6549 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006550 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006551 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006552 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006553 /* we found the server and we can use it */
6554 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006555 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006556 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006557 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006558 break;
6559 } else {
6560 /* we found a server, but it's down,
6561 * mark it as such and go on in case
6562 * another one is available.
6563 */
6564 txn->flags &= ~TX_CK_MASK;
6565 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006566 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006567 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006568 srv = srv->next;
6569 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006570
Willy Tarreauf64d1412010-10-07 20:06:11 +02006571 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006572 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006573 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006574 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006575 txn->flags |= TX_CK_UNUSED;
6576 else
6577 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006578 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006579
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006580 /* depending on the cookie mode, we may have to either :
6581 * - delete the complete cookie if we're in insert+indirect mode, so that
6582 * the server never sees it ;
6583 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlant5ba80252018-11-15 09:25:36 -08006584 * application cookie so that it does not get accidently removed later,
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006585 * if we're in cookie prefix mode
6586 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006587 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006588 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006589
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006590 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006591 val_end += delta;
6592 next += delta;
6593 hdr_end += delta;
6594 hdr_next += delta;
6595 cur_hdr->len += delta;
6596 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006597
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006598 del_from = NULL;
6599 preserve_hdr = 1; /* we want to keep this cookie */
6600 }
6601 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006602 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006603 del_from = prev;
6604 }
6605 } else {
6606 /* This is not our cookie, so we must preserve it. But if we already
6607 * scheduled another cookie for removal, we cannot remove the
6608 * complete header, but we can remove the previous block itself.
6609 */
6610 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006611
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006612 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006613 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006614 if (att_beg >= del_from)
6615 att_beg += delta;
6616 if (att_end >= del_from)
6617 att_end += delta;
6618 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006619 val_end += delta;
6620 next += delta;
6621 hdr_end += delta;
6622 hdr_next += delta;
6623 cur_hdr->len += delta;
6624 http_msg_move_end(&txn->req, delta);
6625 prev = del_from;
6626 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006627 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006628 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006629
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006630 /* continue with next cookie on this header line */
6631 att_beg = next;
6632 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006633
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006634 /* There are no more cookies on this line.
6635 * We may still have one (or several) marked for deletion at the
6636 * end of the line. We must do this now in two ways :
6637 * - if some cookies must be preserved, we only delete from the
6638 * mark to the end of line ;
6639 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006640 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006641 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006642 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006643 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006644 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006645 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006646 cur_hdr->len += delta;
6647 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006648 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006649
6650 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006651 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6652 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006653 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006654 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006655 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006656 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006657 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006658 }
6659
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006660 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006661 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006662 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006663}
6664
6665
Willy Tarreaua15645d2007-03-18 16:22:39 +01006666/* Iterate the same filter through all response headers contained in <rtr>.
6667 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6668 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006669int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006670{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006671 char *cur_ptr, *cur_end, *cur_next;
6672 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006673 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006674 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006675 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006676
6677 last_hdr = 0;
6678
Willy Tarreauf37954d2018-06-15 18:31:02 +02006679 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006680 old_idx = 0;
6681
6682 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006683 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006684 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006685 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006686 (exp->action == ACT_ALLOW ||
6687 exp->action == ACT_DENY))
6688 return 0;
6689
6690 cur_idx = txn->hdr_idx.v[old_idx].next;
6691 if (!cur_idx)
6692 break;
6693
6694 cur_hdr = &txn->hdr_idx.v[cur_idx];
6695 cur_ptr = cur_next;
6696 cur_end = cur_ptr + cur_hdr->len;
6697 cur_next = cur_end + cur_hdr->cr + 1;
6698
6699 /* Now we have one header between cur_ptr and cur_end,
6700 * and the next header starts at cur_next.
6701 */
6702
Willy Tarreau15a53a42015-01-21 13:39:42 +01006703 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006704 switch (exp->action) {
6705 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006706 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006707 last_hdr = 1;
6708 break;
6709
6710 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006711 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006712 last_hdr = 1;
6713 break;
6714
6715 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006716 len = exp_replace(trash.area,
6717 trash.size, cur_ptr,
6718 exp->replace, pmatch);
6719 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006720 return -1;
6721
Willy Tarreau6e27be12018-08-22 04:46:47 +02006722 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6723
Willy Tarreaua15645d2007-03-18 16:22:39 +01006724 /* FIXME: if the user adds a newline in the replacement, the
6725 * index will not be recalculated for now, and the new line
6726 * will not be counted as a new header.
6727 */
6728
6729 cur_end += delta;
6730 cur_next += delta;
6731 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006732 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006733 break;
6734
6735 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006736 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006737 cur_next += delta;
6738
Willy Tarreaufa355d42009-11-29 18:12:29 +01006739 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006740 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6741 txn->hdr_idx.used--;
6742 cur_hdr->len = 0;
6743 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006744 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006745 break;
6746
6747 }
6748 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006749
6750 /* keep the link from this header to next one in case of later
6751 * removal of next header.
6752 */
6753 old_idx = cur_idx;
6754 }
6755 return 0;
6756}
6757
6758
6759/* Apply the filter to the status line in the response buffer <rtr>.
6760 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6761 * or -1 if a replacement resulted in an invalid status line.
6762 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006763int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006764{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006765 char *cur_ptr, *cur_end;
6766 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006767 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006768 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006769
Willy Tarreau3d300592007-03-18 18:34:41 +01006770 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006771 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006772 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006773 (exp->action == ACT_ALLOW ||
6774 exp->action == ACT_DENY))
6775 return 0;
6776 else if (exp->action == ACT_REMOVE)
6777 return 0;
6778
6779 done = 0;
6780
Willy Tarreauf37954d2018-06-15 18:31:02 +02006781 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006782 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006783
6784 /* Now we have the status line between cur_ptr and cur_end */
6785
Willy Tarreau15a53a42015-01-21 13:39:42 +01006786 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006787 switch (exp->action) {
6788 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006789 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006790 done = 1;
6791 break;
6792
6793 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006794 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006795 done = 1;
6796 break;
6797
6798 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006799 len = exp_replace(trash.area, trash.size,
6800 cur_ptr, exp->replace, pmatch);
6801 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006802 return -1;
6803
Willy Tarreau6e27be12018-08-22 04:46:47 +02006804 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6805
Willy Tarreaua15645d2007-03-18 16:22:39 +01006806 /* FIXME: if the user adds a newline in the replacement, the
6807 * index will not be recalculated for now, and the new line
6808 * will not be counted as a new header.
6809 */
6810
Willy Tarreaufa355d42009-11-29 18:12:29 +01006811 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006812 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006813 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006814 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006815 cur_ptr, cur_end + 1,
6816 NULL, NULL);
6817 if (unlikely(!cur_end))
6818 return -1;
6819
6820 /* we have a full respnse and we know that we have either a CR
6821 * or an LF at <ptr>.
6822 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006823 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006824 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006825 /* there is no point trying this regex on headers */
6826 return 1;
6827 }
6828 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006829 return done;
6830}
6831
6832
6833
6834/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006835 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006836 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6837 * unparsable response.
6838 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006839int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006840{
Willy Tarreau192252e2015-04-04 01:47:55 +02006841 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006842 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006843 struct hdr_exp *exp;
6844
6845 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006846 int ret;
6847
6848 /*
6849 * The interleaving of transformations and verdicts
6850 * makes it difficult to decide to continue or stop
6851 * the evaluation.
6852 */
6853
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006854 if (txn->flags & TX_SVDENY)
6855 break;
6856
Willy Tarreau3d300592007-03-18 18:34:41 +01006857 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006858 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6859 exp->action == ACT_PASS)) {
6860 exp = exp->next;
6861 continue;
6862 }
6863
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006864 /* if this filter had a condition, evaluate it now and skip to
6865 * next filter if the condition does not match.
6866 */
6867 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006868 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006869 ret = acl_pass(ret);
6870 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6871 ret = !ret;
6872 if (!ret)
6873 continue;
6874 }
6875
Willy Tarreaua15645d2007-03-18 16:22:39 +01006876 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006877 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006878 if (unlikely(ret < 0))
6879 return -1;
6880
6881 if (likely(ret == 0)) {
6882 /* The filter did not match the response, it can be
6883 * iterated through all headers.
6884 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006885 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6886 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006887 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006888 }
6889 return 0;
6890}
6891
6892
Willy Tarreaua15645d2007-03-18 16:22:39 +01006893/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006894 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006895 * desirable to call it only when needed. This function is also used when we
6896 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006897 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006898void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006899{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006900 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006901 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006902 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006903 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006904 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006905 char *hdr_beg, *hdr_end, *hdr_next;
6906 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006907
Willy Tarreaua15645d2007-03-18 16:22:39 +01006908 /* Iterate through the headers.
6909 * we start with the start line.
6910 */
6911 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006912 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006913
6914 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6915 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006916 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006917
6918 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006919 hdr_beg = hdr_next;
6920 hdr_end = hdr_beg + cur_hdr->len;
6921 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006922
Willy Tarreau24581ba2010-08-31 22:39:35 +02006923 /* We have one full header between hdr_beg and hdr_end, and the
6924 * next header starts at hdr_next. We're only interested in
6925 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006926 */
6927
Willy Tarreau24581ba2010-08-31 22:39:35 +02006928 is_cookie2 = 0;
6929 prev = hdr_beg + 10;
6930 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006931 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006932 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6933 if (!val) {
6934 old_idx = cur_idx;
6935 continue;
6936 }
6937 is_cookie2 = 1;
6938 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006939 }
6940
Willy Tarreau24581ba2010-08-31 22:39:35 +02006941 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6942 * <prev> points to the colon.
6943 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006944 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006945
Willy Tarreau24581ba2010-08-31 22:39:35 +02006946 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6947 * check-cache is enabled) and we are not interested in checking
6948 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006949 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006950 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006951 return;
6952
Willy Tarreau24581ba2010-08-31 22:39:35 +02006953 /* OK so now we know we have to process this response cookie.
6954 * The format of the Set-Cookie header is slightly different
6955 * from the format of the Cookie header in that it does not
6956 * support the comma as a cookie delimiter (thus the header
6957 * cannot be folded) because the Expires attribute described in
6958 * the original Netscape's spec may contain an unquoted date
6959 * with a comma inside. We have to live with this because
6960 * many browsers don't support Max-Age and some browsers don't
6961 * support quoted strings. However the Set-Cookie2 header is
6962 * clean.
6963 *
6964 * We have to keep multiple pointers in order to support cookie
6965 * removal at the beginning, middle or end of header without
6966 * corrupting the header (in case of set-cookie2). A special
6967 * pointer, <scav> points to the beginning of the set-cookie-av
6968 * fields after the first semi-colon. The <next> pointer points
6969 * either to the end of line (set-cookie) or next unquoted comma
6970 * (set-cookie2). All of these headers are valid :
6971 *
6972 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6973 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6974 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6975 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6976 * | | | | | | | | | |
6977 * | | | | | | | | +-> next hdr_end <--+
6978 * | | | | | | | +------------> scav
6979 * | | | | | | +--------------> val_end
6980 * | | | | | +--------------------> val_beg
6981 * | | | | +----------------------> equal
6982 * | | | +------------------------> att_end
6983 * | | +----------------------------> att_beg
6984 * | +------------------------------> prev
6985 * +-----------------------------------------> hdr_beg
6986 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006987
Willy Tarreau24581ba2010-08-31 22:39:35 +02006988 for (; prev < hdr_end; prev = next) {
6989 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006990
Willy Tarreau24581ba2010-08-31 22:39:35 +02006991 /* find att_beg */
6992 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006993 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006994 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006995
Willy Tarreau24581ba2010-08-31 22:39:35 +02006996 /* find att_end : this is the first character after the last non
6997 * space before the equal. It may be equal to hdr_end.
6998 */
6999 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007000
Willy Tarreau24581ba2010-08-31 22:39:35 +02007001 while (equal < hdr_end) {
7002 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7003 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007004 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007005 continue;
7006 att_end = equal;
7007 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007008
Willy Tarreau24581ba2010-08-31 22:39:35 +02007009 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7010 * is between <att_beg> and <equal>, both may be identical.
7011 */
7012
7013 /* look for end of cookie if there is an equal sign */
7014 if (equal < hdr_end && *equal == '=') {
7015 /* look for the beginning of the value */
7016 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007017 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007018 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007019
Willy Tarreau24581ba2010-08-31 22:39:35 +02007020 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02007021 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007022
7023 /* make val_end point to the first white space or delimitor after the value */
7024 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007025 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007026 val_end--;
7027 } else {
7028 /* <equal> points to next comma, semi-colon or EOL */
7029 val_beg = val_end = next = equal;
7030 }
7031
7032 if (next < hdr_end) {
7033 /* Set-Cookie2 supports multiple cookies, and <next> points to
7034 * a colon or semi-colon before the end. So skip all attr-value
7035 * pairs and look for the next comma. For Set-Cookie, since
7036 * commas are permitted in values, skip to the end.
7037 */
7038 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02007039 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007040 else
7041 next = hdr_end;
7042 }
7043
7044 /* Now everything is as on the diagram above */
7045
7046 /* Ignore cookies with no equal sign */
7047 if (equal == val_end)
7048 continue;
7049
7050 /* If there are spaces around the equal sign, we need to
7051 * strip them otherwise we'll get trouble for cookie captures,
7052 * or even for rewrites. Since this happens extremely rarely,
7053 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007054 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007055 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7056 int stripped_before = 0;
7057 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007058
Willy Tarreau24581ba2010-08-31 22:39:35 +02007059 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007060 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007061 equal += stripped_before;
7062 val_beg += stripped_before;
7063 }
7064
7065 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007066 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007067 val_beg += stripped_after;
7068 stripped_before += stripped_after;
7069 }
7070
7071 val_end += stripped_before;
7072 next += stripped_before;
7073 hdr_end += stripped_before;
7074 hdr_next += stripped_before;
7075 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007076 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007077 }
7078
7079 /* First, let's see if we want to capture this cookie. We check
7080 * that we don't already have a server side cookie, because we
7081 * can only capture one. Also as an optimisation, we ignore
7082 * cookies shorter than the declared name.
7083 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007084 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007085 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007086 (val_end - att_beg >= sess->fe->capture_namelen) &&
7087 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007088 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007089 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007090 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007091 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007092 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007093 if (log_len > sess->fe->capture_len)
7094 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007095 memcpy(txn->srv_cookie, att_beg, log_len);
7096 txn->srv_cookie[log_len] = 0;
7097 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007098 }
7099
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007100 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007101 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007102 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007103 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7104 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007105 /* assume passive cookie by default */
7106 txn->flags &= ~TX_SCK_MASK;
7107 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007108
7109 /* If the cookie is in insert mode on a known server, we'll delete
7110 * this occurrence because we'll insert another one later.
7111 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007112 * a direct access.
7113 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007114 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007115 /* The "preserve" flag was set, we don't want to touch the
7116 * server's cookie.
7117 */
7118 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007119 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007120 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007121 /* this cookie must be deleted */
7122 if (*prev == ':' && next == hdr_end) {
7123 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007124 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007125 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7126 txn->hdr_idx.used--;
7127 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007128 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007129 hdr_next += delta;
7130 http_msg_move_end(&txn->rsp, delta);
7131 /* note: while both invalid now, <next> and <hdr_end>
7132 * are still equal, so the for() will stop as expected.
7133 */
7134 } else {
7135 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007136 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007137 next = prev;
7138 hdr_end += delta;
7139 hdr_next += delta;
7140 cur_hdr->len += delta;
7141 http_msg_move_end(&txn->rsp, delta);
7142 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007143 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007144 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007145 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007146 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007147 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007148 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007149 * with this server since we know it.
7150 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007151 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007152 next += delta;
7153 hdr_end += delta;
7154 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007155 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007156 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007157
Willy Tarreauf1348312010-10-07 15:54:11 +02007158 txn->flags &= ~TX_SCK_MASK;
7159 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007160 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007161 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007162 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007163 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007164 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007165 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007166 next += delta;
7167 hdr_end += delta;
7168 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007169 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007170 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007171
Willy Tarreau827aee92011-03-10 16:55:02 +01007172 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007173 txn->flags &= ~TX_SCK_MASK;
7174 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007175 }
7176 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007177 /* that's done for this cookie, check the next one on the same
7178 * line when next != hdr_end (only if is_cookie2).
7179 */
7180 }
7181 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007182 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007183 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007184}
7185
7186
Willy Tarreaua15645d2007-03-18 16:22:39 +01007187/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007188 * Parses the Cache-Control and Pragma request header fields to determine if
7189 * the request may be served from the cache and/or if it is cacheable. Updates
7190 * s->txn->flags.
7191 */
7192void check_request_for_cacheability(struct stream *s, struct channel *chn)
7193{
7194 struct http_txn *txn = s->txn;
7195 char *p1, *p2;
7196 char *cur_ptr, *cur_end, *cur_next;
7197 int pragma_found;
7198 int cc_found;
7199 int cur_idx;
7200
Christopher Faulet25a02f62018-10-24 12:00:25 +02007201 if (IS_HTX_STRM(s))
7202 return htx_check_request_for_cacheability(s, chn);
7203
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007204 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7205 return; /* nothing more to do here */
7206
7207 cur_idx = 0;
7208 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007209 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007210
7211 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7212 struct hdr_idx_elem *cur_hdr;
7213 int val;
7214
7215 cur_hdr = &txn->hdr_idx.v[cur_idx];
7216 cur_ptr = cur_next;
7217 cur_end = cur_ptr + cur_hdr->len;
7218 cur_next = cur_end + cur_hdr->cr + 1;
7219
7220 /* We have one full header between cur_ptr and cur_end, and the
7221 * next header starts at cur_next.
7222 */
7223
7224 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7225 if (val) {
7226 if ((cur_end - (cur_ptr + val) >= 8) &&
7227 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7228 pragma_found = 1;
7229 continue;
7230 }
7231 }
7232
William Lallemand8a16fe02018-05-22 11:04:33 +02007233 /* Don't use the cache and don't try to store if we found the
7234 * Authorization header */
7235 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7236 if (val) {
7237 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7238 txn->flags |= TX_CACHE_IGNORE;
7239 continue;
7240 }
7241
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007242 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7243 if (!val)
7244 continue;
7245
7246 /* OK, right now we know we have a cache-control header at cur_ptr */
7247 cc_found = 1;
7248 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7249
7250 if (p1 >= cur_end) /* no more info */
7251 continue;
7252
7253 /* p1 is at the beginning of the value */
7254 p2 = p1;
7255 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7256 p2++;
7257
7258 /* we have a complete value between p1 and p2. We don't check the
7259 * values after max-age, max-stale nor min-fresh, we simply don't
7260 * use the cache when they're specified.
7261 */
7262 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7263 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7264 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7265 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7266 txn->flags |= TX_CACHE_IGNORE;
7267 continue;
7268 }
7269
7270 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7271 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7272 continue;
7273 }
7274 }
7275
7276 /* RFC7234#5.4:
7277 * When the Cache-Control header field is also present and
7278 * understood in a request, Pragma is ignored.
7279 * When the Cache-Control header field is not present in a
7280 * request, caches MUST consider the no-cache request
7281 * pragma-directive as having the same effect as if
7282 * "Cache-Control: no-cache" were present.
7283 */
7284 if (!cc_found && pragma_found)
7285 txn->flags |= TX_CACHE_IGNORE;
7286}
7287
7288/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007289 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007290 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007291void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007292{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007293 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007294 char *p1, *p2;
7295
7296 char *cur_ptr, *cur_end, *cur_next;
7297 int cur_idx;
7298
Christopher Faulet25a02f62018-10-24 12:00:25 +02007299
7300 if (IS_HTX_STRM(s))
7301 return htx_check_response_for_cacheability(s, rtr);
7302
Willy Tarreau12b32f22017-12-21 16:08:09 +01007303 if (txn->status < 200) {
7304 /* do not try to cache interim responses! */
7305 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007306 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007307 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007308
7309 /* Iterate through the headers.
7310 * we start with the start line.
7311 */
7312 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007313 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007314
7315 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7316 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007317 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007318
7319 cur_hdr = &txn->hdr_idx.v[cur_idx];
7320 cur_ptr = cur_next;
7321 cur_end = cur_ptr + cur_hdr->len;
7322 cur_next = cur_end + cur_hdr->cr + 1;
7323
7324 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007325 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007326 */
7327
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007328 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7329 if (val) {
7330 if ((cur_end - (cur_ptr + val) >= 8) &&
7331 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7332 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7333 return;
7334 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007335 }
7336
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007337 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7338 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007339 continue;
7340
7341 /* OK, right now we know we have a cache-control header at cur_ptr */
7342
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007343 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007344
7345 if (p1 >= cur_end) /* no more info */
7346 continue;
7347
7348 /* p1 is at the beginning of the value */
7349 p2 = p1;
7350
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007351 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007352 p2++;
7353
7354 /* we have a complete value between p1 and p2 */
7355 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007356 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7357 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7358 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7359 continue;
7360 }
7361
Willy Tarreaua15645d2007-03-18 16:22:39 +01007362 /* we have something of the form no-cache="set-cookie" */
7363 if ((cur_end - p1 >= 21) &&
7364 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7365 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007366 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007367 continue;
7368 }
7369
7370 /* OK, so we know that either p2 points to the end of string or to a comma */
7371 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007372 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007373 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007374 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007375 return;
7376 }
7377
7378 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007379 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007380 continue;
7381 }
7382 }
7383}
7384
Willy Tarreau58f10d72006-12-04 02:26:12 +01007385
Willy Tarreaub2513902006-12-17 14:52:38 +01007386/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007387 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007388 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007389 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007390 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007391 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007392 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007393 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007394 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007395int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007396{
7397 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007398 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007399 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007400
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007401 if (!uri_auth)
7402 return 0;
7403
Cyril Bonté70be45d2010-10-12 00:14:35 +02007404 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007405 return 0;
7406
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007407 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007408 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007409 return 0;
7410
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007411 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007412 return 0;
7413
Willy Tarreaub2513902006-12-17 14:52:38 +01007414 return 1;
7415}
7416
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007417/* Append the description of what is present in error snapshot <es> into <out>.
7418 * The description must be small enough to always fit in a trash. The output
7419 * buffer may be the trash so the trash must not be used inside this function.
7420 */
7421void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7422{
7423 chunk_appendf(&trash,
7424 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7425 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7426 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7427 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7428 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7429 es->ctx.http.m_flags, es->ctx.http.m_clen,
7430 es->ctx.http.m_blen, es->ctx.http.b_flags);
7431}
7432
Willy Tarreau4076a152009-04-02 15:18:36 +02007433/*
7434 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007435 * By default it tries to report the error position as msg->err_pos. However if
7436 * this one is not set, it will then report msg->next, which is the last known
7437 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007438 * displays buffers as a contiguous area starting at buf->p. The direction is
7439 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007440 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007441void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007442 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007443 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007444{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007445 union error_snapshot_ctx ctx;
7446 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007447
Willy Tarreauef3ca732018-09-07 15:47:35 +02007448 /* http-specific part now */
7449 ctx.http.sid = s->uniq_id;
7450 ctx.http.state = state;
7451 ctx.http.b_flags = msg->chn->flags;
7452 ctx.http.s_flags = s->flags;
7453 ctx.http.t_flags = s->txn->flags;
7454 ctx.http.m_flags = msg->flags;
7455 ctx.http.m_clen = msg->chunk_len;
7456 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007457
Willy Tarreauef3ca732018-09-07 15:47:35 +02007458 ofs = msg->chn->total - ci_data(msg->chn);
7459 if (ofs < 0)
7460 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007461
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007462 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007463 other_end, s->target,
7464 strm_sess(s), &msg->chn->buf,
7465 ofs, co_data(msg->chn),
7466 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7467 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007468}
Willy Tarreaub2513902006-12-17 14:52:38 +01007469
Willy Tarreau294c4732011-12-16 21:35:50 +01007470/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7471 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7472 * performed over the whole headers. Otherwise it must contain a valid header
7473 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7474 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7475 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7476 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007477 * -1. The value fetch stops at commas, so this function is suited for use with
7478 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007479 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007480 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007481unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007482 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007483 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007484{
Willy Tarreau294c4732011-12-16 21:35:50 +01007485 struct hdr_ctx local_ctx;
7486 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007487 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007488 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007489 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007490
Willy Tarreau294c4732011-12-16 21:35:50 +01007491 if (!ctx) {
7492 local_ctx.idx = 0;
7493 ctx = &local_ctx;
7494 }
7495
Willy Tarreaubce70882009-09-07 11:51:47 +02007496 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007497 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007498 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007499 occ--;
7500 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007501 *vptr = ctx->line + ctx->val;
7502 *vlen = ctx->vlen;
7503 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007504 }
7505 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007506 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007507 }
7508
7509 /* negative occurrence, we scan all the list then walk back */
7510 if (-occ > MAX_HDR_HISTORY)
7511 return 0;
7512
Willy Tarreau294c4732011-12-16 21:35:50 +01007513 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007514 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007515 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7516 len_hist[hist_ptr] = ctx->vlen;
7517 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007518 hist_ptr = 0;
7519 found++;
7520 }
7521 if (-occ > found)
7522 return 0;
7523 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007524 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7525 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7526 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007527 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007528 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007529 if (hist_ptr >= MAX_HDR_HISTORY)
7530 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007531 *vptr = ptr_hist[hist_ptr];
7532 *vlen = len_hist[hist_ptr];
7533 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007534}
7535
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007536/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7537 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7538 * performed over the whole headers. Otherwise it must contain a valid header
7539 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7540 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7541 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7542 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7543 * -1. This function differs from http_get_hdr() in that it only returns full
7544 * line header values and does not stop at commas.
7545 * The return value is 0 if nothing was found, or non-zero otherwise.
7546 */
7547unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7548 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007549 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007550{
7551 struct hdr_ctx local_ctx;
7552 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007553 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007554 unsigned int hist_ptr;
7555 int found;
7556
7557 if (!ctx) {
7558 local_ctx.idx = 0;
7559 ctx = &local_ctx;
7560 }
7561
7562 if (occ >= 0) {
7563 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007564 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007565 occ--;
7566 if (occ <= 0) {
7567 *vptr = ctx->line + ctx->val;
7568 *vlen = ctx->vlen;
7569 return 1;
7570 }
7571 }
7572 return 0;
7573 }
7574
7575 /* negative occurrence, we scan all the list then walk back */
7576 if (-occ > MAX_HDR_HISTORY)
7577 return 0;
7578
7579 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007580 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007581 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7582 len_hist[hist_ptr] = ctx->vlen;
7583 if (++hist_ptr >= MAX_HDR_HISTORY)
7584 hist_ptr = 0;
7585 found++;
7586 }
7587 if (-occ > found)
7588 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007589
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007590 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007591 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7592 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7593 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007594 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007595 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007596 if (hist_ptr >= MAX_HDR_HISTORY)
7597 hist_ptr -= MAX_HDR_HISTORY;
7598 *vptr = ptr_hist[hist_ptr];
7599 *vlen = len_hist[hist_ptr];
7600 return 1;
7601}
7602
Willy Tarreaubaaee002006-06-26 02:48:02 +02007603/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007604 * Print a debug line with a header. Always stop at the first CR or LF char,
7605 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7606 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007607 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007608void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007609{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007610 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007611 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007612
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007613 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007614 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007615 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007616 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 +02007617
7618 for (max = 0; start + max < end; max++)
7619 if (start[max] == '\r' || start[max] == '\n')
7620 break;
7621
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007622 UBOUND(max, trash.size - trash.data - 3);
7623 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7624 trash.area[trash.data++] = '\n';
7625 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007626}
7627
Willy Tarreaueee5b512015-04-03 23:46:31 +02007628
7629/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7630 * The hdr_idx is allocated as well. In case of allocation failure, everything
7631 * allocated is freed and NULL is returned. Otherwise the new transaction is
7632 * assigned to the stream and returned.
7633 */
7634struct http_txn *http_alloc_txn(struct stream *s)
7635{
7636 struct http_txn *txn = s->txn;
7637
7638 if (txn)
7639 return txn;
7640
Willy Tarreaubafbe012017-11-24 17:34:44 +01007641 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007642 if (!txn)
7643 return txn;
7644
7645 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007646 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007647 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007648 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007649 return NULL;
7650 }
7651
7652 s->txn = txn;
7653 return txn;
7654}
7655
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007656void http_txn_reset_req(struct http_txn *txn)
7657{
7658 txn->req.flags = 0;
7659 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7660 txn->req.next = 0;
7661 txn->req.chunk_len = 0LL;
7662 txn->req.body_len = 0LL;
7663 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7664}
7665
7666void http_txn_reset_res(struct http_txn *txn)
7667{
7668 txn->rsp.flags = 0;
7669 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7670 txn->rsp.next = 0;
7671 txn->rsp.chunk_len = 0LL;
7672 txn->rsp.body_len = 0LL;
7673 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7674}
7675
Willy Tarreau0937bc42009-12-22 15:03:09 +01007676/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007677 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007678 * the required fields are properly allocated and that we only need to (re)init
7679 * them. This should be used before processing any new request.
7680 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007681void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007682{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007683 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007684 struct proxy *fe = strm_fe(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02007685 struct conn_stream *cs = objt_cs(s->si[0].end);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007686
Christopher Fauletf2824e62018-10-01 12:12:37 +02007687 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
7688 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
7689 : 0);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007690 txn->status = -1;
7691
Willy Tarreauf64d1412010-10-07 20:06:11 +02007692 txn->cookie_first_date = 0;
7693 txn->cookie_last_date = 0;
7694
Willy Tarreaueee5b512015-04-03 23:46:31 +02007695 txn->srv_cookie = NULL;
7696 txn->cli_cookie = NULL;
7697 txn->uri = NULL;
7698
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007699 http_txn_reset_req(txn);
7700 http_txn_reset_res(txn);
7701
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007702 txn->req.chn = &s->req;
7703 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007704
7705 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007706
7707 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7708 if (fe->options2 & PR_O2_REQBUG_OK)
7709 txn->req.err_pos = -1; /* let buggy requests pass */
7710
Willy Tarreau0937bc42009-12-22 15:03:09 +01007711 if (txn->hdr_idx.v)
7712 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007713
7714 vars_init(&s->vars_txn, SCOPE_TXN);
7715 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007716}
7717
7718/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007719void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007720{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007721 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007722 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007723
7724 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007725 pool_free(pool_head_requri, txn->uri);
7726 pool_free(pool_head_capture, txn->cli_cookie);
7727 pool_free(pool_head_capture, txn->srv_cookie);
7728 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007729
William Lallemanda73203e2012-03-12 12:48:57 +01007730 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007731 txn->uri = NULL;
7732 txn->srv_cookie = NULL;
7733 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007734
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007735 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007736 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007737 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007738 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007739 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007740 }
7741
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007742 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007743 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007744 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007745 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007746 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007747 }
7748
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007749 if (!LIST_ISEMPTY(&s->vars_txn.head))
7750 vars_prune(&s->vars_txn, s->sess, s);
7751 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7752 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007753}
7754
7755/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007756void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007757{
7758 http_end_txn(s);
7759 http_init_txn(s);
7760
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007761 /* reinitialise the current rule list pointer to NULL. We are sure that
7762 * any rulelist match the NULL pointer.
7763 */
7764 s->current_rule_list = NULL;
7765
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007766 s->be = strm_fe(s);
7767 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007768 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007769 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007770 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007771 /* re-init store persistence */
7772 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007773 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007774
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007775 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007776
Willy Tarreau739cfba2010-01-25 23:11:14 +01007777 /* We must trim any excess data from the response buffer, because we
7778 * may have blocked an invalid response from a server that we don't
Joseph Herlant5ba80252018-11-15 09:25:36 -08007779 * want to accidently forward once we disable the analysers, nor do
Willy Tarreau739cfba2010-01-25 23:11:14 +01007780 * we want those data to come along with next response. A typical
7781 * example of such data would be from a buggy server responding to
7782 * a HEAD with some data, or sending more than the advertised
7783 * content-length.
7784 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007785 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007786 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007787
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007788 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007789 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007790
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007791 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007792 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007793
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007794 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007795 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007796
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007797 s->req.rex = TICK_ETERNITY;
7798 s->req.wex = TICK_ETERNITY;
7799 s->req.analyse_exp = TICK_ETERNITY;
7800 s->res.rex = TICK_ETERNITY;
7801 s->res.wex = TICK_ETERNITY;
7802 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007803 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007804}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007805
Willy Tarreau79e57332018-10-02 16:01:16 +02007806/* This function executes one of the set-{method,path,query,uri} actions. It
7807 * takes the string from the variable 'replace' with length 'len', then modifies
7808 * the relevant part of the request line accordingly. Then it updates various
7809 * pointers to the next elements which were moved, and the total buffer length.
7810 * It finds the action to be performed in p[2], previously filled by function
7811 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7812 * error, though this can be revisited when this code is finally exploited.
7813 *
7814 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7815 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007816 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007817 * In query string case, the mark question '?' must be set at the start of the
7818 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007819 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007820int http_replace_req_line(int action, const char *replace, int len,
7821 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007822{
Willy Tarreau79e57332018-10-02 16:01:16 +02007823 struct http_txn *txn = s->txn;
7824 char *cur_ptr, *cur_end;
7825 int offset = 0;
7826 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007827
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007828 if (IS_HTX_STRM(s))
7829 return htx_req_replace_stline(action, replace, len, px, s);
7830
Willy Tarreau79e57332018-10-02 16:01:16 +02007831 switch (action) {
7832 case 0: // method
7833 cur_ptr = ci_head(&s->req);
7834 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007835
Willy Tarreau79e57332018-10-02 16:01:16 +02007836 /* adjust req line offsets and lengths */
7837 delta = len - offset - (cur_end - cur_ptr);
7838 txn->req.sl.rq.m_l += delta;
7839 txn->req.sl.rq.u += delta;
7840 txn->req.sl.rq.v += delta;
7841 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007842
Willy Tarreau79e57332018-10-02 16:01:16 +02007843 case 1: // path
7844 cur_ptr = http_txn_get_path(txn);
7845 if (!cur_ptr)
7846 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007847
Willy Tarreau79e57332018-10-02 16:01:16 +02007848 cur_end = cur_ptr;
7849 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7850 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007851
Willy Tarreau79e57332018-10-02 16:01:16 +02007852 /* adjust req line offsets and lengths */
7853 delta = len - offset - (cur_end - cur_ptr);
7854 txn->req.sl.rq.u_l += delta;
7855 txn->req.sl.rq.v += delta;
7856 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007857
Willy Tarreau79e57332018-10-02 16:01:16 +02007858 case 2: // query
7859 offset = 1;
7860 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7861 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7862 while (cur_ptr < cur_end && *cur_ptr != '?')
7863 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007864
Willy Tarreau79e57332018-10-02 16:01:16 +02007865 /* skip the question mark or indicate that we must insert it
7866 * (but only if the format string is not empty then).
7867 */
7868 if (cur_ptr < cur_end)
7869 cur_ptr++;
7870 else if (len > 1)
7871 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007872
Willy Tarreau79e57332018-10-02 16:01:16 +02007873 /* adjust req line offsets and lengths */
7874 delta = len - offset - (cur_end - cur_ptr);
7875 txn->req.sl.rq.u_l += delta;
7876 txn->req.sl.rq.v += delta;
7877 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007878
Willy Tarreau79e57332018-10-02 16:01:16 +02007879 case 3: // uri
7880 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7881 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007882
Willy Tarreau79e57332018-10-02 16:01:16 +02007883 /* adjust req line offsets and lengths */
7884 delta = len - offset - (cur_end - cur_ptr);
7885 txn->req.sl.rq.u_l += delta;
7886 txn->req.sl.rq.v += delta;
7887 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007888
Willy Tarreau79e57332018-10-02 16:01:16 +02007889 default:
7890 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007891 }
7892
Willy Tarreau79e57332018-10-02 16:01:16 +02007893 /* commit changes and adjust end of message */
7894 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7895 txn->req.sl.rq.l += delta;
7896 txn->hdr_idx.v[0].len += delta;
7897 http_msg_move_end(&txn->req, delta);
7898 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007899}
7900
Willy Tarreau79e57332018-10-02 16:01:16 +02007901/* This function replace the HTTP status code and the associated message. The
7902 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007903 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007904void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007905{
Willy Tarreau79e57332018-10-02 16:01:16 +02007906 struct http_txn *txn = s->txn;
7907 char *cur_ptr, *cur_end;
7908 int delta;
7909 char *res;
7910 int c_l;
7911 const char *msg = reason;
7912 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007913
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007914 if (IS_HTX_STRM(s))
7915 return htx_res_set_status(status, reason, s);
7916
Willy Tarreau79e57332018-10-02 16:01:16 +02007917 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007918
Willy Tarreau79e57332018-10-02 16:01:16 +02007919 res = ultoa_o(status, trash.area, trash.size);
7920 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007921
Willy Tarreau79e57332018-10-02 16:01:16 +02007922 trash.area[c_l] = ' ';
7923 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007924
Willy Tarreau79e57332018-10-02 16:01:16 +02007925 /* Do we have a custom reason format string? */
7926 if (msg == NULL)
7927 msg = http_get_reason(status);
7928 msg_len = strlen(msg);
7929 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7930 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007931
Willy Tarreau79e57332018-10-02 16:01:16 +02007932 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7933 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007934
Willy Tarreau79e57332018-10-02 16:01:16 +02007935 /* commit changes and adjust message */
7936 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7937 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007938
Willy Tarreau79e57332018-10-02 16:01:16 +02007939 /* adjust res line offsets and lengths */
7940 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7941 txn->rsp.sl.st.c_l = c_l;
7942 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007943
Willy Tarreau79e57332018-10-02 16:01:16 +02007944 delta = trash.data - (cur_end - cur_ptr);
7945 txn->rsp.sl.st.l += delta;
7946 txn->hdr_idx.v[0].len += delta;
7947 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007948}
7949
Willy Tarreau58f10d72006-12-04 02:26:12 +01007950/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007951 * Local variables:
7952 * c-indent-level: 8
7953 * c-basic-offset: 8
7954 * End:
7955 */