blob: ec5140981e80421cfa8a4ff73b0917c981ce3022 [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
90
Willy Tarreau87b09662015-04-03 00:22:06 +020091static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +020092
David Carlier7365f7d2016-04-04 11:54:42 +010093static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
94static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +010095
Willy Tarreau80587432006-12-24 17:47:20 +010096void init_proto_http()
97{
Willy Tarreau332f8bf2007-05-13 21:36:56 +020098 /* memory allocations */
Willy Tarreaubafbe012017-11-24 17:34:44 +010099 pool_head_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
100 pool_head_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100101}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102
Willy Tarreau53b6c742006-12-17 13:37:46 +0100103/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100104 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +0200105 * the last CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100106 * The header is also automatically added to the index <hdr_idx>, and the end
107 * of headers is automatically adjusted. The number of bytes added is returned
108 * on success, otherwise <0 is returned indicating an error.
109 */
Willy Tarreau4d893d42018-07-12 15:43:32 +0200110static inline int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100111{
Willy Tarreau4d893d42018-07-12 15:43:32 +0200112 return http_header_add_tail2(msg, hdr_idx, text, strlen(text));
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100113}
114
115/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100116 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +0200117 * the last CRLF. <len> bytes are copied, not counting the CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100118 * The header is also automatically added to the index <hdr_idx>, and the end
119 * of headers is automatically adjusted. The number of bytes added is returned
120 * on success, otherwise <0 is returned indicating an error.
121 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100122int http_header_add_tail2(struct http_msg *msg,
123 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100124{
125 int bytes;
126
Willy Tarreau4d893d42018-07-12 15:43:32 +0200127 bytes = ci_insert_line2(msg->chn, msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100128 if (!bytes)
129 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100130 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100131 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
132}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200133
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200134/* Find the first or next occurrence of header <name> in message buffer <sol>
135 * using headers index <idx>, and return it in the <ctx> structure. This
136 * structure holds everything necessary to use the header and find next
137 * occurrence. If its <idx> member is 0, the header is searched from the
138 * beginning. Otherwise, the next occurrence is returned. The function returns
139 * 1 when it finds a value, and 0 when there is no more. It is very similar to
140 * http_find_header2() except that it is designed to work with full-line headers
141 * whose comma is not a delimiter but is part of the syntax. As a special case,
142 * if ctx->val is NULL when searching for a new values of a header, the current
143 * header is rescanned. This allows rescanning after a header deletion.
144 */
145int http_find_full_header2(const char *name, int len,
146 char *sol, struct hdr_idx *idx,
147 struct hdr_ctx *ctx)
148{
149 char *eol, *sov;
150 int cur_idx, old_idx;
151
152 cur_idx = ctx->idx;
153 if (cur_idx) {
154 /* We have previously returned a header, let's search another one */
155 sol = ctx->line;
156 eol = sol + idx->v[cur_idx].len;
157 goto next_hdr;
158 }
159
160 /* first request for this header */
161 sol += hdr_idx_first_pos(idx);
162 old_idx = 0;
163 cur_idx = hdr_idx_first_idx(idx);
164 while (cur_idx) {
165 eol = sol + idx->v[cur_idx].len;
166
167 if (len == 0) {
168 /* No argument was passed, we want any header.
169 * To achieve this, we simply build a fake request. */
170 while (sol + len < eol && sol[len] != ':')
171 len++;
172 name = sol;
173 }
174
175 if ((len < eol - sol) &&
176 (sol[len] == ':') &&
177 (strncasecmp(sol, name, len) == 0)) {
178 ctx->del = len;
179 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100180 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200181 sov++;
182
183 ctx->line = sol;
184 ctx->prev = old_idx;
185 ctx->idx = cur_idx;
186 ctx->val = sov - sol;
187 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100188 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200189 eol--;
190 ctx->tws++;
191 }
192 ctx->vlen = eol - sov;
193 return 1;
194 }
195 next_hdr:
196 sol = eol + idx->v[cur_idx].cr + 1;
197 old_idx = cur_idx;
198 cur_idx = idx->v[cur_idx].next;
199 }
200 return 0;
201}
202
Willy Tarreauc90dc232015-02-20 13:51:36 +0100203/* Find the first or next header field in message buffer <sol> using headers
204 * index <idx>, and return it in the <ctx> structure. This structure holds
205 * everything necessary to use the header and find next occurrence. If its
206 * <idx> member is 0, the first header is retrieved. Otherwise, the next
207 * occurrence is returned. The function returns 1 when it finds a value, and
208 * 0 when there is no more. It is equivalent to http_find_full_header2() with
209 * no header name.
210 */
211int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
212{
213 char *eol, *sov;
214 int cur_idx, old_idx;
215 int len;
216
217 cur_idx = ctx->idx;
218 if (cur_idx) {
219 /* We have previously returned a header, let's search another one */
220 sol = ctx->line;
221 eol = sol + idx->v[cur_idx].len;
222 goto next_hdr;
223 }
224
225 /* first request for this header */
226 sol += hdr_idx_first_pos(idx);
227 old_idx = 0;
228 cur_idx = hdr_idx_first_idx(idx);
229 while (cur_idx) {
230 eol = sol + idx->v[cur_idx].len;
231
232 len = 0;
233 while (1) {
234 if (len >= eol - sol)
235 goto next_hdr;
236 if (sol[len] == ':')
237 break;
238 len++;
239 }
240
241 ctx->del = len;
242 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100243 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100244 sov++;
245
246 ctx->line = sol;
247 ctx->prev = old_idx;
248 ctx->idx = cur_idx;
249 ctx->val = sov - sol;
250 ctx->tws = 0;
251
Willy Tarreau2235b262016-11-05 15:50:20 +0100252 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100253 eol--;
254 ctx->tws++;
255 }
256 ctx->vlen = eol - sov;
257 return 1;
258
259 next_hdr:
260 sol = eol + idx->v[cur_idx].cr + 1;
261 old_idx = cur_idx;
262 cur_idx = idx->v[cur_idx].next;
263 }
264 return 0;
265}
266
Willy Tarreau33a7e692007-06-10 19:45:56 +0200267/* Find the first or next occurrence of header <name> in message buffer <sol>
268 * using headers index <idx>, and return it in the <ctx> structure. This
269 * structure holds everything necessary to use the header and find next
270 * occurrence. If its <idx> member is 0, the header is searched from the
271 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100272 * 1 when it finds a value, and 0 when there is no more. It is designed to work
273 * with headers defined as comma-separated lists. As a special case, if ctx->val
274 * is NULL when searching for a new values of a header, the current header is
275 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200276 */
277int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100278 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200279 struct hdr_ctx *ctx)
280{
Willy Tarreau68085d82010-01-18 14:54:04 +0100281 char *eol, *sov;
282 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200283
Willy Tarreau68085d82010-01-18 14:54:04 +0100284 cur_idx = ctx->idx;
285 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200286 /* We have previously returned a value, let's search
287 * another one on the same line.
288 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200289 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200290 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100291 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200292 eol = sol + idx->v[cur_idx].len;
293
294 if (sov >= eol)
295 /* no more values in this header */
296 goto next_hdr;
297
Willy Tarreau68085d82010-01-18 14:54:04 +0100298 /* values remaining for this header, skip the comma but save it
299 * for later use (eg: for header deletion).
300 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200301 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100302 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200303 sov++;
304
305 goto return_hdr;
306 }
307
308 /* first request for this header */
309 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100310 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200311 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200312 while (cur_idx) {
313 eol = sol + idx->v[cur_idx].len;
314
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200315 if (len == 0) {
316 /* No argument was passed, we want any header.
317 * To achieve this, we simply build a fake request. */
318 while (sol + len < eol && sol[len] != ':')
319 len++;
320 name = sol;
321 }
322
Willy Tarreau33a7e692007-06-10 19:45:56 +0200323 if ((len < eol - sol) &&
324 (sol[len] == ':') &&
325 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100326 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200327 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100328 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200329 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100330
Willy Tarreau33a7e692007-06-10 19:45:56 +0200331 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100332 ctx->prev = old_idx;
333 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200334 ctx->idx = cur_idx;
335 ctx->val = sov - sol;
336
Willy Tarreauab813a42018-09-10 18:41:28 +0200337 eol = http_find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200338 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100339 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200340 eol--;
341 ctx->tws++;
342 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200343 ctx->vlen = eol - sov;
344 return 1;
345 }
346 next_hdr:
347 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100348 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200349 cur_idx = idx->v[cur_idx].next;
350 }
351 return 0;
352}
353
354int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100355 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200356 struct hdr_ctx *ctx)
357{
358 return http_find_header2(name, strlen(name), sol, idx, ctx);
359}
360
Willy Tarreau68085d82010-01-18 14:54:04 +0100361/* Remove one value of a header. This only works on a <ctx> returned by one of
362 * the http_find_header functions. The value is removed, as well as surrounding
363 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100364 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100365 * message <msg>. The new index is returned. If it is zero, it means there is
366 * no more header, so any processing may stop. The ctx is always left in a form
367 * that can be handled by http_find_header2() to find next occurrence.
368 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100369int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100370{
371 int cur_idx = ctx->idx;
372 char *sol = ctx->line;
373 struct hdr_idx_elem *hdr;
374 int delta, skip_comma;
375
376 if (!cur_idx)
377 return 0;
378
379 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200380 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100381 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200382 delta = b_rep_blk(&msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100383 http_msg_move_end(msg, delta);
384 idx->used--;
385 hdr->len = 0; /* unused entry */
386 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100387 if (idx->tail == ctx->idx)
388 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100389 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100390 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100391 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200392 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100393 return ctx->idx;
394 }
395
396 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200397 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
398 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100399 */
400
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200401 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200402 delta = b_rep_blk(&msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200403 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100404 NULL, 0);
405 hdr->len += delta;
406 http_msg_move_end(msg, delta);
407 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200408 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100409 return ctx->idx;
410}
411
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100412/* This function handles a server error at the stream interface level. The
413 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100414 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100415 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100416 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200417 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200418static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau83061a82018-07-13 11:56:34 +0200419 int err, int finst, const struct buffer *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200420{
Willy Tarreau2019f952017-03-14 11:07:31 +0100421 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100422 channel_auto_read(si_oc(si));
423 channel_abort(si_oc(si));
424 channel_auto_close(si_oc(si));
425 channel_erase(si_oc(si));
426 channel_auto_close(si_ic(si));
427 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200428 if (msg)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200429 co_inject(si_ic(si), msg->area, msg->data);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200430 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200431 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200432 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200433 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200434}
435
Willy Tarreau87b09662015-04-03 00:22:06 +0200436/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100437 * and message.
438 */
439
Willy Tarreau83061a82018-07-13 11:56:34 +0200440struct buffer *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100441{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200442 const int msgnum = http_get_status_idx(s->txn->status);
443
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200444 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200445 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200446 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200447 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100448 else
449 return &http_err_chunks[msgnum];
450}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200451
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100452void
Willy Tarreau83061a82018-07-13 11:56:34 +0200453http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100454{
Christopher Fauletd7c91962015-04-30 11:48:27 +0200455 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100456 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100457 stream_int_retnclose(&s->si[0], msg);
458}
459
Willy Tarreau21d2af32008-02-14 20:25:24 +0100460/* Parse the URI from the given transaction (which is assumed to be in request
461 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
462 * It is returned otherwise.
463 */
Willy Tarreau6b952c82018-09-10 17:45:34 +0200464char *http_txn_get_path(const struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100465{
Willy Tarreau6b952c82018-09-10 17:45:34 +0200466 struct ist ret;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100467
Willy Tarreau6b952c82018-09-10 17:45:34 +0200468 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 +0100469
Willy Tarreau6b952c82018-09-10 17:45:34 +0200470 return ret.ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +0100471}
472
Willy Tarreau71241ab2012-12-27 11:30:54 +0100473/* Returns a 302 for a redirectable request that reaches a server working in
474 * in redirect mode. This may only be called just after the stream interface
475 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
476 * follow normal proxy processing. NOTE: this function is designed to support
477 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100478 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200479void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100480{
481 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100482 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100483 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200484 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100485
486 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200487 trash.data = strlen(HTTP_302);
488 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100489
Willy Tarreaub05e48a2018-09-20 11:12:58 +0200490 srv = __objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100491
Willy Tarreauefb453c2008-10-26 20:49:47 +0100492 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200493 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100494 return;
495
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100496 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100497 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200498 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
499 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100500 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100501
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200502 /* 3: add the request URI. Since it was already forwarded, we need
503 * to temporarily rewind the buffer.
504 */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200505 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200506 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200507
Willy Tarreau6b952c82018-09-10 17:45:34 +0200508 path = http_txn_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200509 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 +0200510
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200511 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200512
Willy Tarreauefb453c2008-10-26 20:49:47 +0100513 if (!path)
514 return;
515
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200516 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100517 return;
518
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200519 memcpy(trash.area + trash.data, path, len);
520 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100521
522 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200523 memcpy(trash.area + trash.data,
524 "\r\nProxy-Connection: close\r\n\r\n", 29);
525 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100526 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200527 memcpy(trash.area + trash.data,
528 "\r\nConnection: close\r\n\r\n", 23);
529 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100530 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100531
532 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200533 si_shutr(si);
534 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100535 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100536 si->state = SI_ST_CLO;
537
538 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +0100539 txn->status = 302;
540 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100541
542 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100543 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500544 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100545}
546
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100547/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100548 * that the server side is closed. Note that err_type is actually a
549 * bitmask, where almost only aborts may be cumulated with other
550 * values. We consider that aborted operations are more important
551 * than timeouts or errors due to the fact that nobody else in the
552 * logs might explain incomplete retries. All others should avoid
553 * being cumulated. It should normally not be possible to have multiple
554 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100555 * Note that connection errors appearing on the second request of a keep-alive
556 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100557 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200558void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100559{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100560 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100561
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200562 /* set s->txn->status for http_error_message(s) */
563 s->txn->status = 503;
564
Willy Tarreauefb453c2008-10-26 20:49:47 +0100565 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200566 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100567 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100568 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200569 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100570 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200571 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100572 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200573 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100574 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100575 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200576 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100577 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100578 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200579 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100580 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200581 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100582 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200583 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100584 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200585 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200586 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200587 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100588 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200589 http_error_message(s));
590 else { /* SI_ET_CONN_OTHER and others */
591 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200592 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100593 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200594 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100595}
596
Willy Tarreau42250582007-04-01 01:30:43 +0200597extern const char sess_term_cond[8];
598extern const char sess_fin_state[8];
599extern const char *monthname[12];
Willy Tarreaubafbe012017-11-24 17:34:44 +0100600struct pool_head *pool_head_http_txn;
601struct pool_head *pool_head_requri;
602struct pool_head *pool_head_capture = NULL;
603struct pool_head *pool_head_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100604
Willy Tarreau117f59e2007-03-04 18:17:17 +0100605/*
606 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +0200607 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +0100608 */
609void capture_headers(char *som, struct hdr_idx *idx,
610 char **cap, struct cap_hdr *cap_hdr)
611{
612 char *eol, *sol, *col, *sov;
613 int cur_idx;
614 struct cap_hdr *h;
615 int len;
616
617 sol = som + hdr_idx_first_pos(idx);
618 cur_idx = hdr_idx_first_idx(idx);
619
620 while (cur_idx) {
621 eol = sol + idx->v[cur_idx].len;
622
623 col = sol;
624 while (col < eol && *col != ':')
625 col++;
626
627 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100628 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +0100629 sov++;
630
631 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +0200632 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +0100633 (strncasecmp(sol, h->name, h->namelen) == 0)) {
634 if (cap[h->index] == NULL)
635 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +0100636 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100637
638 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100639 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +0100640 continue;
641 }
642
643 len = eol - sov;
644 if (len > h->len)
645 len = h->len;
646
647 memcpy(cap[h->index], sov, len);
648 cap[h->index][len]=0;
649 }
650 }
651 sol = eol + idx->v[cur_idx].cr + 1;
652 cur_idx = idx->v[cur_idx].next;
653 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100654}
655
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200656/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
657 * conversion succeeded, 0 in case of error. If the request was already 1.X,
658 * nothing is done and 1 is returned.
659 */
Willy Tarreau79e57332018-10-02 16:01:16 +0200660int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200661{
662 int delta;
663 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100664 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200665
666 if (msg->sl.rq.v_l != 0)
667 return 1;
668
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300669 /* RFC 1945 allows only GET for HTTP/0.9 requests */
670 if (txn->meth != HTTP_METH_GET)
671 return 0;
672
Willy Tarreauf37954d2018-06-15 18:31:02 +0200673 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200674
675 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300676 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
677 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200678 }
679 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200680 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +0100681 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200682 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200683 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200684 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +0200685 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200686 NULL, NULL);
687 if (unlikely(!cur_end))
688 return 0;
689
690 /* we have a full HTTP/1.0 request now and we know that
691 * we have either a CR or an LF at <ptr>.
692 */
693 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
694 return 1;
695}
696
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100697/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100698 * and "keep-alive" values. If we already know that some headers may safely
699 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100700 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
701 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +0100702 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100703 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
704 * found, and TX_CON_*_SET is adjusted depending on what is left so only
705 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100706 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +0100707 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100708void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +0100709{
Willy Tarreau5b154472009-12-21 20:11:07 +0100710 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100711 const char *hdr_val = "Connection";
712 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +0100713
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100714 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +0100715 return;
716
Willy Tarreau88d349d2010-01-25 12:15:43 +0100717 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
718 hdr_val = "Proxy-Connection";
719 hdr_len = 16;
720 }
721
Willy Tarreau5b154472009-12-21 20:11:07 +0100722 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100723 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200724 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100725 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
726 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100727 if (to_del & 2)
728 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100729 else
730 txn->flags |= TX_CON_KAL_SET;
731 }
732 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
733 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100734 if (to_del & 1)
735 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100736 else
737 txn->flags |= TX_CON_CLO_SET;
738 }
Willy Tarreau50fc7772012-11-11 22:19:57 +0100739 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
740 txn->flags |= TX_HDR_CONN_UPG;
741 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100742 }
743
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100744 txn->flags |= TX_HDR_CONN_PRS;
745 return;
746}
Willy Tarreau5b154472009-12-21 20:11:07 +0100747
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100748/* Apply desired changes on the Connection: header. Values may be removed and/or
749 * added depending on the <wanted> flags, which are exclusively composed of
750 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
751 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
752 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100753void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100754{
755 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100756 const char *hdr_val = "Connection";
757 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100758
759 ctx.idx = 0;
760
Willy Tarreau88d349d2010-01-25 12:15:43 +0100761
762 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
763 hdr_val = "Proxy-Connection";
764 hdr_len = 16;
765 }
766
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100767 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200768 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100769 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
770 if (wanted & TX_CON_KAL_SET)
771 txn->flags |= TX_CON_KAL_SET;
772 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100773 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +0100774 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100775 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
776 if (wanted & TX_CON_CLO_SET)
777 txn->flags |= TX_CON_CLO_SET;
778 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100779 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +0100780 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100781 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100782
783 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
784 return;
785
786 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
787 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100788 hdr_val = "Connection: close";
789 hdr_len = 17;
790 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
791 hdr_val = "Proxy-Connection: close";
792 hdr_len = 23;
793 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100794 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100795 }
796
797 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
798 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100799 hdr_val = "Connection: keep-alive";
800 hdr_len = 22;
801 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
802 hdr_val = "Proxy-Connection: keep-alive";
803 hdr_len = 28;
804 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100805 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100806 }
807 return;
Willy Tarreau5b154472009-12-21 20:11:07 +0100808}
809
Willy Tarreau87b09662015-04-03 00:22:06 +0200810void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200811{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200812 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200813 int tmp = TX_CON_WANT_KAL;
814
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200815 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
816 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200817 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
818 tmp = TX_CON_WANT_TUN;
819
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200820 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200821 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
822 tmp = TX_CON_WANT_TUN;
823 }
824
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200825 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200826 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
827 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200828 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200829 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
830 tmp = TX_CON_WANT_CLO;
831 else
832 tmp = TX_CON_WANT_SCL;
833 }
834
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200835 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200836 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
837 tmp = TX_CON_WANT_CLO;
838
839 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
840 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
841
842 if (!(txn->flags & TX_HDR_CONN_PRS) &&
843 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
844 /* parse the Connection header and possibly clean it */
845 int to_del = 0;
846 if ((msg->flags & HTTP_MSGF_VER_11) ||
847 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200848 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200849 to_del |= 2; /* remove "keep-alive" */
850 if (!(msg->flags & HTTP_MSGF_VER_11))
851 to_del |= 1; /* remove "close" */
852 http_parse_connection_header(txn, msg, to_del);
853 }
854
855 /* check if client or config asks for explicit close in KAL/SCL */
856 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
857 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
858 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
859 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
860 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200861 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200862 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
863}
William Lallemand82fe75c2012-10-23 10:25:10 +0200864
Willy Tarreaud787e662009-07-07 10:14:51 +0200865/* This stream analyser waits for a complete HTTP request. It returns 1 if the
866 * processing can continue on next analysers, or zero if it either needs more
867 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100868 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200869 * when it has nothing left to do, and may remove any analyser when it wants to
870 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100871 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200872int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100873{
Willy Tarreau59234e92008-11-30 23:51:27 +0100874 /*
875 * We will parse the partial (or complete) lines.
876 * We will check the request syntax, and also join multi-line
877 * headers. An index of all the lines will be elaborated while
878 * parsing.
879 *
880 * For the parsing, we use a 28 states FSM.
881 *
882 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200883 * ci_head(req) = beginning of request
884 * ci_head(req) + msg->eoh = end of processed headers / start of current one
885 * ci_tail(req) = end of input data
886 * msg->eol = end of current header or line (LF or CRLF)
887 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200888 *
889 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200890 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200891 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
892 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100893 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100894
Willy Tarreau59234e92008-11-30 23:51:27 +0100895 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200896 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200897 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100898 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200899 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100900
Christopher Faulet45073512018-07-20 10:16:29 +0200901 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 +0100902 now_ms, __FUNCTION__,
903 s,
904 req,
905 req->rex, req->wex,
906 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200907 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100908 req->analysers);
909
Willy Tarreau52a0c602009-08-16 22:45:38 +0200910 /* we're speaking HTTP here, so let's speak HTTP to the client */
911 s->srv_error = http_return_srv_error;
912
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300913 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200914 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300915 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
916
Willy Tarreau83e3af02009-12-28 17:39:57 +0100917 /* There's a protected area at the end of the buffer for rewriting
918 * purposes. We don't want to start to parse the request if the
919 * protected area is affected, because we may have to move processed
920 * data later, which is much more complicated.
921 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200922 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200923 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100924 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200925 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100926 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100927 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200928 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200929 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100930 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100931 return 0;
932 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200933 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200934 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200935 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100936 }
937
Willy Tarreauf37954d2018-06-15 18:31:02 +0200938 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100939 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100940 }
941
Willy Tarreau59234e92008-11-30 23:51:27 +0100942 /* 1: we might have to print this header in debug mode */
943 if (unlikely((global.mode & MODE_DEBUG) &&
944 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200945 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100946 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100947
Willy Tarreauf37954d2018-06-15 18:31:02 +0200948 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200949 /* this is a bit complex : in case of error on the request line,
950 * we know that rq.l is still zero, so we display only the part
951 * up to the end of the line (truncated by debug_hdr).
952 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200953 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100954 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100955
Willy Tarreau59234e92008-11-30 23:51:27 +0100956 sol += hdr_idx_first_pos(&txn->hdr_idx);
957 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100958
Willy Tarreau59234e92008-11-30 23:51:27 +0100959 while (cur_idx) {
960 eol = sol + txn->hdr_idx.v[cur_idx].len;
961 debug_hdr("clihdr", s, sol, eol);
962 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
963 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100964 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100965 }
966
Willy Tarreau58f10d72006-12-04 02:26:12 +0100967
Willy Tarreau59234e92008-11-30 23:51:27 +0100968 /*
969 * Now we quickly check if we have found a full valid request.
970 * If not so, we check the FD and buffer states before leaving.
971 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100972 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100973 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200974 * on a keep-alive stream, if we encounter and error, close, t/o,
975 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100976 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200977 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200978 * Last, we may increase some tracked counters' http request errors on
979 * the cases that are deliberately the client's fault. For instance,
980 * a timeout or connection reset is not counted as an error. However
981 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100982 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100983
Willy Tarreau655dce92009-11-08 13:10:58 +0100984 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100985 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100986 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100987 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100988 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200989 stream_inc_http_req_ctr(s);
990 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200991 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100992 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100993 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100994
Willy Tarreau59234e92008-11-30 23:51:27 +0100995 /* 1: Since we are in header mode, if there's no space
996 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200997 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100998 * must terminate it now.
999 */
Willy Tarreau23752332018-06-15 14:54:53 +02001000 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001001 /* FIXME: check if URI is set and return Status
1002 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001003 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001004 stream_inc_http_req_ctr(s);
1005 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001006 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02001007 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02001008 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +01001009 goto return_bad_req;
1010 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001011
Willy Tarreau59234e92008-11-30 23:51:27 +01001012 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001013 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001014 if (!(s->flags & SF_ERR_MASK))
1015 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001016
Willy Tarreaufcffa692010-01-10 14:21:19 +01001017 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001018 goto failed_keep_alive;
1019
Willy Tarreau0f228a02015-05-01 15:37:53 +02001020 if (sess->fe->options & PR_O_IGNORE_PRB)
1021 goto failed_keep_alive;
1022
Willy Tarreau59234e92008-11-30 23:51:27 +01001023 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001024 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001025 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001026 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001027 }
1028
Willy Tarreaudc979f22012-12-04 10:39:01 +01001029 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001030 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001031 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001032 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001033 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001034 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001035 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001036 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001037 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001038 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001039
Willy Tarreaue7dff022015-04-03 01:14:29 +02001040 if (!(s->flags & SF_FINST_MASK))
1041 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001042 return 0;
1043 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001044
Willy Tarreau59234e92008-11-30 23:51:27 +01001045 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001046 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001047 if (!(s->flags & SF_ERR_MASK))
1048 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001049
Willy Tarreaufcffa692010-01-10 14:21:19 +01001050 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001051 goto failed_keep_alive;
1052
Willy Tarreau0f228a02015-05-01 15:37:53 +02001053 if (sess->fe->options & PR_O_IGNORE_PRB)
1054 goto failed_keep_alive;
1055
Willy Tarreau59234e92008-11-30 23:51:27 +01001056 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001057 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001058 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001059 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001060 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001061 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001062 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001063 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001064 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001065 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001066
Willy Tarreau87b09662015-04-03 00:22:06 +02001067 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001068 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001069 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001070 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001071 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001072
Willy Tarreaue7dff022015-04-03 01:14:29 +02001073 if (!(s->flags & SF_FINST_MASK))
1074 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001075 return 0;
1076 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001077
Willy Tarreau59234e92008-11-30 23:51:27 +01001078 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001079 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001080 if (!(s->flags & SF_ERR_MASK))
1081 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001082
Willy Tarreaufcffa692010-01-10 14:21:19 +01001083 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001084 goto failed_keep_alive;
1085
Willy Tarreau0f228a02015-05-01 15:37:53 +02001086 if (sess->fe->options & PR_O_IGNORE_PRB)
1087 goto failed_keep_alive;
1088
Willy Tarreau4076a152009-04-02 15:18:36 +02001089 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001090 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001091 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001092 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001093 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001094 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001095 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001096 stream_inc_http_err_ctr(s);
1097 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001098 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001099 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001100 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001101 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001102
Willy Tarreaue7dff022015-04-03 01:14:29 +02001103 if (!(s->flags & SF_FINST_MASK))
1104 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001105 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001106 }
1107
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001108 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001109 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001110 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001111#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001112 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001113 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001114 /* We need more data, we have to re-enable quick-ack in case we
1115 * previously disabled it, otherwise we might cause the client
1116 * to delay next data.
1117 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001118 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001119 }
1120#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001121
Willy Tarreaufcffa692010-01-10 14:21:19 +01001122 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1123 /* If the client starts to talk, let's fall back to
1124 * request timeout processing.
1125 */
1126 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001127 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001128 }
1129
Willy Tarreau59234e92008-11-30 23:51:27 +01001130 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001131 if (!tick_isset(req->analyse_exp)) {
1132 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1133 (txn->flags & TX_WAIT_NEXT_RQ) &&
1134 tick_isset(s->be->timeout.httpka))
1135 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1136 else
1137 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1138 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001139
Willy Tarreau59234e92008-11-30 23:51:27 +01001140 /* we're not ready yet */
1141 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001142
1143 failed_keep_alive:
1144 /* Here we process low-level errors for keep-alive requests. In
1145 * short, if the request is not the first one and it experiences
1146 * a timeout, read error or shutdown, we just silently close so
1147 * that the client can try again.
1148 */
1149 txn->status = 0;
1150 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001151 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001152 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001153 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001154 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001155 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001156 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001157 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001158
Willy Tarreaud787e662009-07-07 10:14:51 +02001159 /* OK now we have a complete HTTP request with indexed headers. Let's
1160 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001161 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001162 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001163 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001164 * byte after the last LF. msg->sov points to the first byte of data.
1165 * msg->eol cannot be trusted because it may have been left uninitialized
1166 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001167 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001168
Willy Tarreau87b09662015-04-03 00:22:06 +02001169 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001170 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001171
Willy Tarreaub16a5742010-01-10 14:46:16 +01001172 if (txn->flags & TX_WAIT_NEXT_RQ) {
1173 /* kill the pending keep-alive timeout */
1174 txn->flags &= ~TX_WAIT_NEXT_RQ;
1175 req->analyse_exp = TICK_ETERNITY;
1176 }
1177
1178
Willy Tarreaud787e662009-07-07 10:14:51 +02001179 /* Maybe we found in invalid header name while we were configured not
1180 * to block on that, so we have to capture it now.
1181 */
1182 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001183 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001184
Willy Tarreau59234e92008-11-30 23:51:27 +01001185 /*
1186 * 1: identify the method
1187 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001188 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001189
1190 /* we can make use of server redirect on GET and HEAD */
1191 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001192 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001193 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001194 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001195 /* PRI is reserved for the HTTP/2 preface */
1196 msg->err_pos = 0;
1197 goto return_bad_req;
1198 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001199
Willy Tarreau59234e92008-11-30 23:51:27 +01001200 /*
1201 * 2: check if the URI matches the monitor_uri.
1202 * We have to do this for every request which gets in, because
1203 * the monitor-uri is defined by the frontend.
1204 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001205 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1206 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001207 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001208 sess->fe->monitor_uri,
1209 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001210 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001211 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001212 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001213 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001214
Willy Tarreaue7dff022015-04-03 01:14:29 +02001215 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001216 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001217
Willy Tarreau59234e92008-11-30 23:51:27 +01001218 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001219 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001220 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001221
Willy Tarreau59234e92008-11-30 23:51:27 +01001222 ret = acl_pass(ret);
1223 if (cond->pol == ACL_COND_UNLESS)
1224 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001225
Willy Tarreau59234e92008-11-30 23:51:27 +01001226 if (ret) {
1227 /* we fail this request, let's return 503 service unavail */
1228 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001229 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001230 if (!(s->flags & SF_ERR_MASK))
1231 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001232 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001233 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001234 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001235
Willy Tarreau59234e92008-11-30 23:51:27 +01001236 /* nothing to fail, let's reply normaly */
1237 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001238 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001239 if (!(s->flags & SF_ERR_MASK))
1240 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001241 goto return_prx_cond;
1242 }
1243
1244 /*
1245 * 3: Maybe we have to copy the original REQURI for the logs ?
1246 * Note: we cannot log anymore if the request has been
1247 * classified as invalid.
1248 */
1249 if (unlikely(s->logs.logwait & LW_REQ)) {
1250 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001251 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001252 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001253
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001254 if (urilen >= global.tune.requri_len )
1255 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001256 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001257 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001258
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001259 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001260 s->do_log(s);
1261 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001262 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001263 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001264 }
Willy Tarreau06619262006-12-17 08:37:22 +01001265
Willy Tarreau91852eb2015-05-01 13:26:00 +02001266 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1267 * exactly one digit "." one digit. This check may be disabled using
1268 * option accept-invalid-http-request.
1269 */
1270 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1271 if (msg->sl.rq.v_l != 8) {
1272 msg->err_pos = msg->sl.rq.v;
1273 goto return_bad_req;
1274 }
1275
Willy Tarreauf37954d2018-06-15 18:31:02 +02001276 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1277 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1278 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1279 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001280 msg->err_pos = msg->sl.rq.v + 4;
1281 goto return_bad_req;
1282 }
1283 }
Willy Tarreau13317662015-05-01 13:47:08 +02001284 else {
1285 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1286 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1287 goto return_bad_req;
1288 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001289
Willy Tarreau5b154472009-12-21 20:11:07 +01001290 /* ... and check if the request is HTTP/1.1 or above */
1291 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001292 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
1293 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
1294 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001295 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001296
1297 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001298 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 +01001299
Willy Tarreau88d349d2010-01-25 12:15:43 +01001300 /* if the frontend has "option http-use-proxy-header", we'll check if
1301 * we have what looks like a proxied connection instead of a connection,
1302 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1303 * Note that this is *not* RFC-compliant, however browsers and proxies
1304 * happen to do that despite being non-standard :-(
1305 * We consider that a request not beginning with either '/' or '*' is
1306 * a proxied connection, which covers both "scheme://location" and
1307 * CONNECT ip:port.
1308 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001309 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001310 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01001311 txn->flags |= TX_USE_PX_CONN;
1312
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001313 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001314 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001315
Willy Tarreau59234e92008-11-30 23:51:27 +01001316 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001317 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreauf37954d2018-06-15 18:31:02 +02001318 capture_headers(ci_head(req), &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001319 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001320
Willy Tarreau557f1992015-05-01 10:05:17 +02001321 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1322 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001323 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001324 * The length of a message body is determined by one of the following
1325 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001326 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001327 * 1. Any response to a HEAD request and any response with a 1xx
1328 * (Informational), 204 (No Content), or 304 (Not Modified) status
1329 * code is always terminated by the first empty line after the
1330 * header fields, regardless of the header fields present in the
1331 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001332 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001333 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1334 * the connection will become a tunnel immediately after the empty
1335 * line that concludes the header fields. A client MUST ignore any
1336 * Content-Length or Transfer-Encoding header fields received in
1337 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001338 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001339 * 3. If a Transfer-Encoding header field is present and the chunked
1340 * transfer coding (Section 4.1) is the final encoding, the message
1341 * body length is determined by reading and decoding the chunked
1342 * data until the transfer coding indicates the data is complete.
1343 *
1344 * If a Transfer-Encoding header field is present in a response and
1345 * the chunked transfer coding is not the final encoding, the
1346 * message body length is determined by reading the connection until
1347 * it is closed by the server. If a Transfer-Encoding header field
1348 * is present in a request and the chunked transfer coding is not
1349 * the final encoding, the message body length cannot be determined
1350 * reliably; the server MUST respond with the 400 (Bad Request)
1351 * status code and then close the connection.
1352 *
1353 * If a message is received with both a Transfer-Encoding and a
1354 * Content-Length header field, the Transfer-Encoding overrides the
1355 * Content-Length. Such a message might indicate an attempt to
1356 * perform request smuggling (Section 9.5) or response splitting
1357 * (Section 9.4) and ought to be handled as an error. A sender MUST
1358 * remove the received Content-Length field prior to forwarding such
1359 * a message downstream.
1360 *
1361 * 4. If a message is received without Transfer-Encoding and with
1362 * either multiple Content-Length header fields having differing
1363 * field-values or a single Content-Length header field having an
1364 * invalid value, then the message framing is invalid and the
1365 * recipient MUST treat it as an unrecoverable error. If this is a
1366 * request message, the server MUST respond with a 400 (Bad Request)
1367 * status code and then close the connection. If this is a response
1368 * message received by a proxy, the proxy MUST close the connection
1369 * to the server, discard the received response, and send a 502 (Bad
1370 * Gateway) response to the client. If this is a response message
1371 * received by a user agent, the user agent MUST close the
1372 * connection to the server and discard the received response.
1373 *
1374 * 5. If a valid Content-Length header field is present without
1375 * Transfer-Encoding, its decimal value defines the expected message
1376 * body length in octets. If the sender closes the connection or
1377 * the recipient times out before the indicated number of octets are
1378 * received, the recipient MUST consider the message to be
1379 * incomplete and close the connection.
1380 *
1381 * 6. If this is a request message and none of the above are true, then
1382 * the message body length is zero (no message body is present).
1383 *
1384 * 7. Otherwise, this is a response message without a declared message
1385 * body length, so the message body length is determined by the
1386 * number of octets received prior to the server closing the
1387 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001388 */
1389
Willy Tarreau32b47f42009-10-18 20:55:02 +02001390 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001391 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001392 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001393 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001394 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001395 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001396 /* chunked not last, return badreq */
1397 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001398 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001399 }
1400
Willy Tarreau1c913912015-04-30 10:57:51 +02001401 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001402 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001403 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001404 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001405 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1406 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001407 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001408 signed long long cl;
1409
Willy Tarreauad14f752011-09-02 20:33:27 +02001410 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001411 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001412 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001413 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001414
Willy Tarreauad14f752011-09-02 20:33:27 +02001415 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001416 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001417 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001418 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001419
Willy Tarreauad14f752011-09-02 20:33:27 +02001420 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001421 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001422 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001423 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001424
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001425 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001426 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001427 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001428 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001429
Christopher Fauletbe821b92017-03-30 11:21:53 +02001430 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001431 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001432 }
1433
Willy Tarreau34dfc602015-05-01 10:09:49 +02001434 /* even bodyless requests have a known length */
1435 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001436
Willy Tarreau179085c2014-04-28 16:48:56 +02001437 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1438 * only change if both the request and the config reference something else.
1439 * Option httpclose by itself sets tunnel mode where headers are mangled.
1440 * However, if another mode is set, it will affect it (eg: server-close/
1441 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1442 * if FE and BE have the same settings (common). The method consists in
1443 * checking if options changed between the two calls (implying that either
1444 * one is non-null, or one of them is non-null and we are there for the first
1445 * time.
1446 */
1447 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001448 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001449 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001450
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001451 /* we may have to wait for the request's body */
1452 if ((s->be->options & PR_O_WREQ_BODY) &&
1453 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1454 req->analysers |= AN_REQ_HTTP_BODY;
1455
Willy Tarreau83ece462017-12-21 15:13:09 +01001456 /*
1457 * RFC7234#4:
1458 * A cache MUST write through requests with methods
1459 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1460 * the origin server; i.e., a cache is not allowed
1461 * to generate a reply to such a request before
1462 * having forwarded the request and having received
1463 * a corresponding response.
1464 *
1465 * RFC7231#4.2.1:
1466 * Of the request methods defined by this
1467 * specification, the GET, HEAD, OPTIONS, and TRACE
1468 * methods are defined to be safe.
1469 */
1470 if (likely(txn->meth == HTTP_METH_GET ||
1471 txn->meth == HTTP_METH_HEAD ||
1472 txn->meth == HTTP_METH_OPTIONS ||
1473 txn->meth == HTTP_METH_TRACE))
1474 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1475
Willy Tarreaud787e662009-07-07 10:14:51 +02001476 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001477 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001478 req->analyse_exp = TICK_ETERNITY;
1479 return 1;
1480
1481 return_bad_req:
1482 /* We centralize bad requests processing here */
1483 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1484 /* we detected a parsing error. We want to archive this request
1485 * in the dedicated proxy area for later troubleshooting.
1486 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001487 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001488 }
1489
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001490 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001491 txn->req.msg_state = HTTP_MSG_ERROR;
1492 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001493 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001494
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001495 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001496 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001497 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001498
1499 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001500 if (!(s->flags & SF_ERR_MASK))
1501 s->flags |= SF_ERR_PRXCOND;
1502 if (!(s->flags & SF_FINST_MASK))
1503 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001504
Christopher Faulet0184ea72017-01-05 14:06:34 +01001505 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001506 req->analyse_exp = TICK_ETERNITY;
1507 return 0;
1508}
1509
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001510
Willy Tarreau347a35d2013-11-22 17:51:09 +01001511/* This function prepares an applet to handle the stats. It can deal with the
1512 * "100-continue" expectation, check that admin rules are met for POST requests,
1513 * and program a response message if something was unexpected. It cannot fail
1514 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001515 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001516 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001517 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001518 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001519int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001520{
1521 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001522 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001523 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001524 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001525 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001526 struct uri_auth *uri_auth = s->be->uri_auth;
1527 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001528 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001529
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001530 appctx = si_appctx(si);
1531 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1532 appctx->st1 = appctx->st2 = 0;
1533 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1534 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001535 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001536 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001537
Willy Tarreauf37954d2018-06-15 18:31:02 +02001538 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001539 lookup = uri + uri_auth->uri_len;
1540
1541 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1542 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001543 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001544 break;
1545 }
1546 }
1547
1548 if (uri_auth->refresh) {
1549 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1550 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001551 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001552 break;
1553 }
1554 }
1555 }
1556
1557 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1558 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001559 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001560 break;
1561 }
1562 }
1563
Willy Tarreau1e62df92016-01-11 18:57:53 +01001564 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1565 if (memcmp(h, ";typed", 6) == 0) {
1566 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1567 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1568 break;
1569 }
1570 }
1571
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001572 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1573 if (memcmp(h, ";st=", 4) == 0) {
1574 int i;
1575 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001576 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001577 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1578 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001579 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001580 break;
1581 }
1582 }
1583 break;
1584 }
1585 }
1586
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001587 appctx->ctx.stats.scope_str = 0;
1588 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001589 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1590 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1591 int itx = 0;
1592 const char *h2;
1593 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1594 const char *err;
1595
1596 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1597 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001598 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001599 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1600 itx++;
1601 h++;
1602 }
1603
1604 if (itx > STAT_SCOPE_TXT_MAXLEN)
1605 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001606 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001607
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001608 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001609 memcpy(scope_txt, h2, itx);
1610 scope_txt[itx] = '\0';
1611 err = invalid_char(scope_txt);
1612 if (err) {
1613 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001614 appctx->ctx.stats.scope_str = 0;
1615 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001616 }
1617 break;
1618 }
1619 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001620
1621 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001622 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001623 int ret = 1;
1624
1625 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001626 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 +01001627 ret = acl_pass(ret);
1628 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1629 ret = !ret;
1630 }
1631
1632 if (ret) {
1633 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001634 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001635 break;
1636 }
1637 }
1638
1639 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001640 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001641 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001642 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001643 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1644 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001645 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001646 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001647 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001648 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1649 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001650 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001651 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001652 else {
1653 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001654 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001655 }
1656
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001657 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001658 return 1;
1659}
1660
Lukas Tribus67db8df2013-06-23 17:37:13 +02001661/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
1662 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
1663 */
Vincent Bernat6e615892016-05-18 16:17:44 +02001664void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001665{
1666#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02001667 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001668 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1669#endif
1670#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02001671 if (from->ss_family == AF_INET6) {
1672 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02001673 /* v4-mapped addresses need IP_TOS */
1674 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1675 else
1676 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
1677 }
1678#endif
1679}
1680
Willy Tarreau87b09662015-04-03 00:22:06 +02001681int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001682 const char* name, unsigned int name_len,
1683 const char *str, struct my_regex *re,
1684 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001685{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001686 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001687 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02001688 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001689 int (*http_find_hdr_func)(const char *name, int len, char *sol,
1690 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02001691 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001692
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001693 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06001694
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001695 /* Choose the header browsing function. */
1696 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001697 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001698 http_find_hdr_func = http_find_header2;
1699 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001700 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001701 http_find_hdr_func = http_find_full_header2;
1702 break;
1703 default: /* impossible */
1704 return -1;
1705 }
1706
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001707 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
1708 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02001709 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001710 char *val = ctx.line + ctx.val;
1711 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06001712
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001713 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
1714 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06001715
Willy Tarreau6e27be12018-08-22 04:46:47 +02001716 len = exp_replace(output->area, output->size, val, str, pmatch);
1717 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06001718 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06001719
Willy Tarreau6e27be12018-08-22 04:46:47 +02001720 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06001721
1722 hdr->len += delta;
1723 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001724
1725 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001726 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06001727 }
1728
1729 return 0;
1730}
1731
Willy Tarreau87b09662015-04-03 00:22:06 +02001732static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001733 const char* name, unsigned int name_len,
1734 struct list *fmt, struct my_regex *re,
1735 int action)
1736{
Willy Tarreau83061a82018-07-13 11:56:34 +02001737 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001738 int ret = -1;
1739
1740 replace = alloc_trash_chunk();
1741 if (!replace)
1742 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001743
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001744 replace->data = build_logline(s, replace->area, replace->size, fmt);
1745 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001746 goto leave;
1747
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001748 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1749 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001750
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001751 leave:
1752 free_trash_chunk(replace);
1753 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001754}
1755
Willy Tarreau87b09662015-04-03 00:22:06 +02001756/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001757 * transaction <txn>. Returns the verdict of the first rule that prevents
1758 * further processing of the request (auth, deny, ...), and defaults to
1759 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1760 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001761 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1762 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1763 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001764 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001765enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001766http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001767{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001768 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001769 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001770 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001771 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001772 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001773 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02001774 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001775 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001776
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001777 /* If "the current_rule_list" match the executed rule list, we are in
1778 * resume condition. If a resume is needed it is always in the action
1779 * and never in the ACL or converters. In this case, we initialise the
1780 * current rule, and go to the action execution point.
1781 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001782 if (s->current_rule) {
1783 rule = s->current_rule;
1784 s->current_rule = NULL;
1785 if (s->current_rule_list == rules)
1786 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001787 }
1788 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001789
Willy Tarreauff011f22011-01-06 17:51:27 +01001790 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001791
Willy Tarreau96257ec2012-12-27 10:46:37 +01001792 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001793 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001794 int ret;
1795
Willy Tarreau192252e2015-04-04 01:47:55 +02001796 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001797 ret = acl_pass(ret);
1798
Willy Tarreauff011f22011-01-06 17:51:27 +01001799 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001800 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001801
1802 if (!ret) /* condition not matched */
1803 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001804 }
1805
Willy Tarreauacc98002015-09-27 23:34:39 +02001806 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001807resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001808 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001809 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02001810 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001811
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001812 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001813 if (deny_status)
1814 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02001815 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001816
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001817 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001818 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001819 if (deny_status)
1820 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02001821 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001822
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001823 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02001824 /* Auth might be performed on regular http-req rules as well as on stats */
1825 auth_realm = rule->arg.auth.realm;
1826 if (!auth_realm) {
1827 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1828 auth_realm = STATS_DEFAULT_REALM;
1829 else
1830 auth_realm = px->id;
1831 }
1832 /* send 401/407 depending on whether we use a proxy or not. We still
1833 * count one error, because normal browsing won't significantly
1834 * increase the counter but brute force attempts will.
1835 */
1836 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1837 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001838 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001839 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02001840 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001841
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001842 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02001843 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
1844 return HTTP_RULE_RES_BADREQ;
1845 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001846
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001847 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001848 s->task->nice = rule->arg.nice;
1849 break;
1850
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001851 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001852 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001853 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001854 break;
1855
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001856 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02001857#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001858 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001859 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02001860#endif
1861 break;
1862
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001863 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001864 s->logs.level = rule->arg.loglevel;
1865 break;
1866
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001867 case ACT_HTTP_REPLACE_HDR:
1868 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001869 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1870 rule->arg.hdr_add.name_len,
1871 &rule->arg.hdr_add.fmt,
1872 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06001873 return HTTP_RULE_RES_BADREQ;
1874 break;
1875
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001876 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001877 ctx.idx = 0;
1878 /* remove all occurrences of the header */
1879 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001880 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001881 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001882 }
Willy Tarreau85603282015-01-21 20:39:27 +01001883 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001884
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001885 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001886 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001887 /* The scope of the trash buffer must be limited to this function. The
1888 * build_logline() function can execute a lot of other function which
1889 * can use the trash buffer. So for limiting the scope of this global
1890 * buffer, we build first the header value using build_logline, and
1891 * after we store the header name.
1892 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001893 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001894
1895 replace = alloc_trash_chunk();
1896 if (!replace)
1897 return HTTP_RULE_RES_BADREQ;
1898
Thierry Fournier4b788f72016-06-01 13:35:36 +02001899 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001900 len += build_logline(s, replace->area + len,
1901 replace->size - len,
1902 &rule->arg.hdr_add.fmt);
1903 memcpy(replace->area, rule->arg.hdr_add.name,
1904 rule->arg.hdr_add.name_len);
1905 replace->area[rule->arg.hdr_add.name_len] = ':';
1906 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1907 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001908
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001909 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001910 /* remove all occurrences of the header */
1911 ctx.idx = 0;
1912 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001913 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001914 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1915 }
1916 }
1917
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001918 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001919 static unsigned char rate_limit = 0;
1920
1921 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001922 replace->area[rule->arg.hdr_add.name_len] = 0;
1923 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,
1924 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001925 }
1926
1927 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1928 if (sess->fe != s->be)
1929 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1930 if (sess->listener->counters)
1931 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1932 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001933
1934 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001935 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001936 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001937
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001938 case ACT_HTTP_DEL_ACL:
1939 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001940 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001941 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001942
1943 /* collect reference */
1944 ref = pat_ref_lookup(rule->arg.map.ref);
1945 if (!ref)
1946 continue;
1947
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001948 /* allocate key */
1949 key = alloc_trash_chunk();
1950 if (!key)
1951 return HTTP_RULE_RES_BADREQ;
1952
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001953 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001954 key->data = build_logline(s, key->area, key->size,
1955 &rule->arg.map.key);
1956 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001957
1958 /* perform update */
1959 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001960 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001961 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001962 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001963
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001964 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001965 break;
1966 }
1967
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001968 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001969 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001970 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001971
1972 /* collect reference */
1973 ref = pat_ref_lookup(rule->arg.map.ref);
1974 if (!ref)
1975 continue;
1976
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001977 /* allocate key */
1978 key = alloc_trash_chunk();
1979 if (!key)
1980 return HTTP_RULE_RES_BADREQ;
1981
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001982 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001983 key->data = build_logline(s, key->area, key->size,
1984 &rule->arg.map.key);
1985 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001986
1987 /* perform update */
1988 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001989 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001990 if (pat_ref_find_elt(ref, key->area) == NULL)
1991 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001992 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001993
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001994 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001995 break;
1996 }
1997
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001998 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001999 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002000 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002001
2002 /* collect reference */
2003 ref = pat_ref_lookup(rule->arg.map.ref);
2004 if (!ref)
2005 continue;
2006
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002007 /* allocate key */
2008 key = alloc_trash_chunk();
2009 if (!key)
2010 return HTTP_RULE_RES_BADREQ;
2011
2012 /* allocate value */
2013 value = alloc_trash_chunk();
2014 if (!value) {
2015 free_trash_chunk(key);
2016 return HTTP_RULE_RES_BADREQ;
2017 }
2018
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002019 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002020 key->data = build_logline(s, key->area, key->size,
2021 &rule->arg.map.key);
2022 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002023
2024 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002025 value->data = build_logline(s, value->area,
2026 value->size,
2027 &rule->arg.map.value);
2028 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002029
2030 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002031 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002032 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002033 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002034 else
2035 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002036 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002037
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002038 free_trash_chunk(key);
2039 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002040 break;
2041 }
William Lallemand73025dd2014-04-24 14:38:37 +02002042
Thierry FOURNIER42148732015-09-02 17:17:33 +02002043 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002044 if ((s->req.flags & CF_READ_ERROR) ||
2045 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2046 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2047 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002048 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002049
Willy Tarreauacc98002015-09-27 23:34:39 +02002050 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002051 case ACT_RET_ERR:
2052 case ACT_RET_CONT:
2053 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002054 case ACT_RET_STOP:
2055 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002056 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002057 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002058 return HTTP_RULE_RES_YIELD;
2059 }
William Lallemand73025dd2014-04-24 14:38:37 +02002060 break;
2061
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002062 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002063 /* Note: only the first valid tracking parameter of each
2064 * applies.
2065 */
2066
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002067 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002068 struct stktable *t;
2069 struct stksess *ts;
2070 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002071 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002072
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002073 t = rule->arg.trk_ctr.table.t;
2074 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 +02002075
2076 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002077 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002078
2079 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002080 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2081 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2082 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002083 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002084
Emeric Brun819fc6f2017-06-13 19:37:32 +02002085 if (ptr1)
2086 stktable_data_cast(ptr1, http_req_cnt)++;
2087
2088 if (ptr2)
2089 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2090 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2091
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002092 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002093
2094 /* If data was modified, we need to touch to re-schedule sync */
2095 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002096 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002097
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002098 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002099 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002100 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002101 }
2102 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002103 break;
2104
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002105 /* other flags exists, but normaly, they never be matched. */
2106 default:
2107 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002108 }
2109 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002110
2111 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002112 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002113}
2114
Willy Tarreau71241ab2012-12-27 11:30:54 +01002115
Willy Tarreau51d861a2015-05-22 17:30:48 +02002116/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2117 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2118 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2119 * is returned, the process can continue the evaluation of next rule list. If
2120 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2121 * is returned, it means the operation could not be processed and a server error
2122 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2123 * deny rule. If *YIELD is returned, the caller must call again the function
2124 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002125 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002126static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002127http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002128{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002129 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002130 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002131 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002132 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002133 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002134 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002135
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002136 /* If "the current_rule_list" match the executed rule list, we are in
2137 * resume condition. If a resume is needed it is always in the action
2138 * and never in the ACL or converters. In this case, we initialise the
2139 * current rule, and go to the action execution point.
2140 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002141 if (s->current_rule) {
2142 rule = s->current_rule;
2143 s->current_rule = NULL;
2144 if (s->current_rule_list == rules)
2145 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002146 }
2147 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002148
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002149 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002150
2151 /* check optional condition */
2152 if (rule->cond) {
2153 int ret;
2154
Willy Tarreau192252e2015-04-04 01:47:55 +02002155 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002156 ret = acl_pass(ret);
2157
2158 if (rule->cond->pol == ACL_COND_UNLESS)
2159 ret = !ret;
2160
2161 if (!ret) /* condition not matched */
2162 continue;
2163 }
2164
Willy Tarreauacc98002015-09-27 23:34:39 +02002165 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002166resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002167 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002168 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002169 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002170
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002171 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002172 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002173 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002174
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002175 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002176 s->task->nice = rule->arg.nice;
2177 break;
2178
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002179 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002180 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002181 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002182 break;
2183
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002184 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002185#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002186 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002187 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002188#endif
2189 break;
2190
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002191 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002192 s->logs.level = rule->arg.loglevel;
2193 break;
2194
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002195 case ACT_HTTP_REPLACE_HDR:
2196 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002197 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2198 rule->arg.hdr_add.name_len,
2199 &rule->arg.hdr_add.fmt,
2200 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002201 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002202 break;
2203
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002204 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002205 ctx.idx = 0;
2206 /* remove all occurrences of the header */
2207 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002208 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002209 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2210 }
Willy Tarreau85603282015-01-21 20:39:27 +01002211 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002212
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002213 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002214 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002215 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002216
2217 replace = alloc_trash_chunk();
2218 if (!replace)
2219 return HTTP_RULE_RES_BADREQ;
2220
2221 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002222 memcpy(replace->area, rule->arg.hdr_add.name,
2223 rule->arg.hdr_add.name_len);
2224 replace->data = rule->arg.hdr_add.name_len;
2225 replace->area[replace->data++] = ':';
2226 replace->area[replace->data++] = ' ';
2227 replace->data += build_logline(s,
2228 replace->area + replace->data,
2229 replace->size - replace->data,
2230 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002231
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002232 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002233 /* remove all occurrences of the header */
2234 ctx.idx = 0;
2235 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002236 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002237 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2238 }
2239 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002240
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002241 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002242 static unsigned char rate_limit = 0;
2243
2244 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002245 replace->area[rule->arg.hdr_add.name_len] = 0;
2246 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,
2247 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002248 }
2249
2250 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2251 if (sess->fe != s->be)
2252 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2253 if (sess->listener->counters)
2254 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2255 if (objt_server(s->target))
2256 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2257 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002258
2259 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002260 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002261 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002262
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002263 case ACT_HTTP_DEL_ACL:
2264 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002265 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002266 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002267
2268 /* collect reference */
2269 ref = pat_ref_lookup(rule->arg.map.ref);
2270 if (!ref)
2271 continue;
2272
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002273 /* allocate key */
2274 key = alloc_trash_chunk();
2275 if (!key)
2276 return HTTP_RULE_RES_BADREQ;
2277
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002278 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002279 key->data = build_logline(s, key->area, key->size,
2280 &rule->arg.map.key);
2281 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002282
2283 /* perform update */
2284 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002285 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002286 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002287 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002288
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002289 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002290 break;
2291 }
2292
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002293 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002294 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002295 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002296
2297 /* collect reference */
2298 ref = pat_ref_lookup(rule->arg.map.ref);
2299 if (!ref)
2300 continue;
2301
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002302 /* allocate key */
2303 key = alloc_trash_chunk();
2304 if (!key)
2305 return HTTP_RULE_RES_BADREQ;
2306
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002307 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002308 key->data = build_logline(s, key->area, key->size,
2309 &rule->arg.map.key);
2310 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002311
2312 /* perform update */
2313 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002314 if (pat_ref_find_elt(ref, key->area) == NULL)
2315 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002316
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002317 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002318 break;
2319 }
2320
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002321 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002322 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002323 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002324
2325 /* collect reference */
2326 ref = pat_ref_lookup(rule->arg.map.ref);
2327 if (!ref)
2328 continue;
2329
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002330 /* allocate key */
2331 key = alloc_trash_chunk();
2332 if (!key)
2333 return HTTP_RULE_RES_BADREQ;
2334
2335 /* allocate value */
2336 value = alloc_trash_chunk();
2337 if (!value) {
2338 free_trash_chunk(key);
2339 return HTTP_RULE_RES_BADREQ;
2340 }
2341
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002342 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002343 key->data = build_logline(s, key->area, key->size,
2344 &rule->arg.map.key);
2345 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002346
2347 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002348 value->data = build_logline(s, value->area,
2349 value->size,
2350 &rule->arg.map.value);
2351 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002352
2353 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002354 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002355 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002356 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002357 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002358 else
2359 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002360 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002361 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002362 free_trash_chunk(key);
2363 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002364 break;
2365 }
William Lallemand73025dd2014-04-24 14:38:37 +02002366
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002367 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02002368 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2369 return HTTP_RULE_RES_BADREQ;
2370 return HTTP_RULE_RES_DONE;
2371
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002372 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2373 /* Note: only the first valid tracking parameter of each
2374 * applies.
2375 */
2376
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002377 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002378 struct stktable *t;
2379 struct stksess *ts;
2380 struct stktable_key *key;
2381 void *ptr;
2382
2383 t = rule->arg.trk_ctr.table.t;
2384 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2385
2386 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002387 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002388
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002389 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002390
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002391 /* let's count a new HTTP request as it's the first time we do it */
2392 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2393 if (ptr)
2394 stktable_data_cast(ptr, http_req_cnt)++;
2395
2396 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2397 if (ptr)
2398 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2399 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2400
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002401 /* When the client triggers a 4xx from the server, it's most often due
2402 * to a missing object or permission. These events should be tracked
2403 * because if they happen often, it may indicate a brute force or a
2404 * vulnerability scan. Normally this is done when receiving the response
2405 * but here we're tracking after this ought to have been done so we have
2406 * to do it on purpose.
2407 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002408 if ((unsigned)(txn->status - 400) < 100) {
2409 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2410 if (ptr)
2411 stktable_data_cast(ptr, http_err_cnt)++;
2412
2413 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2414 if (ptr)
2415 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2416 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2417 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002418
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002419 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002420
Emeric Brun0fed0b02017-11-29 16:15:07 +01002421 /* If data was modified, we need to touch to re-schedule sync */
2422 stktable_touch_local(t, ts, 0);
2423
Emeric Brun819fc6f2017-06-13 19:37:32 +02002424 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2425 if (sess->fe != s->be)
2426 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2427
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002428 }
2429 }
2430 break;
2431
Thierry FOURNIER42148732015-09-02 17:17:33 +02002432 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002433 if ((s->req.flags & CF_READ_ERROR) ||
2434 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2435 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2436 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002437 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002438
Willy Tarreauacc98002015-09-27 23:34:39 +02002439 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002440 case ACT_RET_ERR:
2441 case ACT_RET_CONT:
2442 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002443 case ACT_RET_STOP:
2444 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002445 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002446 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002447 return HTTP_RULE_RES_YIELD;
2448 }
William Lallemand73025dd2014-04-24 14:38:37 +02002449 break;
2450
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002451 /* other flags exists, but normaly, they never be matched. */
2452 default:
2453 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002454 }
2455 }
2456
2457 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002458 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002459}
2460
2461
Willy Tarreau71241ab2012-12-27 11:30:54 +01002462/* Perform an HTTP redirect based on the information in <rule>. The function
2463 * returns non-zero on success, or zero in case of a, irrecoverable error such
2464 * as too large a request to build a valid response.
2465 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002466static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002467{
Willy Tarreaub329a312015-05-22 16:27:37 +02002468 struct http_msg *req = &txn->req;
2469 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002470 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002471 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002472 int ret = 0;
2473
2474 chunk = alloc_trash_chunk();
2475 if (!chunk)
2476 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002477
2478 /* build redirect message */
2479 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002480 case 308:
2481 msg_fmt = HTTP_308;
2482 break;
2483 case 307:
2484 msg_fmt = HTTP_307;
2485 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002486 case 303:
2487 msg_fmt = HTTP_303;
2488 break;
2489 case 301:
2490 msg_fmt = HTTP_301;
2491 break;
2492 case 302:
2493 default:
2494 msg_fmt = HTTP_302;
2495 break;
2496 }
2497
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002498 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2499 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002500
2501 switch(rule->type) {
2502 case REDIRECT_TYPE_SCHEME: {
2503 const char *path;
2504 const char *host;
2505 struct hdr_ctx ctx;
2506 int pathlen;
2507 int hostlen;
2508
2509 host = "";
2510 hostlen = 0;
2511 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002512 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002513 host = ctx.line + ctx.val;
2514 hostlen = ctx.vlen;
2515 }
2516
Willy Tarreau6b952c82018-09-10 17:45:34 +02002517 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002518 /* build message using path */
2519 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002520 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002521 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2522 int qs = 0;
2523 while (qs < pathlen) {
2524 if (path[qs] == '?') {
2525 pathlen = qs;
2526 break;
2527 }
2528 qs++;
2529 }
2530 }
2531 } else {
2532 path = "/";
2533 pathlen = 1;
2534 }
2535
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002536 if (rule->rdr_str) { /* this is an old "redirect" rule */
2537 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002538 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002539 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002540
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002541 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002542 memcpy(chunk->area + chunk->data, rule->rdr_str,
2543 rule->rdr_len);
2544 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002545 }
2546 else {
2547 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002548 chunk->data += build_logline(s,
2549 chunk->area + chunk->data,
2550 chunk->size - chunk->data,
2551 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002552
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002553 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002554 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002555 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002556 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002557 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002558 memcpy(chunk->area + chunk->data, "://", 3);
2559 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002560
2561 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002562 memcpy(chunk->area + chunk->data, host, hostlen);
2563 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002564
2565 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002566 memcpy(chunk->area + chunk->data, path, pathlen);
2567 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002568
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002569 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002570 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002571 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002572 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002573 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002574 chunk->area[chunk->data] = '/';
2575 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002576 }
2577
2578 break;
2579 }
2580 case REDIRECT_TYPE_PREFIX: {
2581 const char *path;
2582 int pathlen;
2583
Willy Tarreau6b952c82018-09-10 17:45:34 +02002584 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002585 /* build message using path */
2586 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002587 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002588 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2589 int qs = 0;
2590 while (qs < pathlen) {
2591 if (path[qs] == '?') {
2592 pathlen = qs;
2593 break;
2594 }
2595 qs++;
2596 }
2597 }
2598 } else {
2599 path = "/";
2600 pathlen = 1;
2601 }
2602
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002603 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002604 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002605 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002606
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002607 /* add prefix. Note that if prefix == "/", we don't want to
2608 * add anything, otherwise it makes it hard for the user to
2609 * configure a self-redirection.
2610 */
2611 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002612 memcpy(chunk->area + chunk->data,
2613 rule->rdr_str, rule->rdr_len);
2614 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002615 }
2616 }
2617 else {
2618 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002619 chunk->data += build_logline(s,
2620 chunk->area + chunk->data,
2621 chunk->size - chunk->data,
2622 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002623
2624 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002625 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002626 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002627 }
2628
2629 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002630 memcpy(chunk->area + chunk->data, path, pathlen);
2631 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002632
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002633 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002634 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002635 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002636 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002637 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002638 chunk->area[chunk->data] = '/';
2639 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002640 }
2641
2642 break;
2643 }
2644 case REDIRECT_TYPE_LOCATION:
2645 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002646 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002647 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002648 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002649
2650 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002651 memcpy(chunk->area + chunk->data, rule->rdr_str,
2652 rule->rdr_len);
2653 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002654 }
2655 else {
2656 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002657 chunk->data += build_logline(s,
2658 chunk->area + chunk->data,
2659 chunk->size - chunk->data,
2660 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002661
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002662 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002663 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002664 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002665 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002666 break;
2667 }
2668
2669 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002670 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2671 chunk->data += 14;
2672 memcpy(chunk->area + chunk->data, rule->cookie_str,
2673 rule->cookie_len);
2674 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002675 }
2676
Willy Tarreau19b14122017-02-28 09:48:11 +01002677 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002678 txn->status = rule->code;
2679 /* let's log the request time */
2680 s->logs.tv_request = now;
2681
Christopher Fauletbe821b92017-03-30 11:21:53 +02002682 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002683 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2684 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2685 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002686 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002687 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002688 memcpy(chunk->area + chunk->data,
2689 "\r\nProxy-Connection: keep-alive", 30);
2690 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002691 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002692 memcpy(chunk->area + chunk->data,
2693 "\r\nConnection: keep-alive", 24);
2694 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002695 }
2696 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002697 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2698 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002699 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002700 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002701 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002702 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002703 req->next -= req->sov;
2704 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002705 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002706 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002707 req->msg_state = HTTP_MSG_CLOSED;
2708 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002709 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002710 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002711 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002712 /* let the server side turn to SI_ST_CLO */
2713 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002714 } else {
2715 /* keep-alive not possible */
2716 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002717 memcpy(chunk->area + chunk->data,
2718 "\r\nProxy-Connection: close\r\n\r\n", 29);
2719 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002720 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002721 memcpy(chunk->area + chunk->data,
2722 "\r\nConnection: close\r\n\r\n", 23);
2723 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002724 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002725 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002726 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002727 }
2728
Willy Tarreaue7dff022015-04-03 01:14:29 +02002729 if (!(s->flags & SF_ERR_MASK))
2730 s->flags |= SF_ERR_LOCAL;
2731 if (!(s->flags & SF_FINST_MASK))
2732 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002733
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002734 ret = 1;
2735 leave:
2736 free_trash_chunk(chunk);
2737 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002738}
2739
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002740/* This stream analyser runs all HTTP request processing which is common to
2741 * frontends and backends, which means blocking ACLs, filters, connection-close,
2742 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002743 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002744 * either needs more data or wants to immediately abort the request (eg: deny,
2745 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002746 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002747int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002748{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002749 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002750 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002751 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002752 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002753 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002754 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002755 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002756 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002757
Willy Tarreau655dce92009-11-08 13:10:58 +01002758 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002759 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002760 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002761 }
2762
Christopher Faulet45073512018-07-20 10:16:29 +02002763 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 +02002764 now_ms, __FUNCTION__,
2765 s,
2766 req,
2767 req->rex, req->wex,
2768 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002769 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002770 req->analysers);
2771
Willy Tarreau65410832014-04-28 21:25:43 +02002772 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002773 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002774
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002775 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002776 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002777 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002778
Willy Tarreau0b748332014-04-29 00:13:29 +02002779 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002780 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2781 goto return_prx_yield;
2782
Willy Tarreau0b748332014-04-29 00:13:29 +02002783 case HTTP_RULE_RES_CONT:
2784 case HTTP_RULE_RES_STOP: /* nothing to do */
2785 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002786
Willy Tarreau0b748332014-04-29 00:13:29 +02002787 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2788 if (txn->flags & TX_CLTARPIT)
2789 goto tarpit;
2790 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002791
Willy Tarreau0b748332014-04-29 00:13:29 +02002792 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2793 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002794
Willy Tarreau0b748332014-04-29 00:13:29 +02002795 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002796 goto done;
2797
Willy Tarreau0b748332014-04-29 00:13:29 +02002798 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2799 goto return_bad_req;
2800 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002801 }
2802
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002803 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2804 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002805 struct hdr_ctx ctx;
2806
2807 ctx.idx = 0;
2808 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002809 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002810 if (unlikely(http_header_add_tail2(&txn->req,
2811 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002812 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002813 goto return_bad_req;
2814 }
2815 }
2816
2817 }
2818
Willy Tarreau52542592014-04-28 18:33:26 +02002819 /* OK at this stage, we know that the request was accepted according to
2820 * the http-request rules, we can check for the stats. Note that the
2821 * URI is detected *before* the req* rules in order not to be affected
2822 * by a possible reqrep, while they are processed *after* so that a
2823 * reqdeny can still block them. This clearly needs to change in 1.6!
2824 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002825 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002826 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01002827 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002828 txn->status = 500;
2829 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002830 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002831
Willy Tarreaue7dff022015-04-03 01:14:29 +02002832 if (!(s->flags & SF_ERR_MASK))
2833 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002834 goto return_prx_cond;
2835 }
2836
2837 /* parse the whole stats request and extract the relevant information */
2838 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002839 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002840 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002841
Willy Tarreau0b748332014-04-29 00:13:29 +02002842 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2843 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002844
Willy Tarreau0b748332014-04-29 00:13:29 +02002845 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2846 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002847 }
2848
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002849 /* evaluate the req* rules except reqadd */
2850 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002851 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002852 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002853
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002854 if (txn->flags & TX_CLDENY)
2855 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002856
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002857 if (txn->flags & TX_CLTARPIT) {
2858 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002859 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002860 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002861 }
Willy Tarreau06619262006-12-17 08:37:22 +01002862
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002863 /* add request headers from the rule sets in the same order */
2864 list_for_each_entry(wl, &px->req_add, list) {
2865 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002866 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002867 ret = acl_pass(ret);
2868 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2869 ret = !ret;
2870 if (!ret)
2871 continue;
2872 }
2873
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002874 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002875 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002876 }
2877
Willy Tarreau52542592014-04-28 18:33:26 +02002878
2879 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002880 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2881 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002882 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002883 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002884 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002885
Willy Tarreaue7dff022015-04-03 01:14:29 +02002886 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2887 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2888 if (!(s->flags & SF_FINST_MASK))
2889 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002890
Willy Tarreau70730dd2014-04-24 18:06:27 +02002891 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002892 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2893 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002894 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002895 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002896 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002897
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002898 /* check whether we have some ACLs set to redirect this request */
2899 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002900 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002901 int ret;
2902
Willy Tarreau192252e2015-04-04 01:47:55 +02002903 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002904 ret = acl_pass(ret);
2905 if (rule->cond->pol == ACL_COND_UNLESS)
2906 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002907 if (!ret)
2908 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01002909 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002910 if (!http_apply_redirect_rule(rule, s, txn))
2911 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002912 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002913 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002914
Willy Tarreau2be39392010-01-03 17:24:51 +01002915 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2916 * If this happens, then the data will not come immediately, so we must
2917 * send all what we have without waiting. Note that due to the small gain
2918 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002919 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01002920 * itself once used.
2921 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002922 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01002923
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002924 done: /* done with this analyser, continue with next ones that the calling
2925 * points will have set, if any.
2926 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002927 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002928 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
2929 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002930 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002931
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002932 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02002933 /* Allow cookie logging
2934 */
2935 if (s->be->cookie_name || sess->fe->capture_name)
2936 manage_client_side_cookies(s, req);
2937
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002938 /* When a connection is tarpitted, we use the tarpit timeout,
2939 * which may be the same as the connect timeout if unspecified.
2940 * If unset, then set it to zero because we really want it to
2941 * eventually expire. We build the tarpit as an analyser.
2942 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002943 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002944
2945 /* wipe the request out so that we can drop the connection early
2946 * if the client closes first.
2947 */
2948 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002949
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002950 txn->status = http_err_codes[deny_status];
2951
Christopher Faulet0184ea72017-01-05 14:06:34 +01002952 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002953 req->analysers |= AN_REQ_HTTP_TARPIT;
2954 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2955 if (!req->analyse_exp)
2956 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02002957 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002958 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002959 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002960 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002961 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002962 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002963 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002964
2965 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002966
2967 /* Allow cookie logging
2968 */
2969 if (s->be->cookie_name || sess->fe->capture_name)
2970 manage_client_side_cookies(s, req);
2971
Willy Tarreau0b748332014-04-29 00:13:29 +02002972 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002973 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002974 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002975 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02002976 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002977 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002978 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002979 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002980 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002981 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002982 goto return_prx_cond;
2983
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002984 return_bad_req:
2985 /* We centralize bad requests processing here */
2986 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2987 /* we detected a parsing error. We want to archive this request
2988 * in the dedicated proxy area for later troubleshooting.
2989 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02002990 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002991 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002992
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002993 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002994 txn->req.msg_state = HTTP_MSG_ERROR;
2995 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002996 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002997
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002998 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002999 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003000 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003001
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003002 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003003 if (!(s->flags & SF_ERR_MASK))
3004 s->flags |= SF_ERR_PRXCOND;
3005 if (!(s->flags & SF_FINST_MASK))
3006 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003007
Christopher Faulet0184ea72017-01-05 14:06:34 +01003008 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003009 req->analyse_exp = TICK_ETERNITY;
3010 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003011
3012 return_prx_yield:
3013 channel_dont_connect(req);
3014 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003015}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003016
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003017/* This function performs all the processing enabled for the current request.
3018 * It returns 1 if the processing can continue on next analysers, or zero if it
3019 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003020 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003021 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003022int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003023{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003024 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003025 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003026 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003027 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003028
Willy Tarreau655dce92009-11-08 13:10:58 +01003029 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003030 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003031 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003032 return 0;
3033 }
3034
Christopher Faulet45073512018-07-20 10:16:29 +02003035 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 +02003036 now_ms, __FUNCTION__,
3037 s,
3038 req,
3039 req->rex, req->wex,
3040 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003041 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003042 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003043
Willy Tarreau59234e92008-11-30 23:51:27 +01003044 /*
3045 * Right now, we know that we have processed the entire headers
3046 * and that unwanted requests have been filtered out. We can do
3047 * whatever we want with the remaining request. Also, now we
3048 * may have separate values for ->fe, ->be.
3049 */
Willy Tarreau06619262006-12-17 08:37:22 +01003050
Willy Tarreau59234e92008-11-30 23:51:27 +01003051 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003052 * If HTTP PROXY is set we simply get remote server address parsing
3053 * incoming request. Note that this requires that a connection is
3054 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003055 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003056 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003057 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003058 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003059
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003060 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003061 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003062 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003063 txn->req.msg_state = HTTP_MSG_ERROR;
3064 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003065 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003066 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003067
Willy Tarreaue7dff022015-04-03 01:14:29 +02003068 if (!(s->flags & SF_ERR_MASK))
3069 s->flags |= SF_ERR_RESOURCE;
3070 if (!(s->flags & SF_FINST_MASK))
3071 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003072
3073 return 0;
3074 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003075
Willy Tarreau6b952c82018-09-10 17:45:34 +02003076 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003077 if (url2sa(ci_head(req) + msg->sl.rq.u,
3078 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003079 &conn->addr.to, NULL) == -1)
3080 goto return_bad_req;
3081
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003082 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003083 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3084 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003085 * u_l characters by a single "/".
3086 */
3087 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003088 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003089 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3090 int delta;
3091
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003092 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003093 http_msg_move_end(&txn->req, delta);
3094 cur_end += delta;
3095 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3096 goto return_bad_req;
3097 }
3098 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003099 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003100 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3101 int delta;
3102
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003103 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003104 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003105 http_msg_move_end(&txn->req, delta);
3106 cur_end += delta;
3107 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3108 goto return_bad_req;
3109 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003110 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003111
Willy Tarreau59234e92008-11-30 23:51:27 +01003112 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003113 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003114 * Note that doing so might move headers in the request, but
3115 * the fields will stay coherent and the URI will not move.
3116 * This should only be performed in the backend.
3117 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003118 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003119 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003120
William Lallemanda73203e2012-03-12 12:48:57 +01003121 /* add unique-id if "header-unique-id" is specified */
3122
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003123 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003124 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003125 goto return_bad_req;
3126 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003127 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003128 }
William Lallemanda73203e2012-03-12 12:48:57 +01003129
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003130 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003131 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003132 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003133 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003134 goto return_bad_req;
3135 }
3136
Cyril Bontéb21570a2009-11-29 20:04:48 +01003137 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003138 * 9: add X-Forwarded-For if either the frontend or the backend
3139 * asks for it.
3140 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003141 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003142 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003143 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3144 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3145 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003146 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003147 /* The header is set to be added only if none is present
3148 * and we found it, so don't do anything.
3149 */
3150 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003151 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003152 /* Add an X-Forwarded-For header unless the source IP is
3153 * in the 'except' network range.
3154 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003155 if ((!sess->fe->except_mask.s_addr ||
3156 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3157 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003158 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003159 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003160 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003161 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003162 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003163 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003164
3165 /* Note: we rely on the backend to get the header name to be used for
3166 * x-forwarded-for, because the header is really meant for the backends.
3167 * However, if the backend did not specify any option, we have to rely
3168 * on the frontend's header name.
3169 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003170 if (s->be->fwdfor_hdr_len) {
3171 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003172 memcpy(trash.area,
3173 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003174 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003175 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003176 memcpy(trash.area,
3177 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003178 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003179 len += snprintf(trash.area + len,
3180 trash.size - len,
3181 ": %d.%d.%d.%d", pn[0], pn[1],
3182 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003183
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003184 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003185 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003186 }
3187 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003188 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003189 /* FIXME: for the sake of completeness, we should also support
3190 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003191 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003192 int len;
3193 char pn[INET6_ADDRSTRLEN];
3194 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003195 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003196 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003197
Willy Tarreau59234e92008-11-30 23:51:27 +01003198 /* Note: we rely on the backend to get the header name to be used for
3199 * x-forwarded-for, because the header is really meant for the backends.
3200 * However, if the backend did not specify any option, we have to rely
3201 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003202 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003203 if (s->be->fwdfor_hdr_len) {
3204 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003205 memcpy(trash.area, s->be->fwdfor_hdr_name,
3206 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003207 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003208 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003209 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3210 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003211 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003212 len += snprintf(trash.area + len, trash.size - len,
3213 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003214
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003215 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003216 goto return_bad_req;
3217 }
3218 }
3219
3220 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003221 * 10: add X-Original-To if either the frontend or the backend
3222 * asks for it.
3223 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003224 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003225
3226 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003227 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003228 /* Add an X-Original-To header unless the destination IP is
3229 * in the 'except' network range.
3230 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003231 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003232
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003233 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003234 ((!sess->fe->except_mask_to.s_addr ||
3235 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3236 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003237 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003238 (((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 +02003239 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003240 int len;
3241 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003242 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003243
3244 /* Note: we rely on the backend to get the header name to be used for
3245 * x-original-to, because the header is really meant for the backends.
3246 * However, if the backend did not specify any option, we have to rely
3247 * on the frontend's header name.
3248 */
3249 if (s->be->orgto_hdr_len) {
3250 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003251 memcpy(trash.area,
3252 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003253 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003254 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003255 memcpy(trash.area,
3256 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003257 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003258 len += snprintf(trash.area + len,
3259 trash.size - len,
3260 ": %d.%d.%d.%d", pn[0], pn[1],
3261 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003262
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003263 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003264 goto return_bad_req;
3265 }
3266 }
3267 }
3268
Willy Tarreau50fc7772012-11-11 22:19:57 +01003269 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3270 * If an "Upgrade" token is found, the header is left untouched in order not to have
3271 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3272 * "Upgrade" is present in the Connection header.
3273 */
3274 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3275 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003276 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003277 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003278 unsigned int want_flags = 0;
3279
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003280 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003281 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003282 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003283 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003284 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003285 want_flags |= TX_CON_CLO_SET;
3286 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003287 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003288 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003289 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003290 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003291 want_flags |= TX_CON_KAL_SET;
3292 }
3293
3294 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003295 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003296 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003297
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003298
Willy Tarreau522d6c02009-12-06 18:49:18 +01003299 /* If we have no server assigned yet and we're balancing on url_param
3300 * with a POST request, we may be interested in checking the body for
3301 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003302 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003303 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003304 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003305 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003306 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003307 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003308 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003309
Christopher Fauletbe821b92017-03-30 11:21:53 +02003310 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3311 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003312#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003313 /* We expect some data from the client. Unless we know for sure
3314 * we already have a full request, we have to re-enable quick-ack
3315 * in case we previously disabled it, otherwise we might cause
3316 * the client to delay further data.
3317 */
3318 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3319 cli_conn && conn_ctrl_ready(cli_conn) &&
3320 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003321 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003322 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003323#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003324
Willy Tarreau59234e92008-11-30 23:51:27 +01003325 /*************************************************************
3326 * OK, that's finished for the headers. We have done what we *
3327 * could. Let's switch to the DATA state. *
3328 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003329 req->analyse_exp = TICK_ETERNITY;
3330 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003331
Willy Tarreau59234e92008-11-30 23:51:27 +01003332 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003333 /* OK let's go on with the BODY now */
3334 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003335
Willy Tarreau59234e92008-11-30 23:51:27 +01003336 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003337 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003338 /* we detected a parsing error. We want to archive this request
3339 * in the dedicated proxy area for later troubleshooting.
3340 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003341 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003342 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003343
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003344 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003345 txn->req.msg_state = HTTP_MSG_ERROR;
3346 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003347 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003348 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003349
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003350 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003351 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003352 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003353
Willy Tarreaue7dff022015-04-03 01:14:29 +02003354 if (!(s->flags & SF_ERR_MASK))
3355 s->flags |= SF_ERR_PRXCOND;
3356 if (!(s->flags & SF_FINST_MASK))
3357 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003358 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003359}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003360
Willy Tarreau60b85b02008-11-30 23:28:40 +01003361/* This function is an analyser which processes the HTTP tarpit. It always
3362 * returns zero, at the beginning because it prevents any other processing
3363 * from occurring, and at the end because it terminates the request.
3364 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003365int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003366{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003367 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003368
3369 /* This connection is being tarpitted. The CLIENT side has
3370 * already set the connect expiration date to the right
3371 * timeout. We just have to check that the client is still
3372 * there and that the timeout has not expired.
3373 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003374 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003375 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003376 !tick_is_expired(req->analyse_exp, now_ms))
3377 return 0;
3378
3379 /* We will set the queue timer to the time spent, just for
3380 * logging purposes. We fake a 500 server error, so that the
3381 * attacker will not suspect his connection has been tarpitted.
3382 * It will not cause trouble to the logs because we can exclude
3383 * the tarpitted connections by filtering on the 'PT' status flags.
3384 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003385 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3386
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003387 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003388 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003389
Christopher Faulet0184ea72017-01-05 14:06:34 +01003390 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003391 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003392
Willy Tarreaue7dff022015-04-03 01:14:29 +02003393 if (!(s->flags & SF_ERR_MASK))
3394 s->flags |= SF_ERR_PRXCOND;
3395 if (!(s->flags & SF_FINST_MASK))
3396 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003397 return 0;
3398}
3399
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003400/* This function is an analyser which waits for the HTTP request body. It waits
3401 * for either the buffer to be full, or the full advertised contents to have
3402 * reached the buffer. It must only be called after the standard HTTP request
3403 * processing has occurred, because it expects the request to be parsed and will
3404 * look for the Expect header. It may send a 100-Continue interim response. It
3405 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3406 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3407 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003408 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003409int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003410{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003411 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003412 struct http_txn *txn = s->txn;
3413 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003414
3415 /* We have to parse the HTTP request body to find any required data.
3416 * "balance url_param check_post" should have been the only way to get
3417 * into this. We were brought here after HTTP header analysis, so all
3418 * related structures are ready.
3419 */
3420
Willy Tarreau890988f2014-04-10 11:59:33 +02003421 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3422 /* This is the first call */
3423 if (msg->msg_state < HTTP_MSG_BODY)
3424 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003425
Willy Tarreau890988f2014-04-10 11:59:33 +02003426 if (msg->msg_state < HTTP_MSG_100_SENT) {
3427 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3428 * send an HTTP/1.1 100 Continue intermediate response.
3429 */
3430 if (msg->flags & HTTP_MSGF_VER_11) {
3431 struct hdr_ctx ctx;
3432 ctx.idx = 0;
3433 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003434 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003435 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003436 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003437 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003438 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003439 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003440 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003441 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003442
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003443 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003444 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003445 * must save the body in msg->next because it survives buffer
3446 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003447 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003448 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003449
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003450 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003451 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3452 else
3453 msg->msg_state = HTTP_MSG_DATA;
3454 }
3455
Willy Tarreau890988f2014-04-10 11:59:33 +02003456 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3457 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003458 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003459 goto missing_data;
3460
3461 /* OK we have everything we need now */
3462 goto http_end;
3463 }
3464
3465 /* OK here we're parsing a chunked-encoded message */
3466
Willy Tarreau522d6c02009-12-06 18:49:18 +01003467 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003468 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003469 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003470 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003471 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003472 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003473 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003474
Willy Tarreau115acb92009-12-26 13:56:06 +01003475 if (!ret)
3476 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003477 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003478 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003479 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003480 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003481 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003482 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003483 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003484
3485 msg->chunk_len = chunk;
3486 msg->body_len += chunk;
3487
3488 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003489 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003490 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003491 }
3492
Willy Tarreaud98cf932009-12-27 22:54:55 +01003493 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003494 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3495 * for at least a whole chunk or the whole content length bytes after
3496 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003497 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003498 if (msg->msg_state == HTTP_MSG_TRAILERS)
3499 goto http_end;
3500
Willy Tarreaue115b492015-05-01 23:05:14 +02003501 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003502 goto http_end;
3503
3504 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003505 /* we get here if we need to wait for more data. If the buffer is full,
3506 * we have the maximum we can expect.
3507 */
Willy Tarreau23752332018-06-15 14:54:53 +02003508 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003509 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003510
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003511 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003512 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003513 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003514
Willy Tarreaue7dff022015-04-03 01:14:29 +02003515 if (!(s->flags & SF_ERR_MASK))
3516 s->flags |= SF_ERR_CLITO;
3517 if (!(s->flags & SF_FINST_MASK))
3518 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003519 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003520 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003521
3522 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003523 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003524 /* Not enough data. We'll re-use the http-request
3525 * timeout here. Ideally, we should set the timeout
3526 * relative to the accept() date. We just set the
3527 * request timeout once at the beginning of the
3528 * request.
3529 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003530 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003531 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003532 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003533 return 0;
3534 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003535
3536 http_end:
3537 /* The situation will not evolve, so let's give up on the analysis. */
3538 s->logs.tv_request = now; /* update the request timer to reflect full request */
3539 req->analysers &= ~an_bit;
3540 req->analyse_exp = TICK_ETERNITY;
3541 return 1;
3542
3543 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003544 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003545 txn->req.msg_state = HTTP_MSG_ERROR;
3546 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003547 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003548
Willy Tarreaue7dff022015-04-03 01:14:29 +02003549 if (!(s->flags & SF_ERR_MASK))
3550 s->flags |= SF_ERR_PRXCOND;
3551 if (!(s->flags & SF_FINST_MASK))
3552 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003553
Willy Tarreau522d6c02009-12-06 18:49:18 +01003554 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003555 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003556 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003557 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003558 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003559 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003560}
3561
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003562/* send a server's name with an outgoing request over an established connection.
3563 * Note: this function is designed to be called once the request has been scheduled
3564 * for being forwarded. This is the reason why it rewinds the buffer before
3565 * proceeding.
3566 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003567int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003568
3569 struct hdr_ctx ctx;
3570
Mark Lamourinec2247f02012-01-04 13:02:01 -05003571 char *hdr_name = be->server_id_hdr_name;
3572 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003573 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003574 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003575 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003576
William Lallemandd9e90662012-01-30 17:27:17 +01003577 ctx.idx = 0;
3578
Willy Tarreau211cdec2014-04-17 20:18:08 +02003579 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003580 if (old_o) {
3581 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003582 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003583 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003584 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003585 }
3586
Willy Tarreauf37954d2018-06-15 18:31:02 +02003587 old_i = ci_data(chn);
3588 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 -05003589 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003590 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003591 }
3592
3593 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003594 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003595 memcpy(hdr_val, hdr_name, hdr_name_len);
3596 hdr_val += hdr_name_len;
3597 *hdr_val++ = ':';
3598 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003599 hdr_val += strlcpy2(hdr_val, srv_name,
3600 trash.area + trash.size - hdr_val);
3601 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3602 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003603
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003604 if (old_o) {
3605 /* If this was a forwarded request, we must readjust the amount of
3606 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003607 * variations. Note that the current state is >= HTTP_MSG_BODY,
3608 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003609 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003610 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003611 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003612 txn->req.next -= old_o;
3613 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003614 }
3615
Mark Lamourinec2247f02012-01-04 13:02:01 -05003616 return 0;
3617}
3618
Willy Tarreau610ecce2010-01-04 21:15:02 +01003619/* Terminate current transaction and prepare a new one. This is very tricky
3620 * right now but it works.
3621 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003622void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003623{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003624 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003625 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003626 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003627 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003628 struct connection *srv_conn;
3629 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003630 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003631
Willy Tarreau610ecce2010-01-04 21:15:02 +01003632 /* FIXME: We need a more portable way of releasing a backend's and a
3633 * server's connections. We need a safer way to reinitialize buffer
3634 * flags. We also need a more accurate method for computing per-request
3635 * data.
3636 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003637 /*
3638 * XXX cognet: This is probably wrong, this is killing a whole
3639 * connection, in the new world order, we probably want to just kill
3640 * the stream, this is to be revisited the day we handle multiple
3641 * streams in one server connection.
3642 */
3643 cs = objt_cs(s->si[1].end);
3644 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003645
Willy Tarreau4213a112013-12-15 10:25:42 +01003646 /* unless we're doing keep-alive, we want to quickly close the connection
3647 * to the server.
3648 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003649 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003650 !si_conn_ready(&s->si[1])) {
3651 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3652 si_shutr(&s->si[1]);
3653 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003654 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003655
Willy Tarreaue7dff022015-04-03 01:14:29 +02003656 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003657 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003658 if (unlikely(s->srv_conn))
3659 sess_change_server(s, NULL);
3660 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003661
3662 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003663 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003664
Willy Tarreaueee5b512015-04-03 23:46:31 +02003665 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003666 int n;
3667
Willy Tarreaueee5b512015-04-03 23:46:31 +02003668 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003669 if (n < 1 || n > 5)
3670 n = 0;
3671
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003672 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003673 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003674 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003675 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003676 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003677 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3678 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003679 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003680 }
3681
3682 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003683 s->logs.bytes_in -= ci_data(&s->req);
3684 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003685
Willy Tarreau66425e32018-07-25 06:55:12 +02003686 /* we may need to know the position in the queue */
3687 pendconn_free(s);
3688
Willy Tarreau610ecce2010-01-04 21:15:02 +01003689 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003690 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003691 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003692 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003693 s->do_log(s);
3694 }
3695
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003696 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003697 stream_stop_content_counters(s);
3698 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003699
Willy Tarreau610ecce2010-01-04 21:15:02 +01003700 s->logs.accept_date = date; /* user-visible date for logging */
3701 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003702 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3703 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003704 tv_zero(&s->logs.tv_request);
3705 s->logs.t_queue = -1;
3706 s->logs.t_connect = -1;
3707 s->logs.t_data = -1;
3708 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003709 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3710 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003711
Willy Tarreauf37954d2018-06-15 18:31:02 +02003712 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3713 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003714
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003715 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003716 if (s->flags & SF_CURR_SESS) {
3717 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02003718 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003719 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003720 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003721 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003722 }
3723
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003724 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003725
Willy Tarreau4213a112013-12-15 10:25:42 +01003726 /* only release our endpoint if we don't intend to reuse the
3727 * connection.
3728 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003729 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003730 !si_conn_ready(&s->si[1])) {
3731 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02003732 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003733 }
3734
Willy Tarreau350f4872014-11-28 14:42:25 +01003735 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3736 s->si[1].err_type = SI_ET_NONE;
3737 s->si[1].conn_retries = 0; /* used for logging too */
3738 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003739 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 +01003740 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);
Christopher Fauletc5a9d5b2017-11-09 09:36:43 +01003741 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|CF_WRITE_EVENT);
Willy Tarreaue7dff022015-04-03 01:14:29 +02003742 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3743 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3744 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003745
Patrick Hemmere3faf022018-08-22 10:02:00 -04003746 hlua_ctx_destroy(s->hlua);
3747 s->hlua = NULL;
3748
Willy Tarreaueee5b512015-04-03 23:46:31 +02003749 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003750 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003751 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003752
3753 if (prev_status == 401 || prev_status == 407) {
3754 /* In HTTP keep-alive mode, if we receive a 401, we still have
3755 * a chance of being able to send the visitor again to the same
3756 * server over the same connection. This is required by some
3757 * broken protocols such as NTLM, and anyway whenever there is
3758 * an opportunity for sending the challenge to the proper place,
3759 * it's better to do it (at least it helps with debugging).
3760 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003761 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02003762 if (srv_conn)
3763 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01003764 }
3765
Willy Tarreau53f96852016-02-02 18:50:47 +01003766 /* Never ever allow to reuse a connection from a non-reuse backend */
3767 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3768 srv_conn->flags |= CO_FL_PRIVATE;
3769
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003770 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003771 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003772
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003773 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003774 s->req.flags |= CF_NEVER_WAIT;
3775 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003776 }
3777
Willy Tarreau714ea782015-11-25 20:11:11 +01003778 /* we're removing the analysers, we MUST re-enable events detection.
3779 * We don't enable close on the response channel since it's either
3780 * already closed, or in keep-alive with an idle connection handler.
3781 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003782 channel_auto_read(&s->req);
3783 channel_auto_close(&s->req);
3784 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003785
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003786 /* we're in keep-alive with an idle connection, monitor it if not already done */
3787 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003788 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02003789 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003790 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01003791 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003792 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02003793 else if (prev_flags & TX_NOT_FIRST)
3794 /* note: we check the request, not the connection, but
3795 * this is valid for strategies SAFE and AGGR, and in
3796 * case of ALWS, we don't care anyway.
3797 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003798 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02003799 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003800 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003801 }
Christopher Faulete6006242017-03-10 11:52:44 +01003802 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3803 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003804}
3805
3806
3807/* This function updates the request state machine according to the response
3808 * state machine and buffer flags. It returns 1 if it changes anything (flag
3809 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3810 * it is only used to find when a request/response couple is complete. Both
3811 * this function and its equivalent should loop until both return zero. It
3812 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3813 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003814int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003815{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003816 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003817 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003818 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003819 unsigned int old_state = txn->req.msg_state;
3820
Christopher Faulet4be98032017-07-18 10:48:24 +02003821 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003822 return 0;
3823
3824 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003825 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003826 * We can shut the read side unless we want to abort_on_close,
3827 * or we have a POST request. The issue with POST requests is
3828 * that some browsers still send a CRLF after the request, and
3829 * this CRLF must be read so that it does not remain in the kernel
3830 * buffers, otherwise a close could cause an RST on some systems
3831 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003832 * Note that if we're using keep-alive on the client side, we'd
3833 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003834 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003835 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003836 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003837 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3838 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003839 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3840 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003841 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003842 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003843
Willy Tarreau40f151a2012-12-20 12:10:09 +01003844 /* if the server closes the connection, we want to immediately react
3845 * and close the socket to save packets and syscalls.
3846 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003847 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003848
Willy Tarreau7f876a12015-11-18 11:59:55 +01003849 /* In any case we've finished parsing the request so we must
3850 * disable Nagle when sending data because 1) we're not going
3851 * to shut this side, and 2) the server is waiting for us to
3852 * send pending data.
3853 */
3854 chn->flags |= CF_NEVER_WAIT;
3855
Willy Tarreau610ecce2010-01-04 21:15:02 +01003856 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3857 goto wait_other_side;
3858
3859 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3860 /* The server has not finished to respond, so we
3861 * don't want to move in order not to upset it.
3862 */
3863 goto wait_other_side;
3864 }
3865
Willy Tarreau610ecce2010-01-04 21:15:02 +01003866 /* When we get here, it means that both the request and the
3867 * response have finished receiving. Depending on the connection
3868 * mode, we'll have to wait for the last bytes to leave in either
3869 * direction, and sometimes for a close to be effective.
3870 */
3871
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003872 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3873 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003874 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3875 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003876 }
3877 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3878 /* Option forceclose is set, or either side wants to close,
3879 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003880 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003881 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003882 *
3883 * However, there is an exception if the response
3884 * length is undefined. In this case, we need to wait
3885 * the close from the server. The response will be
3886 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003887 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003888 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3889 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3890 goto check_channel_flags;
3891
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003892 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3893 channel_shutr_now(chn);
3894 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003895 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003896 }
3897 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003898 /* The last possible modes are keep-alive and tunnel. Tunnel mode
3899 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003900 */
Willy Tarreau4213a112013-12-15 10:25:42 +01003901 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
3902 channel_auto_read(chn);
3903 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003904 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003905 }
3906
Christopher Faulet4be98032017-07-18 10:48:24 +02003907 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003908 }
3909
3910 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3911 http_msg_closing:
3912 /* nothing else to forward, just waiting for the output buffer
3913 * to be empty and for the shutw_now to take effect.
3914 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003915 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003916 txn->req.msg_state = HTTP_MSG_CLOSED;
3917 goto http_msg_closed;
3918 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003919 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003920 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003921 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003922 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003923 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003924 }
3925
3926 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3927 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01003928 /* if we don't know whether the server will close, we need to hard close */
3929 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
3930 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3931
Willy Tarreau3988d932013-12-27 23:03:08 +01003932 /* see above in MSG_DONE why we only do this in these states */
3933 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3934 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003935 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3936 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01003937 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003938 goto wait_other_side;
3939 }
3940
Christopher Faulet4be98032017-07-18 10:48:24 +02003941 check_channel_flags:
3942 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
3943 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
3944 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02003945 txn->req.msg_state = HTTP_MSG_CLOSING;
3946 goto http_msg_closing;
3947 }
3948
3949
Willy Tarreau610ecce2010-01-04 21:15:02 +01003950 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003951 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003952}
3953
3954
3955/* This function updates the response state machine according to the request
3956 * state machine and buffer flags. It returns 1 if it changes anything (flag
3957 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3958 * it is only used to find when a request/response couple is complete. Both
3959 * this function and its equivalent should loop until both return zero. It
3960 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3961 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003962int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003963{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003964 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003965 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003966 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003967 unsigned int old_state = txn->rsp.msg_state;
3968
Christopher Faulet4be98032017-07-18 10:48:24 +02003969 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003970 return 0;
3971
3972 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3973 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003974 * still monitor the server connection for a possible close
3975 * while the request is being uploaded, so we don't disable
3976 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003977 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003978 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003979
3980 if (txn->req.msg_state == HTTP_MSG_ERROR)
3981 goto wait_other_side;
3982
3983 if (txn->req.msg_state < HTTP_MSG_DONE) {
3984 /* The client seems to still be sending data, probably
3985 * because we got an error response during an upload.
3986 * We have the choice of either breaking the connection
3987 * or letting it pass through. Let's do the later.
3988 */
3989 goto wait_other_side;
3990 }
3991
Willy Tarreau610ecce2010-01-04 21:15:02 +01003992 /* When we get here, it means that both the request and the
3993 * response have finished receiving. Depending on the connection
3994 * mode, we'll have to wait for the last bytes to leave in either
3995 * direction, and sometimes for a close to be effective.
3996 */
3997
3998 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3999 /* Server-close mode : shut read and wait for the request
4000 * side to close its output buffer. The caller will detect
4001 * when we're in DONE and the other is in CLOSED and will
4002 * catch that for the final cleanup.
4003 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004004 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4005 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004006 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004007 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4008 /* Option forceclose is set, or either side wants to close,
4009 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004010 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004011 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004012 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004013 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004014 channel_shutr_now(chn);
4015 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004016 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004017 }
4018 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004019 /* The last possible modes are keep-alive and tunnel. Tunnel will
4020 * need to forward remaining data. Keep-alive will need to monitor
4021 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004022 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004023 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004024 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004025 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4026 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004027 }
4028
Christopher Faulet4be98032017-07-18 10:48:24 +02004029 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004030 }
4031
4032 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4033 http_msg_closing:
4034 /* nothing else to forward, just waiting for the output buffer
4035 * to be empty and for the shutw_now to take effect.
4036 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004037 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004038 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4039 goto http_msg_closed;
4040 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004041 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004042 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004043 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004044 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004045 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004046 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004047 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004048 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004049 }
4050
4051 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4052 http_msg_closed:
4053 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004054 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004055 channel_auto_close(chn);
4056 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004057 goto wait_other_side;
4058 }
4059
Christopher Faulet4be98032017-07-18 10:48:24 +02004060 check_channel_flags:
4061 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4062 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4063 /* if we've just closed an output, let's switch */
4064 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4065 goto http_msg_closing;
4066 }
4067
Willy Tarreau610ecce2010-01-04 21:15:02 +01004068 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004069 /* We force the response to leave immediately if we're waiting for the
4070 * other side, since there is no pending shutdown to push it out.
4071 */
4072 if (!channel_is_empty(chn))
4073 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004074 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004075}
4076
4077
Christopher Faulet894da4c2017-07-18 11:29:07 +02004078/* Resync the request and response state machines. */
4079void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004080{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004081 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004082#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004083 int old_req_state = txn->req.msg_state;
4084 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004085#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004086
Willy Tarreau610ecce2010-01-04 21:15:02 +01004087 http_sync_req_state(s);
4088 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004089 if (!http_sync_res_state(s))
4090 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004091 if (!http_sync_req_state(s))
4092 break;
4093 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004094
Christopher Faulet894da4c2017-07-18 11:29:07 +02004095 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4096 "req->analysers=0x%08x res->analysers=0x%08x\n",
4097 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004098 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4099 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004100 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004101
4102
Willy Tarreau610ecce2010-01-04 21:15:02 +01004103 /* OK, both state machines agree on a compatible state.
4104 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004105 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4106 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004107 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4108 * means we must abort the request.
4109 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4110 * corresponding channel.
4111 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4112 * on the response with server-close mode means we've completed one
4113 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004114 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004115 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4116 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004117 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004118 channel_auto_close(&s->req);
4119 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004120 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004121 channel_auto_close(&s->res);
4122 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004123 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004124 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4125 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004126 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004127 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004128 channel_auto_close(&s->res);
4129 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004130 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004131 channel_abort(&s->req);
4132 channel_auto_close(&s->req);
4133 channel_auto_read(&s->req);
4134 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004135 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004136 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4137 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4138 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4139 s->req.analysers &= AN_REQ_FLT_END;
4140 if (HAS_REQ_DATA_FILTERS(s))
4141 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4142 }
4143 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4144 s->res.analysers &= AN_RES_FLT_END;
4145 if (HAS_RSP_DATA_FILTERS(s))
4146 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4147 }
4148 channel_auto_close(&s->req);
4149 channel_auto_read(&s->req);
4150 channel_auto_close(&s->res);
4151 channel_auto_read(&s->res);
4152 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004153 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4154 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004155 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004156 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4157 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4158 /* server-close/keep-alive: terminate this transaction,
4159 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004160 * a fresh-new transaction, but only once we're sure there's
4161 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004162 * another request. They must not hold any pending output data
4163 * and the response buffer must realigned
4164 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004165 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004166 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004167 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004168 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004169 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004170 else {
4171 s->req.analysers = AN_REQ_FLT_END;
4172 s->res.analysers = AN_RES_FLT_END;
4173 txn->flags |= TX_WAIT_CLEANUP;
4174 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004175 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004176}
4177
Willy Tarreaud98cf932009-12-27 22:54:55 +01004178/* This function is an analyser which forwards request body (including chunk
4179 * sizes if any). It is called as soon as we must forward, even if we forward
4180 * zero byte. The only situation where it must not be called is when we're in
4181 * tunnel mode and we want to forward till the close. It's used both to forward
4182 * remaining data and to resync after end of body. It expects the msg_state to
4183 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004184 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004185 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004186 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004187 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004188int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004189{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004190 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004191 struct http_txn *txn = s->txn;
4192 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004193 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004194
Christopher Faulet45073512018-07-20 10:16:29 +02004195 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 +02004196 now_ms, __FUNCTION__,
4197 s,
4198 req,
4199 req->rex, req->wex,
4200 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004201 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004202 req->analysers);
4203
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004204 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4205 return 0;
4206
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004207 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004208 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004209 /* Output closed while we were sending data. We must abort and
4210 * wake the other side up.
4211 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004212 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004213 msg->msg_state = HTTP_MSG_ERROR;
4214 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004215 return 1;
4216 }
4217
Willy Tarreaud98cf932009-12-27 22:54:55 +01004218 /* Note that we don't have to send 100-continue back because we don't
4219 * need the data to complete our job, and it's up to the server to
4220 * decide whether to return 100, 417 or anything else in return of
4221 * an "Expect: 100-continue" header.
4222 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004223 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004224 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4225 ? HTTP_MSG_CHUNK_SIZE
4226 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004227
4228 /* TODO/filters: when http-buffer-request option is set or if a
4229 * rule on url_param exists, the first chunk size could be
4230 * already parsed. In that case, msg->next is after the chunk
4231 * size (including the CRLF after the size). So this case should
4232 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004233 }
4234
Willy Tarreau7ba23542014-04-17 21:50:00 +02004235 /* Some post-connect processing might want us to refrain from starting to
4236 * forward data. Currently, the only reason for this is "balance url_param"
4237 * whichs need to parse/process the request after we've enabled forwarding.
4238 */
4239 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004240 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004241 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004242 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004243 channel_dont_close(req); /* don't fail on early shutr */
4244 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004245 }
4246 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4247 }
4248
Willy Tarreau80a92c02014-03-12 10:41:13 +01004249 /* in most states, we should abort in case of early close */
4250 channel_auto_close(req);
4251
Willy Tarreauefdf0942014-04-24 20:08:57 +02004252 if (req->to_forward) {
4253 /* We can't process the buffer's contents yet */
4254 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004255 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004256 }
4257
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004258 if (msg->msg_state < HTTP_MSG_DONE) {
4259 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4260 ? http_msg_forward_chunked_body(s, msg)
4261 : http_msg_forward_body(s, msg));
4262 if (!ret)
4263 goto missing_data_or_waiting;
4264 if (ret < 0)
4265 goto return_bad_req;
4266 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004267
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004268 /* other states, DONE...TUNNEL */
4269 /* we don't want to forward closes on DONE except in tunnel mode. */
4270 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4271 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004272
Christopher Faulet894da4c2017-07-18 11:29:07 +02004273 http_resync_states(s);
4274 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004275 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4276 if (req->flags & CF_SHUTW) {
4277 /* request errors are most likely due to the
4278 * server aborting the transfer. */
4279 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004280 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004281 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004282 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004283 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004284 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004285 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004286 }
4287
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004288 /* If "option abortonclose" is set on the backend, we want to monitor
4289 * the client's connection and forward any shutdown notification to the
4290 * server, which will decide whether to close or to go on processing the
4291 * request. We only do that in tunnel mode, and not in other modes since
4292 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004293 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004294 channel_auto_read(req);
4295 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4296 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4297 s->si[1].flags |= SI_FL_NOLINGER;
4298 channel_auto_close(req);
4299 }
4300 else if (s->txn->meth == HTTP_METH_POST) {
4301 /* POST requests may require to read extra CRLF sent by broken
4302 * browsers and which could cause an RST to be sent upon close
4303 * on some systems (eg: Linux). */
4304 channel_auto_read(req);
4305 }
4306 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004307
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004308 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004309 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004310 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004311 if (!(s->flags & SF_ERR_MASK))
4312 s->flags |= SF_ERR_CLICL;
4313 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004314 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004315 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004316 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004317 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004318 }
4319
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004320 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4321 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004322 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004323 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004324
4325 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004326 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004327
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004328 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004329 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004330 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004331 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004332
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004333 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004334 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004335 * And when content-length is used, we never want to let the possible
4336 * shutdown be forwarded to the other side, as the state machine will
4337 * take care of it once the client responds. It's also important to
4338 * prevent TIME_WAITs from accumulating on the backend side, and for
4339 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004340 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004341 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004342 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004343
Willy Tarreau5c620922011-05-11 19:56:11 +02004344 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004345 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004346 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004347 * modes are already handled by the stream sock layer. We must not do
4348 * this in content-length mode because it could present the MSG_MORE
4349 * flag with the last block of forwarded data, which would cause an
4350 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004351 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004352 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004353 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004354
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004355 return 0;
4356
Willy Tarreaud98cf932009-12-27 22:54:55 +01004357 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004358 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004359 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004360 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004361
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004362 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004363 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004364 txn->req.msg_state = HTTP_MSG_ERROR;
4365 if (txn->status) {
4366 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004367 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004368 } else {
4369 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004370 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004371 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004372 req->analysers &= AN_REQ_FLT_END;
4373 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 +01004374
Willy Tarreaue7dff022015-04-03 01:14:29 +02004375 if (!(s->flags & SF_ERR_MASK))
4376 s->flags |= SF_ERR_PRXCOND;
4377 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004378 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004379 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004380 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004381 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004382 }
4383 return 0;
4384
4385 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004386 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004387 txn->req.msg_state = HTTP_MSG_ERROR;
4388 if (txn->status) {
4389 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004390 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004391 } else {
4392 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004393 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004394 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004395 req->analysers &= AN_REQ_FLT_END;
4396 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 +01004397
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004398 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4399 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004400 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004401 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004402
Willy Tarreaue7dff022015-04-03 01:14:29 +02004403 if (!(s->flags & SF_ERR_MASK))
4404 s->flags |= SF_ERR_SRVCL;
4405 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004406 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004407 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004408 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004409 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004410 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004411 return 0;
4412}
4413
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004414/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4415 * processing can continue on next analysers, or zero if it either needs more
4416 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004417 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004418 * when it has nothing left to do, and may remove any analyser when it wants to
4419 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004420 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004421int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004422{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004423 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004424 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004425 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004426 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004427 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004428 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004429 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004430
Christopher Faulet45073512018-07-20 10:16:29 +02004431 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 +02004432 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004433 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004434 rep,
4435 rep->rex, rep->wex,
4436 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004437 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004438 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004439
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004440 /*
4441 * Now parse the partial (or complete) lines.
4442 * We will check the response syntax, and also join multi-line
4443 * headers. An index of all the lines will be elaborated while
4444 * parsing.
4445 *
4446 * For the parsing, we use a 28 states FSM.
4447 *
4448 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004449 * ci_head(rep) = beginning of response
4450 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4451 * ci_tail(rep) = end of input data
4452 * msg->eol = end of current header or line (LF or CRLF)
4453 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004454 */
4455
Willy Tarreau628c40c2014-04-24 19:11:26 +02004456 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004457 /* There's a protected area at the end of the buffer for rewriting
4458 * purposes. We don't want to start to parse the request if the
4459 * protected area is affected, because we may have to move processed
4460 * data later, which is much more complicated.
4461 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004462 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004463 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004464 /* some data has still not left the buffer, wake us once that's done */
4465 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4466 goto abort_response;
4467 channel_dont_close(rep);
4468 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004469 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004470 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004471 }
4472
Willy Tarreau188e2302018-06-15 11:11:53 +02004473 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004474 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004475 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004476
Willy Tarreauf37954d2018-06-15 18:31:02 +02004477 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004478 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004479 }
4480
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004481 /* 1: we might have to print this header in debug mode */
4482 if (unlikely((global.mode & MODE_DEBUG) &&
4483 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004484 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004485 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004486
Willy Tarreauf37954d2018-06-15 18:31:02 +02004487 sol = ci_head(rep);
4488 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004489 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004490
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004491 sol += hdr_idx_first_pos(&txn->hdr_idx);
4492 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004493
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004494 while (cur_idx) {
4495 eol = sol + txn->hdr_idx.v[cur_idx].len;
4496 debug_hdr("srvhdr", s, sol, eol);
4497 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4498 cur_idx = txn->hdr_idx.v[cur_idx].next;
4499 }
4500 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004501
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004502 /*
4503 * Now we quickly check if we have found a full valid response.
4504 * If not so, we check the FD and buffer states before leaving.
4505 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004506 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004507 * responses are checked first.
4508 *
4509 * Depending on whether the client is still there or not, we
4510 * may send an error response back or not. Note that normally
4511 * we should only check for HTTP status there, and check I/O
4512 * errors somewhere else.
4513 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004514
Willy Tarreau655dce92009-11-08 13:10:58 +01004515 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004516 /* Invalid response */
4517 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4518 /* we detected a parsing error. We want to archive this response
4519 * in the dedicated proxy area for later troubleshooting.
4520 */
4521 hdr_response_bad:
4522 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004523 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004524
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004525 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004526 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004527 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004528 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004529 }
Willy Tarreau64648412010-03-05 10:41:54 +01004530 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004531 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004532 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004533 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004534 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004535 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004536 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004537
Willy Tarreaue7dff022015-04-03 01:14:29 +02004538 if (!(s->flags & SF_ERR_MASK))
4539 s->flags |= SF_ERR_PRXCOND;
4540 if (!(s->flags & SF_FINST_MASK))
4541 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004542
4543 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004544 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004545
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004546 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004547 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004548 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004549 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004550 goto hdr_response_bad;
4551 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004552
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004553 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004554 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004555 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004556 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004557 else if (txn->flags & TX_NOT_FIRST)
4558 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004559
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004560 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004561 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004562 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004563 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004564 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004565
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004566 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004567 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004568 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004569
4570 /* Check to see if the server refused the early data.
4571 * If so, just send a 425
4572 */
4573 if (objt_cs(s->si[1].end)) {
4574 struct connection *conn = objt_cs(s->si[1].end)->conn;
4575
4576 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4577 txn->status = 425;
4578 }
4579
Willy Tarreau350f4872014-11-28 14:42:25 +01004580 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004581 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004582 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004583
Willy Tarreaue7dff022015-04-03 01:14:29 +02004584 if (!(s->flags & SF_ERR_MASK))
4585 s->flags |= SF_ERR_SRVCL;
4586 if (!(s->flags & SF_FINST_MASK))
4587 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004588 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004589 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004590
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004591 /* read timeout : return a 504 to the client. */
4592 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004593 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004594 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004595
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004596 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004597 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004598 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004599 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004600 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004601
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004602 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004603 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004604 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004605 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004606 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004607 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004608
Willy Tarreaue7dff022015-04-03 01:14:29 +02004609 if (!(s->flags & SF_ERR_MASK))
4610 s->flags |= SF_ERR_SRVTO;
4611 if (!(s->flags & SF_FINST_MASK))
4612 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004613 return 0;
4614 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004615
Willy Tarreauf003d372012-11-26 13:35:37 +01004616 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004617 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004618 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4619 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004620 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004621 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004622
Christopher Faulet0184ea72017-01-05 14:06:34 +01004623 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004624 channel_auto_close(rep);
4625
4626 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004627 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004628 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004629
Willy Tarreaue7dff022015-04-03 01:14:29 +02004630 if (!(s->flags & SF_ERR_MASK))
4631 s->flags |= SF_ERR_CLICL;
4632 if (!(s->flags & SF_FINST_MASK))
4633 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004634
Willy Tarreau87b09662015-04-03 00:22:06 +02004635 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004636 return 0;
4637 }
4638
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004639 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004640 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004641 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004642 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004643 else if (txn->flags & TX_NOT_FIRST)
4644 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004645
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004646 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004647 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004648 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004649 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004650 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004651
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004652 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004653 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004654 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004655 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004656 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004657 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004658
Willy Tarreaue7dff022015-04-03 01:14:29 +02004659 if (!(s->flags & SF_ERR_MASK))
4660 s->flags |= SF_ERR_SRVCL;
4661 if (!(s->flags & SF_FINST_MASK))
4662 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004663 return 0;
4664 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004665
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004666 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004667 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004668 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004669 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004670 else if (txn->flags & TX_NOT_FIRST)
4671 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004672
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004673 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004674 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004675 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004676
Willy Tarreaue7dff022015-04-03 01:14:29 +02004677 if (!(s->flags & SF_ERR_MASK))
4678 s->flags |= SF_ERR_CLICL;
4679 if (!(s->flags & SF_FINST_MASK))
4680 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004681
Willy Tarreau87b09662015-04-03 00:22:06 +02004682 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004683 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004684 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004685
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004686 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004687 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004688 return 0;
4689 }
4690
4691 /* More interesting part now : we know that we have a complete
4692 * response which at least looks like HTTP. We have an indicator
4693 * of each header's length, so we can parse them quickly.
4694 */
4695
4696 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004697 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004698
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004699 /*
4700 * 1: get the status code
4701 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004702 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004703 if (n < 1 || n > 5)
4704 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004705 /* when the client triggers a 4xx from the server, it's most often due
4706 * to a missing object or permission. These events should be tracked
4707 * because if they happen often, it may indicate a brute force or a
4708 * vulnerability scan.
4709 */
4710 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004711 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004712
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004713 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004714 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004715
Willy Tarreau91852eb2015-05-01 13:26:00 +02004716 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4717 * exactly one digit "." one digit. This check may be disabled using
4718 * option accept-invalid-http-response.
4719 */
4720 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4721 if (msg->sl.st.v_l != 8) {
4722 msg->err_pos = 0;
4723 goto hdr_response_bad;
4724 }
4725
Willy Tarreauf37954d2018-06-15 18:31:02 +02004726 if (ci_head(rep)[4] != '/' ||
4727 !isdigit((unsigned char)ci_head(rep)[5]) ||
4728 ci_head(rep)[6] != '.' ||
4729 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004730 msg->err_pos = 4;
4731 goto hdr_response_bad;
4732 }
4733 }
4734
Willy Tarreau5b154472009-12-21 20:11:07 +01004735 /* check if the response is HTTP/1.1 or above */
4736 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004737 ((ci_head(rep)[5] > '1') ||
4738 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004739 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004740
4741 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004742 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 +01004743
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004744 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004745 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004746
Willy Tarreauf37954d2018-06-15 18:31:02 +02004747 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004748
Willy Tarreau39650402010-03-15 19:44:39 +01004749 /* Adjust server's health based on status code. Note: status codes 501
4750 * and 505 are triggered on demand by client request, so we must not
4751 * count them as server failures.
4752 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004753 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004754 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004755 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004756 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004757 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004758 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004759
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004760 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004761 * We may be facing a 100-continue response, or any other informational
4762 * 1xx response which is non-final, in which case this is not the right
4763 * response, and we're waiting for the next one. Let's allow this response
4764 * to go to the client and wait for the next one. There's an exception for
4765 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004766 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004767 if (txn->status < 200 &&
4768 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004769 hdr_idx_init(&txn->hdr_idx);
4770 msg->next -= channel_forward(rep, msg->next);
4771 msg->msg_state = HTTP_MSG_RPBEFORE;
4772 txn->status = 0;
4773 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004774 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004775 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004776 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004777
Willy Tarreaua14ad722017-07-07 11:36:32 +02004778 /*
4779 * 2: check for cacheability.
4780 */
4781
4782 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004783 case 200:
4784 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004785 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004786 case 206:
4787 case 300:
4788 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004789 case 404:
4790 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004791 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004792 case 414:
4793 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004794 break;
4795 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004796 /* RFC7231#6.1:
4797 * Responses with status codes that are defined as
4798 * cacheable by default (e.g., 200, 203, 204, 206,
4799 * 300, 301, 404, 405, 410, 414, and 501 in this
4800 * specification) can be reused by a cache with
4801 * heuristic expiration unless otherwise indicated
4802 * by the method definition or explicit cache
4803 * controls [RFC7234]; all other status codes are
4804 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004805 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004806 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004807 break;
4808 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004809
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004810 /*
4811 * 3: we may need to capture headers
4812 */
4813 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004814 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreauf37954d2018-06-15 18:31:02 +02004815 capture_headers(ci_head(rep), &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004816 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004817
Willy Tarreau557f1992015-05-01 10:05:17 +02004818 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4819 * by RFC7230#3.3.3 :
4820 *
4821 * The length of a message body is determined by one of the following
4822 * (in order of precedence):
4823 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004824 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4825 * the connection will become a tunnel immediately after the empty
4826 * line that concludes the header fields. A client MUST ignore
4827 * any Content-Length or Transfer-Encoding header fields received
4828 * in such a message. Any 101 response (Switching Protocols) is
4829 * managed in the same manner.
4830 *
4831 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004832 * (Informational), 204 (No Content), or 304 (Not Modified) status
4833 * code is always terminated by the first empty line after the
4834 * header fields, regardless of the header fields present in the
4835 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004836 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004837 * 3. If a Transfer-Encoding header field is present and the chunked
4838 * transfer coding (Section 4.1) is the final encoding, the message
4839 * body length is determined by reading and decoding the chunked
4840 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004841 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004842 * If a Transfer-Encoding header field is present in a response and
4843 * the chunked transfer coding is not the final encoding, the
4844 * message body length is determined by reading the connection until
4845 * it is closed by the server. If a Transfer-Encoding header field
4846 * is present in a request and the chunked transfer coding is not
4847 * the final encoding, the message body length cannot be determined
4848 * reliably; the server MUST respond with the 400 (Bad Request)
4849 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004850 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004851 * If a message is received with both a Transfer-Encoding and a
4852 * Content-Length header field, the Transfer-Encoding overrides the
4853 * Content-Length. Such a message might indicate an attempt to
4854 * perform request smuggling (Section 9.5) or response splitting
4855 * (Section 9.4) and ought to be handled as an error. A sender MUST
4856 * remove the received Content-Length field prior to forwarding such
4857 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004858 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004859 * 4. If a message is received without Transfer-Encoding and with
4860 * either multiple Content-Length header fields having differing
4861 * field-values or a single Content-Length header field having an
4862 * invalid value, then the message framing is invalid and the
4863 * recipient MUST treat it as an unrecoverable error. If this is a
4864 * request message, the server MUST respond with a 400 (Bad Request)
4865 * status code and then close the connection. If this is a response
4866 * message received by a proxy, the proxy MUST close the connection
4867 * to the server, discard the received response, and send a 502 (Bad
4868 * Gateway) response to the client. If this is a response message
4869 * received by a user agent, the user agent MUST close the
4870 * connection to the server and discard the received response.
4871 *
4872 * 5. If a valid Content-Length header field is present without
4873 * Transfer-Encoding, its decimal value defines the expected message
4874 * body length in octets. If the sender closes the connection or
4875 * the recipient times out before the indicated number of octets are
4876 * received, the recipient MUST consider the message to be
4877 * incomplete and close the connection.
4878 *
4879 * 6. If this is a request message and none of the above are true, then
4880 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004881 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004882 * 7. Otherwise, this is a response message without a declared message
4883 * body length, so the message body length is determined by the
4884 * number of octets received prior to the server closing the
4885 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004886 */
4887
4888 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004889 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004890 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004891 * FIXME: should we parse anyway and return an error on chunked encoding ?
4892 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004893 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4894 txn->status == 101)) {
4895 /* Either we've established an explicit tunnel, or we're
4896 * switching the protocol. In both cases, we're very unlikely
4897 * to understand the next protocols. We have to switch to tunnel
4898 * mode, so that we transfer the request and responses then let
4899 * this protocol pass unmodified. When we later implement specific
4900 * parsers for such protocols, we'll want to check the Upgrade
4901 * header which contains information about that protocol for
4902 * responses with status 101 (eg: see RFC2817 about TLS).
4903 */
4904 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4905 msg->flags |= HTTP_MSGF_XFER_LEN;
4906 goto end;
4907 }
4908
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004909 if (txn->meth == HTTP_METH_HEAD ||
4910 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004911 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004912 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004913 goto skip_content_length;
4914 }
4915
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004916 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004917 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004918 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004919 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004920 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4921 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004922 /* bad transfer-encoding (chunked followed by something else) */
4923 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004924 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004925 break;
4926 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004927 }
4928
Willy Tarreau1c913912015-04-30 10:57:51 +02004929 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004930 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02004931 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004932 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02004933 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4934 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02004935 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004936 signed long long cl;
4937
Willy Tarreauad14f752011-09-02 20:33:27 +02004938 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004939 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004940 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004941 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004942
Willy Tarreauad14f752011-09-02 20:33:27 +02004943 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004944 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004945 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004946 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004947
Willy Tarreauad14f752011-09-02 20:33:27 +02004948 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004949 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004950 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004951 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004952
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004953 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004954 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004955 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004956 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004957
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004958 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004959 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004960 }
4961
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004962 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01004963 /* Now we have to check if we need to modify the Connection header.
4964 * This is more difficult on the response than it is on the request,
4965 * because we can have two different HTTP versions and we don't know
4966 * how the client will interprete a response. For instance, let's say
4967 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4968 * HTTP/1.1 response without any header. Maybe it will bound itself to
4969 * HTTP/1.0 because it only knows about it, and will consider the lack
4970 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4971 * the lack of header as a keep-alive. Thus we will use two flags
4972 * indicating how a request MAY be understood by the client. In case
4973 * of multiple possibilities, we'll fix the header to be explicit. If
4974 * ambiguous cases such as both close and keepalive are seen, then we
4975 * will fall back to explicit close. Note that we won't take risks with
4976 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004977 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004978 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004979 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4980 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4981 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4982 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004983 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004984
Willy Tarreau70dffda2014-01-30 03:07:23 +01004985 /* this situation happens when combining pretend-keepalive with httpclose. */
4986 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004987 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02004988 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01004989 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
4990
Willy Tarreau60466522010-01-18 19:08:45 +01004991 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004992 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01004993 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4994 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004995
Willy Tarreau60466522010-01-18 19:08:45 +01004996 /* now adjust header transformations depending on current state */
4997 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4998 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4999 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005000 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005001 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005002 }
Willy Tarreau60466522010-01-18 19:08:45 +01005003 else { /* SCL / KAL */
5004 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005005 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005006 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005007 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005008
Willy Tarreau60466522010-01-18 19:08:45 +01005009 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005010 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005011
Willy Tarreau60466522010-01-18 19:08:45 +01005012 /* Some keep-alive responses are converted to Server-close if
5013 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005014 */
Willy Tarreau60466522010-01-18 19:08:45 +01005015 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5016 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005017 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005018 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005019 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005020 }
5021
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005022 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005023 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005024 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005025
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005026 /* end of job, return OK */
5027 rep->analysers &= ~an_bit;
5028 rep->analyse_exp = TICK_ETERNITY;
5029 channel_auto_close(rep);
5030 return 1;
5031
5032 abort_keep_alive:
5033 /* A keep-alive request to the server failed on a network error.
5034 * The client is required to retry. We need to close without returning
5035 * any other information so that the client retries.
5036 */
5037 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005038 rep->analysers &= AN_RES_FLT_END;
5039 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005040 channel_auto_close(rep);
5041 s->logs.logwait = 0;
5042 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005043 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005044 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005045 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005046 return 0;
5047}
5048
5049/* This function performs all the processing enabled for the current response.
5050 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005051 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005052 * other functions. It works like process_request (see indications above).
5053 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005054int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005055{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005056 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005057 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005058 struct http_msg *msg = &txn->rsp;
5059 struct proxy *cur_proxy;
5060 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005061 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005062
Christopher Faulet45073512018-07-20 10:16:29 +02005063 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 +02005064 now_ms, __FUNCTION__,
5065 s,
5066 rep,
5067 rep->rex, rep->wex,
5068 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005069 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005070 rep->analysers);
5071
5072 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5073 return 0;
5074
Willy Tarreau70730dd2014-04-24 18:06:27 +02005075 /* The stats applet needs to adjust the Connection header but we don't
5076 * apply any filter there.
5077 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005078 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5079 rep->analysers &= ~an_bit;
5080 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005081 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005082 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005083
Willy Tarreau58975672014-04-24 21:13:57 +02005084 /*
5085 * We will have to evaluate the filters.
5086 * As opposed to version 1.2, now they will be evaluated in the
5087 * filters order and not in the header order. This means that
5088 * each filter has to be validated among all headers.
5089 *
5090 * Filters are tried with ->be first, then with ->fe if it is
5091 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005092 *
5093 * Maybe we are in resume condiion. In this case I choose the
5094 * "struct proxy" which contains the rule list matching the resume
5095 * pointer. If none of theses "struct proxy" match, I initialise
5096 * the process with the first one.
5097 *
5098 * In fact, I check only correspondance betwwen the current list
5099 * pointer and the ->fe rule list. If it doesn't match, I initialize
5100 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005101 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005102 if (s->current_rule_list == &sess->fe->http_res_rules)
5103 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005104 else
5105 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005106 while (1) {
5107 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005108
Willy Tarreau58975672014-04-24 21:13:57 +02005109 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005110 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005111 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005112
Willy Tarreau51d861a2015-05-22 17:30:48 +02005113 if (ret == HTTP_RULE_RES_BADREQ)
5114 goto return_srv_prx_502;
5115
5116 if (ret == HTTP_RULE_RES_DONE) {
5117 rep->analysers &= ~an_bit;
5118 rep->analyse_exp = TICK_ETERNITY;
5119 return 1;
5120 }
5121 }
5122
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005123 /* we need to be called again. */
5124 if (ret == HTTP_RULE_RES_YIELD) {
5125 channel_dont_close(rep);
5126 return 0;
5127 }
5128
Willy Tarreau58975672014-04-24 21:13:57 +02005129 /* try headers filters */
5130 if (rule_set->rsp_exp != NULL) {
5131 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5132 return_bad_resp:
5133 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005134 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005135 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005136 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005137 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005138 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005139 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005140 txn->status = 502;
5141 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005142 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005143 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005144 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005145 if (!(s->flags & SF_ERR_MASK))
5146 s->flags |= SF_ERR_PRXCOND;
5147 if (!(s->flags & SF_FINST_MASK))
5148 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005149 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005150 }
Willy Tarreau58975672014-04-24 21:13:57 +02005151 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005152
Willy Tarreau58975672014-04-24 21:13:57 +02005153 /* has the response been denied ? */
5154 if (txn->flags & TX_SVDENY) {
5155 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005156 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005157
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005158 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5159 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005160 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005161 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005162
Willy Tarreau58975672014-04-24 21:13:57 +02005163 goto return_srv_prx_502;
5164 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005165
Willy Tarreau58975672014-04-24 21:13:57 +02005166 /* add response headers from the rule sets in the same order */
5167 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005168 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005169 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005170 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005171 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005172 ret = acl_pass(ret);
5173 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5174 ret = !ret;
5175 if (!ret)
5176 continue;
5177 }
5178 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5179 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005180 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005181
Willy Tarreau58975672014-04-24 21:13:57 +02005182 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005183 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005184 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005185 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005186 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005187
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005188 /* After this point, this anayzer can't return yield, so we can
5189 * remove the bit corresponding to this analyzer from the list.
5190 *
5191 * Note that the intermediate returns and goto found previously
5192 * reset the analyzers.
5193 */
5194 rep->analysers &= ~an_bit;
5195 rep->analyse_exp = TICK_ETERNITY;
5196
Willy Tarreau58975672014-04-24 21:13:57 +02005197 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005198 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005199 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005200
Willy Tarreau58975672014-04-24 21:13:57 +02005201 /*
5202 * Now check for a server cookie.
5203 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005204 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005205 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005206
Willy Tarreau58975672014-04-24 21:13:57 +02005207 /*
5208 * Check for cache-control or pragma headers if required.
5209 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005210 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005211 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005212
Willy Tarreau58975672014-04-24 21:13:57 +02005213 /*
5214 * Add server cookie in the response if needed
5215 */
5216 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5217 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005218 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005219 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5220 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5221 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5222 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5223 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005224 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005225 /* the server is known, it's not the one the client requested, or the
5226 * cookie's last seen date needs to be refreshed. We have to
5227 * insert a set-cookie here, except if we want to insert only on POST
5228 * requests and this one isn't. Note that servers which don't have cookies
5229 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005230 */
Willy Tarreau58975672014-04-24 21:13:57 +02005231 if (!objt_server(s->target)->cookie) {
5232 chunk_printf(&trash,
5233 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5234 s->be->cookie_name);
5235 }
5236 else {
5237 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005238
Willy Tarreau58975672014-04-24 21:13:57 +02005239 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5240 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005241 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5242 s30tob64((date.tv_sec+3) >> 2,
5243 trash.area + trash.data);
5244 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005245
Willy Tarreau58975672014-04-24 21:13:57 +02005246 if (s->be->cookie_maxlife) {
5247 /* emit first_date, which is either the original one or
5248 * the current date.
5249 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005250 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005251 s30tob64(txn->cookie_first_date ?
5252 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005253 (date.tv_sec+3) >> 2,
5254 trash.area + trash.data);
5255 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005256 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005257 }
Willy Tarreau58975672014-04-24 21:13:57 +02005258 chunk_appendf(&trash, "; path=/");
5259 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005260
Willy Tarreau58975672014-04-24 21:13:57 +02005261 if (s->be->cookie_domain)
5262 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005263
Willy Tarreau58975672014-04-24 21:13:57 +02005264 if (s->be->ck_opts & PR_CK_HTTPONLY)
5265 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005266
Willy Tarreau58975672014-04-24 21:13:57 +02005267 if (s->be->ck_opts & PR_CK_SECURE)
5268 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005269
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005270 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005271 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005272
Willy Tarreau58975672014-04-24 21:13:57 +02005273 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005274 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005275 /* the server did not change, only the date was updated */
5276 txn->flags |= TX_SCK_UPDATED;
5277 else
5278 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005279
Willy Tarreau58975672014-04-24 21:13:57 +02005280 /* Here, we will tell an eventual cache on the client side that we don't
5281 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5282 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5283 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005284 */
Willy Tarreau58975672014-04-24 21:13:57 +02005285 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005286
Willy Tarreau58975672014-04-24 21:13:57 +02005287 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005288
Willy Tarreau58975672014-04-24 21:13:57 +02005289 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5290 "Cache-control: private", 22) < 0))
5291 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005292 }
Willy Tarreau58975672014-04-24 21:13:57 +02005293 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005294
Willy Tarreau58975672014-04-24 21:13:57 +02005295 /*
5296 * Check if result will be cacheable with a cookie.
5297 * We'll block the response if security checks have caught
5298 * nasty things such as a cacheable cookie.
5299 */
5300 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5301 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5302 (s->be->options & PR_O_CHK_CACHE)) {
5303 /* we're in presence of a cacheable response containing
5304 * a set-cookie header. We'll block it as requested by
5305 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005306 */
Willy Tarreau58975672014-04-24 21:13:57 +02005307 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005308 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005309
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005310 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5311 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005312 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005313 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005314
Christopher Faulet767a84b2017-11-24 16:50:31 +01005315 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5316 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005317 send_log(s->be, LOG_ALERT,
5318 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5319 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5320 goto return_srv_prx_502;
5321 }
Willy Tarreau03945942009-12-22 16:50:27 +01005322
Willy Tarreau70730dd2014-04-24 18:06:27 +02005323 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005324 /*
5325 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5326 * If an "Upgrade" token is found, the header is left untouched in order
5327 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005328 * if anything but "Upgrade" is present in the Connection header. We don't
5329 * want to touch any 101 response either since it's switching to another
5330 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005331 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005332 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005333 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005334 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005335 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5336 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005337
Willy Tarreau58975672014-04-24 21:13:57 +02005338 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5339 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5340 /* we want a keep-alive response here. Keep-alive header
5341 * required if either side is not 1.1.
5342 */
5343 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5344 want_flags |= TX_CON_KAL_SET;
5345 }
5346 else {
5347 /* we want a close response here. Close header required if
5348 * the server is 1.1, regardless of the client.
5349 */
5350 if (msg->flags & HTTP_MSGF_VER_11)
5351 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005352 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005353
Willy Tarreau58975672014-04-24 21:13:57 +02005354 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5355 http_change_connection_header(txn, msg, want_flags);
5356 }
5357
5358 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005359 /* Always enter in the body analyzer */
5360 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5361 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005362
Willy Tarreau58975672014-04-24 21:13:57 +02005363 /* if the user wants to log as soon as possible, without counting
5364 * bytes from the server, then this is the right moment. We have
5365 * to temporarily assign bytes_out to log what we currently have.
5366 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005367 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005368 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5369 s->logs.bytes_out = txn->rsp.eoh;
5370 s->do_log(s);
5371 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005372 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005373 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005374}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005375
Willy Tarreaud98cf932009-12-27 22:54:55 +01005376/* This function is an analyser which forwards response body (including chunk
5377 * sizes if any). It is called as soon as we must forward, even if we forward
5378 * zero byte. The only situation where it must not be called is when we're in
5379 * tunnel mode and we want to forward till the close. It's used both to forward
5380 * remaining data and to resync after end of body. It expects the msg_state to
5381 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005382 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005383 *
5384 * It is capable of compressing response data both in content-length mode and
5385 * in chunked mode. The state machines follows different flows depending on
5386 * whether content-length and chunked modes are used, since there are no
5387 * trailers in content-length :
5388 *
5389 * chk-mode cl-mode
5390 * ,----- BODY -----.
5391 * / \
5392 * V size > 0 V chk-mode
5393 * .--> SIZE -------------> DATA -------------> CRLF
5394 * | | size == 0 | last byte |
5395 * | v final crlf v inspected |
5396 * | TRAILERS -----------> DONE |
5397 * | |
5398 * `----------------------------------------------'
5399 *
5400 * Compression only happens in the DATA state, and must be flushed in final
5401 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5402 * is performed at once on final states for all bytes parsed, or when leaving
5403 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005404 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005405int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005406{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005407 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005408 struct http_txn *txn = s->txn;
5409 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005410 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005411
Christopher Faulet45073512018-07-20 10:16:29 +02005412 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 +02005413 now_ms, __FUNCTION__,
5414 s,
5415 res,
5416 res->rex, res->wex,
5417 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005418 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005419 res->analysers);
5420
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005421 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5422 return 0;
5423
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005424 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005425 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005426 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005427 /* Output closed while we were sending data. We must abort and
5428 * wake the other side up.
5429 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005430 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005431 msg->msg_state = HTTP_MSG_ERROR;
5432 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005433 return 1;
5434 }
5435
Willy Tarreau4fe41902010-06-07 22:27:41 +02005436 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005437 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005438
Christopher Fauletd7c91962015-04-30 11:48:27 +02005439 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005440 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5441 ? HTTP_MSG_CHUNK_SIZE
5442 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005443 }
5444
Willy Tarreauefdf0942014-04-24 20:08:57 +02005445 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005446 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005447 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005448 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005449 }
5450
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005451 if (msg->msg_state < HTTP_MSG_DONE) {
5452 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5453 ? http_msg_forward_chunked_body(s, msg)
5454 : http_msg_forward_body(s, msg));
5455 if (!ret)
5456 goto missing_data_or_waiting;
5457 if (ret < 0)
5458 goto return_bad_res;
5459 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005460
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005461 /* other states, DONE...TUNNEL */
5462 /* for keep-alive we don't want to forward closes on DONE */
5463 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5464 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5465 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005466
Christopher Faulet894da4c2017-07-18 11:29:07 +02005467 http_resync_states(s);
5468 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005469 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5470 if (res->flags & CF_SHUTW) {
5471 /* response errors are most likely due to the
5472 * client aborting the transfer. */
5473 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005474 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005475 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005476 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005477 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005478 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005479 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005480 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005481 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005482
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005483 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005484 if (res->flags & CF_SHUTW)
5485 goto aborted_xfer;
5486
5487 /* stop waiting for data if the input is closed before the end. If the
5488 * client side was already closed, it means that the client has aborted,
5489 * so we don't want to count this as a server abort. Otherwise it's a
5490 * server abort.
5491 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005492 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005493 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005494 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005495 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005496 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005497 if (!(s->flags & SF_ERR_MASK))
5498 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005499 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005500 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005501 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005502 goto return_bad_res_stats_ok;
5503 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005504 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005505
Willy Tarreau40dba092010-03-04 18:14:51 +01005506 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005507 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005508 goto return_bad_res;
5509
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005510 /* When TE: chunked is used, we need to get there again to parse
5511 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005512 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5513 * or if there are filters registered on the stream, we don't want to
5514 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005515 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005516 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005517 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005518 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5519 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005520 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005521
Willy Tarreau5c620922011-05-11 19:56:11 +02005522 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005523 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005524 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005525 * modes are already handled by the stream sock layer. We must not do
5526 * this in content-length mode because it could present the MSG_MORE
5527 * flag with the last block of forwarded data, which would cause an
5528 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005529 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005530 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005531 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005532
Willy Tarreau87b09662015-04-03 00:22:06 +02005533 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005534 return 0;
5535
Willy Tarreau40dba092010-03-04 18:14:51 +01005536 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005537 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005538 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005539 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005540
5541 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005542 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005543 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005544 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005545 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005546 res->analysers &= AN_RES_FLT_END;
5547 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 +01005548 if (objt_server(s->target))
5549 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005550
Willy Tarreaue7dff022015-04-03 01:14:29 +02005551 if (!(s->flags & SF_ERR_MASK))
5552 s->flags |= SF_ERR_PRXCOND;
5553 if (!(s->flags & SF_FINST_MASK))
5554 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005555 return 0;
5556
5557 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005558 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005559 txn->rsp.msg_state = HTTP_MSG_ERROR;
5560 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005561 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005562 res->analysers &= AN_RES_FLT_END;
5563 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 +01005564
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005565 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5566 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005567 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005568 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005569
Willy Tarreaue7dff022015-04-03 01:14:29 +02005570 if (!(s->flags & SF_ERR_MASK))
5571 s->flags |= SF_ERR_CLICL;
5572 if (!(s->flags & SF_FINST_MASK))
5573 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005574 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005575}
5576
5577
5578static inline int
5579http_msg_forward_body(struct stream *s, struct http_msg *msg)
5580{
5581 struct channel *chn = msg->chn;
5582 int ret;
5583
5584 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5585
5586 if (msg->msg_state == HTTP_MSG_ENDING)
5587 goto ending;
5588
5589 /* Neither content-length, nor transfer-encoding was found, so we must
5590 * read the body until the server connection is closed. In that case, we
5591 * eat data as they come. Of course, this happens for response only. */
5592 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005593 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005594 msg->chunk_len += len;
5595 msg->body_len += len;
5596 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005597 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005598 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005599 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005600 msg->next += ret;
5601 msg->chunk_len -= ret;
5602 if (msg->chunk_len) {
5603 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005604 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005605 chn->flags |= CF_WAKE_WRITE;
5606 goto missing_data_or_waiting;
5607 }
5608
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005609 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5610 * always set for a request. */
5611 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5612 /* The server still sending data that should be filtered */
5613 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5614 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005615 msg->msg_state = HTTP_MSG_TUNNEL;
5616 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005617 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005618
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005619 msg->msg_state = HTTP_MSG_ENDING;
5620
5621 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005622 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005623 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005624 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5625 /* default_ret */ msg->next,
5626 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005627 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005628 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005629 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5630 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005631 if (msg->next)
5632 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005633
Christopher Fauletda02e172015-12-04 09:25:05 +01005634 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5635 /* default_ret */ 1,
5636 /* on_error */ goto error,
5637 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005638 if (msg->msg_state == HTTP_MSG_ENDING)
5639 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005640 return 1;
5641
5642 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005643 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005644 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5645 /* default_ret */ msg->next,
5646 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005647 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005648 msg->next -= ret;
5649 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5650 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005651 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005652 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005653 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005654 return 0;
5655 error:
5656 return -1;
5657}
5658
5659static inline int
5660http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
5661{
5662 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005663 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005664 int ret;
5665
5666 /* Here we have the guarantee to be in one of the following state:
5667 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5668 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5669
Christopher Fauletca874b82018-09-20 11:31:01 +02005670 if (msg->msg_state == HTTP_MSG_ENDING)
5671 goto ending;
5672
5673 /* Don't parse chunks if there is no input data */
5674 if (!ci_data(chn))
5675 goto waiting;
5676
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005677 switch_states:
5678 switch (msg->msg_state) {
5679 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005680 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005681 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005682 /* on_error */ goto error);
5683 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005684 msg->chunk_len -= ret;
5685 if (msg->chunk_len) {
5686 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005687 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005688 chn->flags |= CF_WAKE_WRITE;
5689 goto missing_data_or_waiting;
5690 }
5691
5692 /* nothing left to forward for this chunk*/
5693 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5694 /* fall through for HTTP_MSG_CHUNK_CRLF */
5695
5696 case HTTP_MSG_CHUNK_CRLF:
5697 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005698 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005699 if (ret == 0)
5700 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005701 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005702 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005703 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005704 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005705 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005706 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005707 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005708 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5709 /* fall through for HTTP_MSG_CHUNK_SIZE */
5710
5711 case HTTP_MSG_CHUNK_SIZE:
5712 /* read the chunk size and assign it to ->chunk_len,
5713 * then set ->next to point to the body and switch to
5714 * DATA or TRAILERS state.
5715 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005716 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005717 if (ret == 0)
5718 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005719 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005720 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005721 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005722 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005723 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005724 }
5725
5726 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005727 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005728 msg->chunk_len = chunk;
5729 msg->body_len += chunk;
5730
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005731 if (msg->chunk_len) {
5732 msg->msg_state = HTTP_MSG_DATA;
5733 goto switch_states;
5734 }
5735 msg->msg_state = HTTP_MSG_TRAILERS;
5736 /* fall through for HTTP_MSG_TRAILERS */
5737
5738 case HTTP_MSG_TRAILERS:
5739 ret = http_forward_trailers(msg);
5740 if (ret < 0)
5741 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005742 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5743 /* default_ret */ 1,
5744 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005745 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005746 if (!ret)
5747 goto missing_data_or_waiting;
5748 break;
5749
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005750 default:
5751 /* This should no happen in this function */
5752 goto error;
5753 }
5754
5755 msg->msg_state = HTTP_MSG_ENDING;
5756 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005757 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005758 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005759 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005760 /* default_ret */ msg->next,
5761 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005762 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005763 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005764 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5765 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005766 if (msg->next)
5767 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005768
Christopher Fauletda02e172015-12-04 09:25:05 +01005769 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005770 /* default_ret */ 1,
5771 /* on_error */ goto error,
5772 /* on_wait */ goto waiting);
5773 msg->msg_state = HTTP_MSG_DONE;
5774 return 1;
5775
5776 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005777 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005778 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005779 /* default_ret */ msg->next,
5780 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005781 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005782 msg->next -= ret;
5783 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5784 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005785 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005786 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005787 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005788 return 0;
5789
5790 chunk_parsing_error:
5791 if (msg->err_pos >= 0) {
5792 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005793 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005794 msg->msg_state, strm_fe(s));
5795 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005796 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005797 msg, msg->msg_state, s->be);
5798 }
5799 error:
5800 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005801}
5802
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005803
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005804/* Iterate the same filter through all request headers.
5805 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005806 * Since it can manage the switch to another backend, it updates the per-proxy
5807 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005808 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005809int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005810{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005811 char *cur_ptr, *cur_end, *cur_next;
5812 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005813 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005814 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005815 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005816
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005817 last_hdr = 0;
5818
Willy Tarreauf37954d2018-06-15 18:31:02 +02005819 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005820 old_idx = 0;
5821
5822 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005823 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005824 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005825 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005826 (exp->action == ACT_ALLOW ||
5827 exp->action == ACT_DENY ||
5828 exp->action == ACT_TARPIT))
5829 return 0;
5830
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005831 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005832 if (!cur_idx)
5833 break;
5834
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005835 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005836 cur_ptr = cur_next;
5837 cur_end = cur_ptr + cur_hdr->len;
5838 cur_next = cur_end + cur_hdr->cr + 1;
5839
5840 /* Now we have one header between cur_ptr and cur_end,
5841 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005842 */
5843
Willy Tarreau15a53a42015-01-21 13:39:42 +01005844 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005845 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005846 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005847 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005848 last_hdr = 1;
5849 break;
5850
5851 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005852 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005853 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005854 break;
5855
5856 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005857 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005858 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005859 break;
5860
5861 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005862 len = exp_replace(trash.area,
5863 trash.size, cur_ptr,
5864 exp->replace, pmatch);
5865 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005866 return -1;
5867
Willy Tarreau6e27be12018-08-22 04:46:47 +02005868 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5869
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005870 /* FIXME: if the user adds a newline in the replacement, the
5871 * index will not be recalculated for now, and the new line
5872 * will not be counted as a new header.
5873 */
5874
5875 cur_end += delta;
5876 cur_next += delta;
5877 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005878 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005879 break;
5880
5881 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005882 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005883 cur_next += delta;
5884
Willy Tarreaufa355d42009-11-29 18:12:29 +01005885 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005886 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5887 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005888 cur_hdr->len = 0;
5889 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005890 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005891 break;
5892
5893 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005894 }
5895
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005896 /* keep the link from this header to next one in case of later
5897 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005898 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005899 old_idx = cur_idx;
5900 }
5901 return 0;
5902}
5903
5904
5905/* Apply the filter to the request line.
5906 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5907 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005908 * Since it can manage the switch to another backend, it updates the per-proxy
5909 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005910 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005911int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005912{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005913 char *cur_ptr, *cur_end;
5914 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005915 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005916 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005917
Willy Tarreau3d300592007-03-18 18:34:41 +01005918 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005919 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005920 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005921 (exp->action == ACT_ALLOW ||
5922 exp->action == ACT_DENY ||
5923 exp->action == ACT_TARPIT))
5924 return 0;
5925 else if (exp->action == ACT_REMOVE)
5926 return 0;
5927
5928 done = 0;
5929
Willy Tarreauf37954d2018-06-15 18:31:02 +02005930 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005931 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005932
5933 /* Now we have the request line between cur_ptr and cur_end */
5934
Willy Tarreau15a53a42015-01-21 13:39:42 +01005935 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005936 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005937 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005938 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005939 done = 1;
5940 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005941
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005942 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005943 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005944 done = 1;
5945 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005946
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005947 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005948 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005949 done = 1;
5950 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005951
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005952 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005953 len = exp_replace(trash.area, trash.size,
5954 cur_ptr, exp->replace, pmatch);
5955 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005956 return -1;
5957
Willy Tarreau6e27be12018-08-22 04:46:47 +02005958 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5959
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005960 /* FIXME: if the user adds a newline in the replacement, the
5961 * index will not be recalculated for now, and the new line
5962 * will not be counted as a new header.
5963 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005964
Willy Tarreaufa355d42009-11-29 18:12:29 +01005965 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005966 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005967 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005968 HTTP_MSG_RQMETH,
5969 cur_ptr, cur_end + 1,
5970 NULL, NULL);
5971 if (unlikely(!cur_end))
5972 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005973
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005974 /* we have a full request and we know that we have either a CR
5975 * or an LF at <ptr>.
5976 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005977 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5978 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005979 /* there is no point trying this regex on headers */
5980 return 1;
5981 }
5982 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005983 return done;
5984}
Willy Tarreau97de6242006-12-27 17:18:38 +01005985
Willy Tarreau58f10d72006-12-04 02:26:12 +01005986
Willy Tarreau58f10d72006-12-04 02:26:12 +01005987
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005988/*
Willy Tarreau87b09662015-04-03 00:22:06 +02005989 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005990 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005991 * unparsable request. Since it can manage the switch to another backend, it
5992 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005993 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005994int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005995{
Willy Tarreau192252e2015-04-04 01:47:55 +02005996 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005997 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005998 struct hdr_exp *exp;
5999
6000 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006001 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006002
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006003 /*
6004 * The interleaving of transformations and verdicts
6005 * makes it difficult to decide to continue or stop
6006 * the evaluation.
6007 */
6008
Willy Tarreau6c123b12010-01-28 20:22:06 +01006009 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6010 break;
6011
Willy Tarreau3d300592007-03-18 18:34:41 +01006012 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006013 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006014 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006015 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006016
6017 /* if this filter had a condition, evaluate it now and skip to
6018 * next filter if the condition does not match.
6019 */
6020 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006021 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006022 ret = acl_pass(ret);
6023 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6024 ret = !ret;
6025
6026 if (!ret)
6027 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006028 }
6029
6030 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006031 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006032 if (unlikely(ret < 0))
6033 return -1;
6034
6035 if (likely(ret == 0)) {
6036 /* The filter did not match the request, it can be
6037 * iterated through all headers.
6038 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006039 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6040 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006041 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006042 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006043 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006044}
6045
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006046
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006047/* Delete a value in a header between delimiters <from> and <next> in buffer
6048 * <buf>. The number of characters displaced is returned, and the pointer to
6049 * the first delimiter is updated if required. The function tries as much as
6050 * possible to respect the following principles :
6051 * - replace <from> delimiter by the <next> one unless <from> points to a
6052 * colon, in which case <next> is simply removed
6053 * - set exactly one space character after the new first delimiter, unless
6054 * there are not enough characters in the block being moved to do so.
6055 * - remove unneeded spaces before the previous delimiter and after the new
6056 * one.
6057 *
6058 * It is the caller's responsibility to ensure that :
6059 * - <from> points to a valid delimiter or the colon ;
6060 * - <next> points to a valid delimiter or the final CR/LF ;
6061 * - there are non-space chars before <from> ;
6062 * - there is a CR/LF at or after <next>.
6063 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006064int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006065{
6066 char *prev = *from;
6067
6068 if (*prev == ':') {
6069 /* We're removing the first value, preserve the colon and add a
6070 * space if possible.
6071 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006072 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006073 next++;
6074 prev++;
6075 if (prev < next)
6076 *prev++ = ' ';
6077
Willy Tarreau2235b262016-11-05 15:50:20 +01006078 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006079 next++;
6080 } else {
6081 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006082 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006083 prev--;
6084 *from = prev;
6085
6086 /* copy the delimiter and if possible a space if we're
6087 * not at the end of the line.
6088 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006089 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006090 *prev++ = *next++;
6091 if (prev + 1 < next)
6092 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006093 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006094 next++;
6095 }
6096 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006097 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006098}
6099
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006100/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006101 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006102 * desirable to call it only when needed. This code is quite complex because
6103 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6104 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006105 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006106void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006107{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006108 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006109 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006110 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006111 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006112 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6113 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006114
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006115 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006116 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006117 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006118
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006119 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006120 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006121 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006122
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006123 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006124 hdr_beg = hdr_next;
6125 hdr_end = hdr_beg + cur_hdr->len;
6126 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006127
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006128 /* We have one full header between hdr_beg and hdr_end, and the
6129 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006130 * "Cookie:" headers.
6131 */
6132
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006133 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006134 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006135 old_idx = cur_idx;
6136 continue;
6137 }
6138
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006139 del_from = NULL; /* nothing to be deleted */
6140 preserve_hdr = 0; /* assume we may kill the whole header */
6141
Willy Tarreau58f10d72006-12-04 02:26:12 +01006142 /* Now look for cookies. Conforming to RFC2109, we have to support
6143 * attributes whose name begin with a '$', and associate them with
6144 * the right cookie, if we want to delete this cookie.
6145 * So there are 3 cases for each cookie read :
6146 * 1) it's a special attribute, beginning with a '$' : ignore it.
6147 * 2) it's a server id cookie that we *MAY* want to delete : save
6148 * some pointers on it (last semi-colon, beginning of cookie...)
6149 * 3) it's an application cookie : we *MAY* have to delete a previous
6150 * "special" cookie.
6151 * At the end of loop, if a "special" cookie remains, we may have to
6152 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006153 * *MUST* delete it.
6154 *
6155 * Note: RFC2965 is unclear about the processing of spaces around
6156 * the equal sign in the ATTR=VALUE form. A careful inspection of
6157 * the RFC explicitly allows spaces before it, and not within the
6158 * tokens (attrs or values). An inspection of RFC2109 allows that
6159 * too but section 10.1.3 lets one think that spaces may be allowed
6160 * after the equal sign too, resulting in some (rare) buggy
6161 * implementations trying to do that. So let's do what servers do.
6162 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6163 * allowed quoted strings in values, with any possible character
6164 * after a backslash, including control chars and delimitors, which
6165 * causes parsing to become ambiguous. Browsers also allow spaces
6166 * within values even without quotes.
6167 *
6168 * We have to keep multiple pointers in order to support cookie
6169 * removal at the beginning, middle or end of header without
6170 * corrupting the header. All of these headers are valid :
6171 *
6172 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6173 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6174 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6175 * | | | | | | | | |
6176 * | | | | | | | | hdr_end <--+
6177 * | | | | | | | +--> next
6178 * | | | | | | +----> val_end
6179 * | | | | | +-----------> val_beg
6180 * | | | | +--------------> equal
6181 * | | | +----------------> att_end
6182 * | | +---------------------> att_beg
6183 * | +--------------------------> prev
6184 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006185 */
6186
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006187 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6188 /* Iterate through all cookies on this line */
6189
6190 /* find att_beg */
6191 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006192 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006193 att_beg++;
6194
6195 /* find att_end : this is the first character after the last non
6196 * space before the equal. It may be equal to hdr_end.
6197 */
6198 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006199
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006200 while (equal < hdr_end) {
6201 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006202 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006203 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006204 continue;
6205 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006206 }
6207
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006208 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6209 * is between <att_beg> and <equal>, both may be identical.
6210 */
6211
6212 /* look for end of cookie if there is an equal sign */
6213 if (equal < hdr_end && *equal == '=') {
6214 /* look for the beginning of the value */
6215 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006216 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006217 val_beg++;
6218
6219 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006220 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006221
6222 /* make val_end point to the first white space or delimitor after the value */
6223 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006224 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006225 val_end--;
6226 } else {
6227 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006228 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006229
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006230 /* We have nothing to do with attributes beginning with '$'. However,
6231 * they will automatically be removed if a header before them is removed,
6232 * since they're supposed to be linked together.
6233 */
6234 if (*att_beg == '$')
6235 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006236
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006237 /* Ignore cookies with no equal sign */
6238 if (equal == next) {
6239 /* This is not our cookie, so we must preserve it. But if we already
6240 * scheduled another cookie for removal, we cannot remove the
6241 * complete header, but we can remove the previous block itself.
6242 */
6243 preserve_hdr = 1;
6244 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006245 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006246 val_end += delta;
6247 next += delta;
6248 hdr_end += delta;
6249 hdr_next += delta;
6250 cur_hdr->len += delta;
6251 http_msg_move_end(&txn->req, delta);
6252 prev = del_from;
6253 del_from = NULL;
6254 }
6255 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006256 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006257
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006258 /* if there are spaces around the equal sign, we need to
6259 * strip them otherwise we'll get trouble for cookie captures,
6260 * or even for rewrites. Since this happens extremely rarely,
6261 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006262 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006263 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6264 int stripped_before = 0;
6265 int stripped_after = 0;
6266
6267 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006268 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006269 equal += stripped_before;
6270 val_beg += stripped_before;
6271 }
6272
6273 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006274 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006275 val_beg += stripped_after;
6276 stripped_before += stripped_after;
6277 }
6278
6279 val_end += stripped_before;
6280 next += stripped_before;
6281 hdr_end += stripped_before;
6282 hdr_next += stripped_before;
6283 cur_hdr->len += stripped_before;
6284 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006285 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006286 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006287
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006288 /* First, let's see if we want to capture this cookie. We check
6289 * that we don't already have a client side cookie, because we
6290 * can only capture one. Also as an optimisation, we ignore
6291 * cookies shorter than the declared name.
6292 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006293 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6294 (val_end - att_beg >= sess->fe->capture_namelen) &&
6295 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006296 int log_len = val_end - att_beg;
6297
Willy Tarreaubafbe012017-11-24 17:34:44 +01006298 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006299 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006300 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006301 if (log_len > sess->fe->capture_len)
6302 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006303 memcpy(txn->cli_cookie, att_beg, log_len);
6304 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006305 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006306 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006307
Willy Tarreaubca99692010-10-06 19:25:55 +02006308 /* Persistence cookies in passive, rewrite or insert mode have the
6309 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006310 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006311 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006312 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006313 * For cookies in prefix mode, the form is :
6314 *
6315 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006316 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006317 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6318 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6319 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006320 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006321
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006322 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6323 * have the server ID between val_beg and delim, and the original cookie between
6324 * delim+1 and val_end. Otherwise, delim==val_end :
6325 *
6326 * Cookie: NAME=SRV; # in all but prefix modes
6327 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6328 * | || || | |+-> next
6329 * | || || | +--> val_end
6330 * | || || +---------> delim
6331 * | || |+------------> val_beg
6332 * | || +-------------> att_end = equal
6333 * | |+-----------------> att_beg
6334 * | +------------------> prev
6335 * +-------------------------> hdr_beg
6336 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006337
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006338 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006339 for (delim = val_beg; delim < val_end; delim++)
6340 if (*delim == COOKIE_DELIM)
6341 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006342 } else {
6343 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006344 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006345 /* Now check if the cookie contains a date field, which would
6346 * appear after a vertical bar ('|') just after the server name
6347 * and before the delimiter.
6348 */
6349 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6350 if (vbar1) {
6351 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006352 * right is the last seen date. It is a base64 encoded
6353 * 30-bit value representing the UNIX date since the
6354 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006355 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006356 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006357 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006358 if (val_end - vbar1 >= 5) {
6359 val = b64tos30(vbar1);
6360 if (val > 0)
6361 txn->cookie_last_date = val << 2;
6362 }
6363 /* look for a second vertical bar */
6364 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6365 if (vbar1 && (val_end - vbar1 > 5)) {
6366 val = b64tos30(vbar1 + 1);
6367 if (val > 0)
6368 txn->cookie_first_date = val << 2;
6369 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006370 }
6371 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006372
Willy Tarreauf64d1412010-10-07 20:06:11 +02006373 /* if the cookie has an expiration date and the proxy wants to check
6374 * it, then we do that now. We first check if the cookie is too old,
6375 * then only if it has expired. We detect strict overflow because the
6376 * time resolution here is not great (4 seconds). Cookies with dates
6377 * in the future are ignored if their offset is beyond one day. This
6378 * allows an admin to fix timezone issues without expiring everyone
6379 * and at the same time avoids keeping unwanted side effects for too
6380 * long.
6381 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006382 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6383 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006384 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006385 txn->flags &= ~TX_CK_MASK;
6386 txn->flags |= TX_CK_OLD;
6387 delim = val_beg; // let's pretend we have not found the cookie
6388 txn->cookie_first_date = 0;
6389 txn->cookie_last_date = 0;
6390 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006391 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6392 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006393 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006394 txn->flags &= ~TX_CK_MASK;
6395 txn->flags |= TX_CK_EXPIRED;
6396 delim = val_beg; // let's pretend we have not found the cookie
6397 txn->cookie_first_date = 0;
6398 txn->cookie_last_date = 0;
6399 }
6400
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006401 /* Here, we'll look for the first running server which supports the cookie.
6402 * This allows to share a same cookie between several servers, for example
6403 * to dedicate backup servers to specific servers only.
6404 * However, to prevent clients from sticking to cookie-less backup server
6405 * when they have incidentely learned an empty cookie, we simply ignore
6406 * empty cookies and mark them as invalid.
6407 * The same behaviour is applied when persistence must be ignored.
6408 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006409 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006410 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006411
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006412 while (srv) {
6413 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6414 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006415 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006416 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006417 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006418 /* we found the server and we can use it */
6419 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006420 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006421 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006422 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006423 break;
6424 } else {
6425 /* we found a server, but it's down,
6426 * mark it as such and go on in case
6427 * another one is available.
6428 */
6429 txn->flags &= ~TX_CK_MASK;
6430 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006431 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006432 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006433 srv = srv->next;
6434 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006435
Willy Tarreauf64d1412010-10-07 20:06:11 +02006436 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006437 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006438 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006439 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006440 txn->flags |= TX_CK_UNUSED;
6441 else
6442 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006443 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006444
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006445 /* depending on the cookie mode, we may have to either :
6446 * - delete the complete cookie if we're in insert+indirect mode, so that
6447 * the server never sees it ;
6448 * - remove the server id from the cookie value, and tag the cookie as an
6449 * application cookie so that it does not get accidentely removed later,
6450 * if we're in cookie prefix mode
6451 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006452 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006453 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006454
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006455 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006456 val_end += delta;
6457 next += delta;
6458 hdr_end += delta;
6459 hdr_next += delta;
6460 cur_hdr->len += delta;
6461 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006462
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006463 del_from = NULL;
6464 preserve_hdr = 1; /* we want to keep this cookie */
6465 }
6466 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006467 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006468 del_from = prev;
6469 }
6470 } else {
6471 /* This is not our cookie, so we must preserve it. But if we already
6472 * scheduled another cookie for removal, we cannot remove the
6473 * complete header, but we can remove the previous block itself.
6474 */
6475 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006476
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006477 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006478 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006479 if (att_beg >= del_from)
6480 att_beg += delta;
6481 if (att_end >= del_from)
6482 att_end += delta;
6483 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006484 val_end += delta;
6485 next += delta;
6486 hdr_end += delta;
6487 hdr_next += delta;
6488 cur_hdr->len += delta;
6489 http_msg_move_end(&txn->req, delta);
6490 prev = del_from;
6491 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006492 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006493 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006494
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006495 /* continue with next cookie on this header line */
6496 att_beg = next;
6497 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006498
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006499 /* There are no more cookies on this line.
6500 * We may still have one (or several) marked for deletion at the
6501 * end of the line. We must do this now in two ways :
6502 * - if some cookies must be preserved, we only delete from the
6503 * mark to the end of line ;
6504 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006505 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006506 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006507 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006508 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006509 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006510 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006511 cur_hdr->len += delta;
6512 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006513 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006514
6515 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006516 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6517 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006518 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006519 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006520 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006521 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006522 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006523 }
6524
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006525 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006526 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006527 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006528}
6529
6530
Willy Tarreaua15645d2007-03-18 16:22:39 +01006531/* Iterate the same filter through all response headers contained in <rtr>.
6532 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6533 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006534int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006535{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006536 char *cur_ptr, *cur_end, *cur_next;
6537 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006538 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006539 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006540 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006541
6542 last_hdr = 0;
6543
Willy Tarreauf37954d2018-06-15 18:31:02 +02006544 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006545 old_idx = 0;
6546
6547 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006548 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006549 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006550 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006551 (exp->action == ACT_ALLOW ||
6552 exp->action == ACT_DENY))
6553 return 0;
6554
6555 cur_idx = txn->hdr_idx.v[old_idx].next;
6556 if (!cur_idx)
6557 break;
6558
6559 cur_hdr = &txn->hdr_idx.v[cur_idx];
6560 cur_ptr = cur_next;
6561 cur_end = cur_ptr + cur_hdr->len;
6562 cur_next = cur_end + cur_hdr->cr + 1;
6563
6564 /* Now we have one header between cur_ptr and cur_end,
6565 * and the next header starts at cur_next.
6566 */
6567
Willy Tarreau15a53a42015-01-21 13:39:42 +01006568 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006569 switch (exp->action) {
6570 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006571 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006572 last_hdr = 1;
6573 break;
6574
6575 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006576 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006577 last_hdr = 1;
6578 break;
6579
6580 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006581 len = exp_replace(trash.area,
6582 trash.size, cur_ptr,
6583 exp->replace, pmatch);
6584 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006585 return -1;
6586
Willy Tarreau6e27be12018-08-22 04:46:47 +02006587 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6588
Willy Tarreaua15645d2007-03-18 16:22:39 +01006589 /* FIXME: if the user adds a newline in the replacement, the
6590 * index will not be recalculated for now, and the new line
6591 * will not be counted as a new header.
6592 */
6593
6594 cur_end += delta;
6595 cur_next += delta;
6596 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006597 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006598 break;
6599
6600 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006601 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006602 cur_next += delta;
6603
Willy Tarreaufa355d42009-11-29 18:12:29 +01006604 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006605 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6606 txn->hdr_idx.used--;
6607 cur_hdr->len = 0;
6608 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006609 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006610 break;
6611
6612 }
6613 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006614
6615 /* keep the link from this header to next one in case of later
6616 * removal of next header.
6617 */
6618 old_idx = cur_idx;
6619 }
6620 return 0;
6621}
6622
6623
6624/* Apply the filter to the status line in the response buffer <rtr>.
6625 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6626 * or -1 if a replacement resulted in an invalid status line.
6627 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006628int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006629{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006630 char *cur_ptr, *cur_end;
6631 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006632 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006633 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006634
Willy Tarreau3d300592007-03-18 18:34:41 +01006635 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006636 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006637 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006638 (exp->action == ACT_ALLOW ||
6639 exp->action == ACT_DENY))
6640 return 0;
6641 else if (exp->action == ACT_REMOVE)
6642 return 0;
6643
6644 done = 0;
6645
Willy Tarreauf37954d2018-06-15 18:31:02 +02006646 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006647 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006648
6649 /* Now we have the status line between cur_ptr and cur_end */
6650
Willy Tarreau15a53a42015-01-21 13:39:42 +01006651 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006652 switch (exp->action) {
6653 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006654 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006655 done = 1;
6656 break;
6657
6658 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006659 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006660 done = 1;
6661 break;
6662
6663 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006664 len = exp_replace(trash.area, trash.size,
6665 cur_ptr, exp->replace, pmatch);
6666 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006667 return -1;
6668
Willy Tarreau6e27be12018-08-22 04:46:47 +02006669 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6670
Willy Tarreaua15645d2007-03-18 16:22:39 +01006671 /* FIXME: if the user adds a newline in the replacement, the
6672 * index will not be recalculated for now, and the new line
6673 * will not be counted as a new header.
6674 */
6675
Willy Tarreaufa355d42009-11-29 18:12:29 +01006676 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006677 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006678 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006679 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006680 cur_ptr, cur_end + 1,
6681 NULL, NULL);
6682 if (unlikely(!cur_end))
6683 return -1;
6684
6685 /* we have a full respnse and we know that we have either a CR
6686 * or an LF at <ptr>.
6687 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006688 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006689 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006690 /* there is no point trying this regex on headers */
6691 return 1;
6692 }
6693 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006694 return done;
6695}
6696
6697
6698
6699/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006700 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006701 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6702 * unparsable response.
6703 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006704int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006705{
Willy Tarreau192252e2015-04-04 01:47:55 +02006706 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006707 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006708 struct hdr_exp *exp;
6709
6710 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006711 int ret;
6712
6713 /*
6714 * The interleaving of transformations and verdicts
6715 * makes it difficult to decide to continue or stop
6716 * the evaluation.
6717 */
6718
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006719 if (txn->flags & TX_SVDENY)
6720 break;
6721
Willy Tarreau3d300592007-03-18 18:34:41 +01006722 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006723 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6724 exp->action == ACT_PASS)) {
6725 exp = exp->next;
6726 continue;
6727 }
6728
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006729 /* if this filter had a condition, evaluate it now and skip to
6730 * next filter if the condition does not match.
6731 */
6732 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006733 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006734 ret = acl_pass(ret);
6735 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6736 ret = !ret;
6737 if (!ret)
6738 continue;
6739 }
6740
Willy Tarreaua15645d2007-03-18 16:22:39 +01006741 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006742 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006743 if (unlikely(ret < 0))
6744 return -1;
6745
6746 if (likely(ret == 0)) {
6747 /* The filter did not match the response, it can be
6748 * iterated through all headers.
6749 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006750 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6751 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006752 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006753 }
6754 return 0;
6755}
6756
6757
Willy Tarreaua15645d2007-03-18 16:22:39 +01006758/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006759 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006760 * desirable to call it only when needed. This function is also used when we
6761 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006762 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006763void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006764{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006765 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006766 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006767 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006768 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006769 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006770 char *hdr_beg, *hdr_end, *hdr_next;
6771 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006772
Willy Tarreaua15645d2007-03-18 16:22:39 +01006773 /* Iterate through the headers.
6774 * we start with the start line.
6775 */
6776 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006777 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006778
6779 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6780 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006781 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006782
6783 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006784 hdr_beg = hdr_next;
6785 hdr_end = hdr_beg + cur_hdr->len;
6786 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006787
Willy Tarreau24581ba2010-08-31 22:39:35 +02006788 /* We have one full header between hdr_beg and hdr_end, and the
6789 * next header starts at hdr_next. We're only interested in
6790 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006791 */
6792
Willy Tarreau24581ba2010-08-31 22:39:35 +02006793 is_cookie2 = 0;
6794 prev = hdr_beg + 10;
6795 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006796 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006797 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6798 if (!val) {
6799 old_idx = cur_idx;
6800 continue;
6801 }
6802 is_cookie2 = 1;
6803 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006804 }
6805
Willy Tarreau24581ba2010-08-31 22:39:35 +02006806 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6807 * <prev> points to the colon.
6808 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006809 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006810
Willy Tarreau24581ba2010-08-31 22:39:35 +02006811 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6812 * check-cache is enabled) and we are not interested in checking
6813 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006814 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006815 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006816 return;
6817
Willy Tarreau24581ba2010-08-31 22:39:35 +02006818 /* OK so now we know we have to process this response cookie.
6819 * The format of the Set-Cookie header is slightly different
6820 * from the format of the Cookie header in that it does not
6821 * support the comma as a cookie delimiter (thus the header
6822 * cannot be folded) because the Expires attribute described in
6823 * the original Netscape's spec may contain an unquoted date
6824 * with a comma inside. We have to live with this because
6825 * many browsers don't support Max-Age and some browsers don't
6826 * support quoted strings. However the Set-Cookie2 header is
6827 * clean.
6828 *
6829 * We have to keep multiple pointers in order to support cookie
6830 * removal at the beginning, middle or end of header without
6831 * corrupting the header (in case of set-cookie2). A special
6832 * pointer, <scav> points to the beginning of the set-cookie-av
6833 * fields after the first semi-colon. The <next> pointer points
6834 * either to the end of line (set-cookie) or next unquoted comma
6835 * (set-cookie2). All of these headers are valid :
6836 *
6837 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6838 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6839 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6840 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6841 * | | | | | | | | | |
6842 * | | | | | | | | +-> next hdr_end <--+
6843 * | | | | | | | +------------> scav
6844 * | | | | | | +--------------> val_end
6845 * | | | | | +--------------------> val_beg
6846 * | | | | +----------------------> equal
6847 * | | | +------------------------> att_end
6848 * | | +----------------------------> att_beg
6849 * | +------------------------------> prev
6850 * +-----------------------------------------> hdr_beg
6851 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006852
Willy Tarreau24581ba2010-08-31 22:39:35 +02006853 for (; prev < hdr_end; prev = next) {
6854 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006855
Willy Tarreau24581ba2010-08-31 22:39:35 +02006856 /* find att_beg */
6857 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006858 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006859 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006860
Willy Tarreau24581ba2010-08-31 22:39:35 +02006861 /* find att_end : this is the first character after the last non
6862 * space before the equal. It may be equal to hdr_end.
6863 */
6864 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006865
Willy Tarreau24581ba2010-08-31 22:39:35 +02006866 while (equal < hdr_end) {
6867 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6868 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006869 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006870 continue;
6871 att_end = equal;
6872 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006873
Willy Tarreau24581ba2010-08-31 22:39:35 +02006874 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6875 * is between <att_beg> and <equal>, both may be identical.
6876 */
6877
6878 /* look for end of cookie if there is an equal sign */
6879 if (equal < hdr_end && *equal == '=') {
6880 /* look for the beginning of the value */
6881 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006882 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006883 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006884
Willy Tarreau24581ba2010-08-31 22:39:35 +02006885 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006886 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006887
6888 /* make val_end point to the first white space or delimitor after the value */
6889 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006890 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006891 val_end--;
6892 } else {
6893 /* <equal> points to next comma, semi-colon or EOL */
6894 val_beg = val_end = next = equal;
6895 }
6896
6897 if (next < hdr_end) {
6898 /* Set-Cookie2 supports multiple cookies, and <next> points to
6899 * a colon or semi-colon before the end. So skip all attr-value
6900 * pairs and look for the next comma. For Set-Cookie, since
6901 * commas are permitted in values, skip to the end.
6902 */
6903 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02006904 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006905 else
6906 next = hdr_end;
6907 }
6908
6909 /* Now everything is as on the diagram above */
6910
6911 /* Ignore cookies with no equal sign */
6912 if (equal == val_end)
6913 continue;
6914
6915 /* If there are spaces around the equal sign, we need to
6916 * strip them otherwise we'll get trouble for cookie captures,
6917 * or even for rewrites. Since this happens extremely rarely,
6918 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006919 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006920 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6921 int stripped_before = 0;
6922 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006923
Willy Tarreau24581ba2010-08-31 22:39:35 +02006924 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006925 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006926 equal += stripped_before;
6927 val_beg += stripped_before;
6928 }
6929
6930 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006931 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006932 val_beg += stripped_after;
6933 stripped_before += stripped_after;
6934 }
6935
6936 val_end += stripped_before;
6937 next += stripped_before;
6938 hdr_end += stripped_before;
6939 hdr_next += stripped_before;
6940 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006941 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006942 }
6943
6944 /* First, let's see if we want to capture this cookie. We check
6945 * that we don't already have a server side cookie, because we
6946 * can only capture one. Also as an optimisation, we ignore
6947 * cookies shorter than the declared name.
6948 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006949 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006950 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006951 (val_end - att_beg >= sess->fe->capture_namelen) &&
6952 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006953 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006954 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006955 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01006956 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006957 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006958 if (log_len > sess->fe->capture_len)
6959 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01006960 memcpy(txn->srv_cookie, att_beg, log_len);
6961 txn->srv_cookie[log_len] = 0;
6962 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006963 }
6964
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006965 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006966 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006967 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006968 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6969 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006970 /* assume passive cookie by default */
6971 txn->flags &= ~TX_SCK_MASK;
6972 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006973
6974 /* If the cookie is in insert mode on a known server, we'll delete
6975 * this occurrence because we'll insert another one later.
6976 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006977 * a direct access.
6978 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006979 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006980 /* The "preserve" flag was set, we don't want to touch the
6981 * server's cookie.
6982 */
6983 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006984 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006985 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006986 /* this cookie must be deleted */
6987 if (*prev == ':' && next == hdr_end) {
6988 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006989 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006990 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6991 txn->hdr_idx.used--;
6992 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006993 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006994 hdr_next += delta;
6995 http_msg_move_end(&txn->rsp, delta);
6996 /* note: while both invalid now, <next> and <hdr_end>
6997 * are still equal, so the for() will stop as expected.
6998 */
6999 } else {
7000 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007001 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007002 next = prev;
7003 hdr_end += delta;
7004 hdr_next += delta;
7005 cur_hdr->len += delta;
7006 http_msg_move_end(&txn->rsp, delta);
7007 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007008 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007009 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007010 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007011 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007012 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007013 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007014 * with this server since we know it.
7015 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007016 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007017 next += delta;
7018 hdr_end += delta;
7019 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007020 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007021 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007022
Willy Tarreauf1348312010-10-07 15:54:11 +02007023 txn->flags &= ~TX_SCK_MASK;
7024 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007025 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007026 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007027 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007028 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007029 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007030 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007031 next += delta;
7032 hdr_end += delta;
7033 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007034 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007035 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007036
Willy Tarreau827aee92011-03-10 16:55:02 +01007037 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007038 txn->flags &= ~TX_SCK_MASK;
7039 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007040 }
7041 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007042 /* that's done for this cookie, check the next one on the same
7043 * line when next != hdr_end (only if is_cookie2).
7044 */
7045 }
7046 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007047 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007048 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007049}
7050
7051
Willy Tarreaua15645d2007-03-18 16:22:39 +01007052/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007053 * Parses the Cache-Control and Pragma request header fields to determine if
7054 * the request may be served from the cache and/or if it is cacheable. Updates
7055 * s->txn->flags.
7056 */
7057void check_request_for_cacheability(struct stream *s, struct channel *chn)
7058{
7059 struct http_txn *txn = s->txn;
7060 char *p1, *p2;
7061 char *cur_ptr, *cur_end, *cur_next;
7062 int pragma_found;
7063 int cc_found;
7064 int cur_idx;
7065
7066 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7067 return; /* nothing more to do here */
7068
7069 cur_idx = 0;
7070 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007071 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007072
7073 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7074 struct hdr_idx_elem *cur_hdr;
7075 int val;
7076
7077 cur_hdr = &txn->hdr_idx.v[cur_idx];
7078 cur_ptr = cur_next;
7079 cur_end = cur_ptr + cur_hdr->len;
7080 cur_next = cur_end + cur_hdr->cr + 1;
7081
7082 /* We have one full header between cur_ptr and cur_end, and the
7083 * next header starts at cur_next.
7084 */
7085
7086 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7087 if (val) {
7088 if ((cur_end - (cur_ptr + val) >= 8) &&
7089 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7090 pragma_found = 1;
7091 continue;
7092 }
7093 }
7094
William Lallemand8a16fe02018-05-22 11:04:33 +02007095 /* Don't use the cache and don't try to store if we found the
7096 * Authorization header */
7097 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7098 if (val) {
7099 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7100 txn->flags |= TX_CACHE_IGNORE;
7101 continue;
7102 }
7103
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007104 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7105 if (!val)
7106 continue;
7107
7108 /* OK, right now we know we have a cache-control header at cur_ptr */
7109 cc_found = 1;
7110 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7111
7112 if (p1 >= cur_end) /* no more info */
7113 continue;
7114
7115 /* p1 is at the beginning of the value */
7116 p2 = p1;
7117 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7118 p2++;
7119
7120 /* we have a complete value between p1 and p2. We don't check the
7121 * values after max-age, max-stale nor min-fresh, we simply don't
7122 * use the cache when they're specified.
7123 */
7124 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7125 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7126 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7127 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7128 txn->flags |= TX_CACHE_IGNORE;
7129 continue;
7130 }
7131
7132 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7133 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7134 continue;
7135 }
7136 }
7137
7138 /* RFC7234#5.4:
7139 * When the Cache-Control header field is also present and
7140 * understood in a request, Pragma is ignored.
7141 * When the Cache-Control header field is not present in a
7142 * request, caches MUST consider the no-cache request
7143 * pragma-directive as having the same effect as if
7144 * "Cache-Control: no-cache" were present.
7145 */
7146 if (!cc_found && pragma_found)
7147 txn->flags |= TX_CACHE_IGNORE;
7148}
7149
7150/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007151 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007152 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007153void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007154{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007155 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007156 char *p1, *p2;
7157
7158 char *cur_ptr, *cur_end, *cur_next;
7159 int cur_idx;
7160
Willy Tarreau12b32f22017-12-21 16:08:09 +01007161 if (txn->status < 200) {
7162 /* do not try to cache interim responses! */
7163 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007164 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007165 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007166
7167 /* Iterate through the headers.
7168 * we start with the start line.
7169 */
7170 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007171 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007172
7173 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7174 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007175 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007176
7177 cur_hdr = &txn->hdr_idx.v[cur_idx];
7178 cur_ptr = cur_next;
7179 cur_end = cur_ptr + cur_hdr->len;
7180 cur_next = cur_end + cur_hdr->cr + 1;
7181
7182 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007183 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007184 */
7185
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007186 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7187 if (val) {
7188 if ((cur_end - (cur_ptr + val) >= 8) &&
7189 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7190 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7191 return;
7192 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007193 }
7194
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007195 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7196 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007197 continue;
7198
7199 /* OK, right now we know we have a cache-control header at cur_ptr */
7200
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007201 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007202
7203 if (p1 >= cur_end) /* no more info */
7204 continue;
7205
7206 /* p1 is at the beginning of the value */
7207 p2 = p1;
7208
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007209 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007210 p2++;
7211
7212 /* we have a complete value between p1 and p2 */
7213 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007214 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7215 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7216 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7217 continue;
7218 }
7219
Willy Tarreaua15645d2007-03-18 16:22:39 +01007220 /* we have something of the form no-cache="set-cookie" */
7221 if ((cur_end - p1 >= 21) &&
7222 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7223 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007224 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007225 continue;
7226 }
7227
7228 /* OK, so we know that either p2 points to the end of string or to a comma */
7229 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007230 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007231 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007232 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007233 return;
7234 }
7235
7236 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007237 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007238 continue;
7239 }
7240 }
7241}
7242
Willy Tarreau58f10d72006-12-04 02:26:12 +01007243
Willy Tarreaub2513902006-12-17 14:52:38 +01007244/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007245 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007246 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007247 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007248 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007249 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007250 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007251 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007252 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007253int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007254{
7255 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007256 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007257 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007258
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007259 if (!uri_auth)
7260 return 0;
7261
Cyril Bonté70be45d2010-10-12 00:14:35 +02007262 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007263 return 0;
7264
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007265 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007266 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007267 return 0;
7268
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007269 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007270 return 0;
7271
Willy Tarreaub2513902006-12-17 14:52:38 +01007272 return 1;
7273}
7274
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007275/* Append the description of what is present in error snapshot <es> into <out>.
7276 * The description must be small enough to always fit in a trash. The output
7277 * buffer may be the trash so the trash must not be used inside this function.
7278 */
7279void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7280{
7281 chunk_appendf(&trash,
7282 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7283 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7284 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7285 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7286 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7287 es->ctx.http.m_flags, es->ctx.http.m_clen,
7288 es->ctx.http.m_blen, es->ctx.http.b_flags);
7289}
7290
Willy Tarreau4076a152009-04-02 15:18:36 +02007291/*
7292 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007293 * By default it tries to report the error position as msg->err_pos. However if
7294 * this one is not set, it will then report msg->next, which is the last known
7295 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007296 * displays buffers as a contiguous area starting at buf->p. The direction is
7297 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007298 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007299void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007300 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007301 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007302{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007303 union error_snapshot_ctx ctx;
7304 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007305
Willy Tarreauef3ca732018-09-07 15:47:35 +02007306 /* http-specific part now */
7307 ctx.http.sid = s->uniq_id;
7308 ctx.http.state = state;
7309 ctx.http.b_flags = msg->chn->flags;
7310 ctx.http.s_flags = s->flags;
7311 ctx.http.t_flags = s->txn->flags;
7312 ctx.http.m_flags = msg->flags;
7313 ctx.http.m_clen = msg->chunk_len;
7314 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007315
Willy Tarreauef3ca732018-09-07 15:47:35 +02007316 ofs = msg->chn->total - ci_data(msg->chn);
7317 if (ofs < 0)
7318 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007319
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007320 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007321 other_end, s->target,
7322 strm_sess(s), &msg->chn->buf,
7323 ofs, co_data(msg->chn),
7324 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7325 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007326}
Willy Tarreaub2513902006-12-17 14:52:38 +01007327
Willy Tarreau294c4732011-12-16 21:35:50 +01007328/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7329 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7330 * performed over the whole headers. Otherwise it must contain a valid header
7331 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7332 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7333 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7334 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007335 * -1. The value fetch stops at commas, so this function is suited for use with
7336 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007337 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007338 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007339unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007340 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007341 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007342{
Willy Tarreau294c4732011-12-16 21:35:50 +01007343 struct hdr_ctx local_ctx;
7344 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007345 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007346 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007347 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007348
Willy Tarreau294c4732011-12-16 21:35:50 +01007349 if (!ctx) {
7350 local_ctx.idx = 0;
7351 ctx = &local_ctx;
7352 }
7353
Willy Tarreaubce70882009-09-07 11:51:47 +02007354 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007355 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007356 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007357 occ--;
7358 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007359 *vptr = ctx->line + ctx->val;
7360 *vlen = ctx->vlen;
7361 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007362 }
7363 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007364 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007365 }
7366
7367 /* negative occurrence, we scan all the list then walk back */
7368 if (-occ > MAX_HDR_HISTORY)
7369 return 0;
7370
Willy Tarreau294c4732011-12-16 21:35:50 +01007371 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007372 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007373 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7374 len_hist[hist_ptr] = ctx->vlen;
7375 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007376 hist_ptr = 0;
7377 found++;
7378 }
7379 if (-occ > found)
7380 return 0;
7381 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007382 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7383 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7384 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007385 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007386 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007387 if (hist_ptr >= MAX_HDR_HISTORY)
7388 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007389 *vptr = ptr_hist[hist_ptr];
7390 *vlen = len_hist[hist_ptr];
7391 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007392}
7393
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007394/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7395 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7396 * performed over the whole headers. Otherwise it must contain a valid header
7397 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7398 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7399 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7400 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7401 * -1. This function differs from http_get_hdr() in that it only returns full
7402 * line header values and does not stop at commas.
7403 * The return value is 0 if nothing was found, or non-zero otherwise.
7404 */
7405unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7406 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007407 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007408{
7409 struct hdr_ctx local_ctx;
7410 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007411 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007412 unsigned int hist_ptr;
7413 int found;
7414
7415 if (!ctx) {
7416 local_ctx.idx = 0;
7417 ctx = &local_ctx;
7418 }
7419
7420 if (occ >= 0) {
7421 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007422 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007423 occ--;
7424 if (occ <= 0) {
7425 *vptr = ctx->line + ctx->val;
7426 *vlen = ctx->vlen;
7427 return 1;
7428 }
7429 }
7430 return 0;
7431 }
7432
7433 /* negative occurrence, we scan all the list then walk back */
7434 if (-occ > MAX_HDR_HISTORY)
7435 return 0;
7436
7437 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007438 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007439 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7440 len_hist[hist_ptr] = ctx->vlen;
7441 if (++hist_ptr >= MAX_HDR_HISTORY)
7442 hist_ptr = 0;
7443 found++;
7444 }
7445 if (-occ > found)
7446 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007447
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007448 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007449 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7450 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7451 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007452 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007453 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007454 if (hist_ptr >= MAX_HDR_HISTORY)
7455 hist_ptr -= MAX_HDR_HISTORY;
7456 *vptr = ptr_hist[hist_ptr];
7457 *vlen = len_hist[hist_ptr];
7458 return 1;
7459}
7460
Willy Tarreaubaaee002006-06-26 02:48:02 +02007461/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007462 * Print a debug line with a header. Always stop at the first CR or LF char,
7463 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7464 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007465 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007466void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007467{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007468 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007469 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007470
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007471 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007472 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007473 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007474 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 +02007475
7476 for (max = 0; start + max < end; max++)
7477 if (start[max] == '\r' || start[max] == '\n')
7478 break;
7479
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007480 UBOUND(max, trash.size - trash.data - 3);
7481 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7482 trash.area[trash.data++] = '\n';
7483 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007484}
7485
Willy Tarreaueee5b512015-04-03 23:46:31 +02007486
7487/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7488 * The hdr_idx is allocated as well. In case of allocation failure, everything
7489 * allocated is freed and NULL is returned. Otherwise the new transaction is
7490 * assigned to the stream and returned.
7491 */
7492struct http_txn *http_alloc_txn(struct stream *s)
7493{
7494 struct http_txn *txn = s->txn;
7495
7496 if (txn)
7497 return txn;
7498
Willy Tarreaubafbe012017-11-24 17:34:44 +01007499 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007500 if (!txn)
7501 return txn;
7502
7503 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007504 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007505 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007506 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007507 return NULL;
7508 }
7509
7510 s->txn = txn;
7511 return txn;
7512}
7513
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007514void http_txn_reset_req(struct http_txn *txn)
7515{
7516 txn->req.flags = 0;
7517 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7518 txn->req.next = 0;
7519 txn->req.chunk_len = 0LL;
7520 txn->req.body_len = 0LL;
7521 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7522}
7523
7524void http_txn_reset_res(struct http_txn *txn)
7525{
7526 txn->rsp.flags = 0;
7527 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7528 txn->rsp.next = 0;
7529 txn->rsp.chunk_len = 0LL;
7530 txn->rsp.body_len = 0LL;
7531 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7532}
7533
Willy Tarreau0937bc42009-12-22 15:03:09 +01007534/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007535 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007536 * the required fields are properly allocated and that we only need to (re)init
7537 * them. This should be used before processing any new request.
7538 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007539void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007540{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007541 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007542 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007543
7544 txn->flags = 0;
7545 txn->status = -1;
7546
Willy Tarreauf64d1412010-10-07 20:06:11 +02007547 txn->cookie_first_date = 0;
7548 txn->cookie_last_date = 0;
7549
Willy Tarreaueee5b512015-04-03 23:46:31 +02007550 txn->srv_cookie = NULL;
7551 txn->cli_cookie = NULL;
7552 txn->uri = NULL;
7553
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007554 http_txn_reset_req(txn);
7555 http_txn_reset_res(txn);
7556
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007557 txn->req.chn = &s->req;
7558 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007559
7560 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007561
7562 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7563 if (fe->options2 & PR_O2_REQBUG_OK)
7564 txn->req.err_pos = -1; /* let buggy requests pass */
7565
Willy Tarreau0937bc42009-12-22 15:03:09 +01007566 if (txn->hdr_idx.v)
7567 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007568
7569 vars_init(&s->vars_txn, SCOPE_TXN);
7570 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007571}
7572
7573/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007574void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007575{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007576 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007577 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007578
7579 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007580 pool_free(pool_head_requri, txn->uri);
7581 pool_free(pool_head_capture, txn->cli_cookie);
7582 pool_free(pool_head_capture, txn->srv_cookie);
7583 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007584
William Lallemanda73203e2012-03-12 12:48:57 +01007585 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007586 txn->uri = NULL;
7587 txn->srv_cookie = NULL;
7588 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007589
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007590 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007591 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007592 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007593 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007594 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007595 }
7596
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007597 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007598 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007599 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007600 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007601 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007602 }
7603
Willy Tarreau6204cd92016-03-10 16:33:04 +01007604 vars_prune(&s->vars_txn, s->sess, s);
7605 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007606}
7607
7608/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007609void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007610{
7611 http_end_txn(s);
7612 http_init_txn(s);
7613
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007614 /* reinitialise the current rule list pointer to NULL. We are sure that
7615 * any rulelist match the NULL pointer.
7616 */
7617 s->current_rule_list = NULL;
7618
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007619 s->be = strm_fe(s);
7620 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007621 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007622 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007623 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007624 /* re-init store persistence */
7625 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007626 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007627
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007628 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007629
Willy Tarreau739cfba2010-01-25 23:11:14 +01007630 /* We must trim any excess data from the response buffer, because we
7631 * may have blocked an invalid response from a server that we don't
7632 * want to accidentely forward once we disable the analysers, nor do
7633 * we want those data to come along with next response. A typical
7634 * example of such data would be from a buggy server responding to
7635 * a HEAD with some data, or sending more than the advertised
7636 * content-length.
7637 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007638 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007639 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007640
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007641 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007642 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007643
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007644 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007645 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007646
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007647 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007648 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007649
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007650 s->req.rex = TICK_ETERNITY;
7651 s->req.wex = TICK_ETERNITY;
7652 s->req.analyse_exp = TICK_ETERNITY;
7653 s->res.rex = TICK_ETERNITY;
7654 s->res.wex = TICK_ETERNITY;
7655 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007656 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007657}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007658
Willy Tarreau79e57332018-10-02 16:01:16 +02007659/* This function executes one of the set-{method,path,query,uri} actions. It
7660 * takes the string from the variable 'replace' with length 'len', then modifies
7661 * the relevant part of the request line accordingly. Then it updates various
7662 * pointers to the next elements which were moved, and the total buffer length.
7663 * It finds the action to be performed in p[2], previously filled by function
7664 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7665 * error, though this can be revisited when this code is finally exploited.
7666 *
7667 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7668 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007669 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007670 * In query string case, the mark question '?' must be set at the start of the
7671 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007672 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007673int http_replace_req_line(int action, const char *replace, int len,
7674 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007675{
Willy Tarreau79e57332018-10-02 16:01:16 +02007676 struct http_txn *txn = s->txn;
7677 char *cur_ptr, *cur_end;
7678 int offset = 0;
7679 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007680
Willy Tarreau79e57332018-10-02 16:01:16 +02007681 switch (action) {
7682 case 0: // method
7683 cur_ptr = ci_head(&s->req);
7684 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007685
Willy Tarreau79e57332018-10-02 16:01:16 +02007686 /* adjust req line offsets and lengths */
7687 delta = len - offset - (cur_end - cur_ptr);
7688 txn->req.sl.rq.m_l += delta;
7689 txn->req.sl.rq.u += delta;
7690 txn->req.sl.rq.v += delta;
7691 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007692
Willy Tarreau79e57332018-10-02 16:01:16 +02007693 case 1: // path
7694 cur_ptr = http_txn_get_path(txn);
7695 if (!cur_ptr)
7696 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007697
Willy Tarreau79e57332018-10-02 16:01:16 +02007698 cur_end = cur_ptr;
7699 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7700 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007701
Willy Tarreau79e57332018-10-02 16:01:16 +02007702 /* adjust req line offsets and lengths */
7703 delta = len - offset - (cur_end - cur_ptr);
7704 txn->req.sl.rq.u_l += delta;
7705 txn->req.sl.rq.v += delta;
7706 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007707
Willy Tarreau79e57332018-10-02 16:01:16 +02007708 case 2: // query
7709 offset = 1;
7710 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7711 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7712 while (cur_ptr < cur_end && *cur_ptr != '?')
7713 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007714
Willy Tarreau79e57332018-10-02 16:01:16 +02007715 /* skip the question mark or indicate that we must insert it
7716 * (but only if the format string is not empty then).
7717 */
7718 if (cur_ptr < cur_end)
7719 cur_ptr++;
7720 else if (len > 1)
7721 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007722
Willy Tarreau79e57332018-10-02 16:01:16 +02007723 /* adjust req line offsets and lengths */
7724 delta = len - offset - (cur_end - cur_ptr);
7725 txn->req.sl.rq.u_l += delta;
7726 txn->req.sl.rq.v += delta;
7727 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007728
Willy Tarreau79e57332018-10-02 16:01:16 +02007729 case 3: // uri
7730 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7731 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007732
Willy Tarreau79e57332018-10-02 16:01:16 +02007733 /* adjust req line offsets and lengths */
7734 delta = len - offset - (cur_end - cur_ptr);
7735 txn->req.sl.rq.u_l += delta;
7736 txn->req.sl.rq.v += delta;
7737 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007738
Willy Tarreau79e57332018-10-02 16:01:16 +02007739 default:
7740 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007741 }
7742
Willy Tarreau79e57332018-10-02 16:01:16 +02007743 /* commit changes and adjust end of message */
7744 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7745 txn->req.sl.rq.l += delta;
7746 txn->hdr_idx.v[0].len += delta;
7747 http_msg_move_end(&txn->req, delta);
7748 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007749}
7750
Willy Tarreau79e57332018-10-02 16:01:16 +02007751/* This function replace the HTTP status code and the associated message. The
7752 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007753 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007754void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007755{
Willy Tarreau79e57332018-10-02 16:01:16 +02007756 struct http_txn *txn = s->txn;
7757 char *cur_ptr, *cur_end;
7758 int delta;
7759 char *res;
7760 int c_l;
7761 const char *msg = reason;
7762 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007763
Willy Tarreau79e57332018-10-02 16:01:16 +02007764 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007765
Willy Tarreau79e57332018-10-02 16:01:16 +02007766 res = ultoa_o(status, trash.area, trash.size);
7767 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007768
Willy Tarreau79e57332018-10-02 16:01:16 +02007769 trash.area[c_l] = ' ';
7770 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007771
Willy Tarreau79e57332018-10-02 16:01:16 +02007772 /* Do we have a custom reason format string? */
7773 if (msg == NULL)
7774 msg = http_get_reason(status);
7775 msg_len = strlen(msg);
7776 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7777 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007778
Willy Tarreau79e57332018-10-02 16:01:16 +02007779 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7780 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007781
Willy Tarreau79e57332018-10-02 16:01:16 +02007782 /* commit changes and adjust message */
7783 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7784 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007785
Willy Tarreau79e57332018-10-02 16:01:16 +02007786 /* adjust res line offsets and lengths */
7787 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7788 txn->rsp.sl.st.c_l = c_l;
7789 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007790
Willy Tarreau79e57332018-10-02 16:01:16 +02007791 delta = trash.data - (cur_end - cur_ptr);
7792 txn->rsp.sl.st.l += delta;
7793 txn->hdr_idx.v[0].len += delta;
7794 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007795}
7796
Willy Tarreau8797c062007-05-07 00:55:35 +02007797__attribute__((constructor))
7798static void __http_protocol_init(void)
7799{
Willy Tarreau8797c062007-05-07 00:55:35 +02007800}
7801
7802
Willy Tarreau58f10d72006-12-04 02:26:12 +01007803/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007804 * Local variables:
7805 * c-indent-level: 8
7806 * c-basic-offset: 8
7807 * End:
7808 */