blob: 6c7cfcbc075ce8f12210f72144bb23bd2b0faa42 [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
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010026#include <common/base64.h>
Frédéric Lécaillea41d5312018-01-29 12:05:07 +010027#include <common/cfgparse.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020028#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020029#include <common/compat.h>
30#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010031#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020032#include <common/memory.h>
33#include <common/mini-clist.h>
34#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020035#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020036#include <common/time.h>
37#include <common/uri_auth.h>
38#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039
40#include <types/capture.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010041#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020042#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
William Lallemand71bd11a2017-11-20 19:13:14 +010044#include <types/cache.h>
William Lallemand9ed62032016-11-21 17:49:11 +010045#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Willy Tarreau8797c062007-05-07 00:55:35 +020047#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020048#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020049#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010050#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020052#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010053#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010054#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020055#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010056#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020058#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020059#include <proto/frontend.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020060#include <proto/h1.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010062#include <proto/hdr_idx.h>
Patrick Hemmere3faf022018-08-22 10:02:00 -040063#include <proto/hlua.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010064#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020065#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010067#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020069#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010070#include <proto/server.h>
Olivier Houchard985f1392018-11-30 17:31:52 +010071#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020072#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010073#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020074#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020075#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020076#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020077
Cyril Bonté19979e12012-04-04 12:57:21 +020078/* status codes available for the stats admin page (strictly 4 chars length) */
79const char *stat_status_codes[STAT_STATUS_SIZE] = {
80 [STAT_STATUS_DENY] = "DENY",
81 [STAT_STATUS_DONE] = "DONE",
82 [STAT_STATUS_ERRP] = "ERRP",
83 [STAT_STATUS_EXCD] = "EXCD",
84 [STAT_STATUS_NONE] = "NONE",
85 [STAT_STATUS_PART] = "PART",
86 [STAT_STATUS_UNKN] = "UNKN",
87};
88
Willy Tarreau53b6c742006-12-17 13:37:46 +010089/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +010090 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +020091 * the last CRLF. <len> bytes are copied, not counting the CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +010092 * The header is also automatically added to the index <hdr_idx>, and the end
93 * of headers is automatically adjusted. The number of bytes added is returned
94 * on success, otherwise <0 is returned indicating an error.
95 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +010096int http_header_add_tail2(struct http_msg *msg,
97 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +010098{
99 int bytes;
100
Willy Tarreau4d893d42018-07-12 15:43:32 +0200101 bytes = ci_insert_line2(msg->chn, msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100102 if (!bytes)
103 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100104 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100105 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
106}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200108/* Find the first or next occurrence of header <name> in message buffer <sol>
109 * using headers index <idx>, and return it in the <ctx> structure. This
110 * structure holds everything necessary to use the header and find next
111 * occurrence. If its <idx> member is 0, the header is searched from the
112 * beginning. Otherwise, the next occurrence is returned. The function returns
113 * 1 when it finds a value, and 0 when there is no more. It is very similar to
114 * http_find_header2() except that it is designed to work with full-line headers
115 * whose comma is not a delimiter but is part of the syntax. As a special case,
116 * if ctx->val is NULL when searching for a new values of a header, the current
117 * header is rescanned. This allows rescanning after a header deletion.
118 */
119int http_find_full_header2(const char *name, int len,
120 char *sol, struct hdr_idx *idx,
121 struct hdr_ctx *ctx)
122{
123 char *eol, *sov;
124 int cur_idx, old_idx;
125
126 cur_idx = ctx->idx;
127 if (cur_idx) {
128 /* We have previously returned a header, let's search another one */
129 sol = ctx->line;
130 eol = sol + idx->v[cur_idx].len;
131 goto next_hdr;
132 }
133
134 /* first request for this header */
135 sol += hdr_idx_first_pos(idx);
136 old_idx = 0;
137 cur_idx = hdr_idx_first_idx(idx);
138 while (cur_idx) {
139 eol = sol + idx->v[cur_idx].len;
140
141 if (len == 0) {
142 /* No argument was passed, we want any header.
143 * To achieve this, we simply build a fake request. */
144 while (sol + len < eol && sol[len] != ':')
145 len++;
146 name = sol;
147 }
148
149 if ((len < eol - sol) &&
150 (sol[len] == ':') &&
151 (strncasecmp(sol, name, len) == 0)) {
152 ctx->del = len;
153 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100154 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200155 sov++;
156
157 ctx->line = sol;
158 ctx->prev = old_idx;
159 ctx->idx = cur_idx;
160 ctx->val = sov - sol;
161 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100162 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200163 eol--;
164 ctx->tws++;
165 }
166 ctx->vlen = eol - sov;
167 return 1;
168 }
169 next_hdr:
170 sol = eol + idx->v[cur_idx].cr + 1;
171 old_idx = cur_idx;
172 cur_idx = idx->v[cur_idx].next;
173 }
174 return 0;
175}
176
Willy Tarreauc90dc232015-02-20 13:51:36 +0100177/* Find the first or next header field in message buffer <sol> using headers
178 * index <idx>, and return it in the <ctx> structure. This structure holds
179 * everything necessary to use the header and find next occurrence. If its
180 * <idx> member is 0, the first header is retrieved. Otherwise, the next
181 * occurrence is returned. The function returns 1 when it finds a value, and
182 * 0 when there is no more. It is equivalent to http_find_full_header2() with
183 * no header name.
184 */
185int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
186{
187 char *eol, *sov;
188 int cur_idx, old_idx;
189 int len;
190
191 cur_idx = ctx->idx;
192 if (cur_idx) {
193 /* We have previously returned a header, let's search another one */
194 sol = ctx->line;
195 eol = sol + idx->v[cur_idx].len;
196 goto next_hdr;
197 }
198
199 /* first request for this header */
200 sol += hdr_idx_first_pos(idx);
201 old_idx = 0;
202 cur_idx = hdr_idx_first_idx(idx);
203 while (cur_idx) {
204 eol = sol + idx->v[cur_idx].len;
205
206 len = 0;
207 while (1) {
208 if (len >= eol - sol)
209 goto next_hdr;
210 if (sol[len] == ':')
211 break;
212 len++;
213 }
214
215 ctx->del = len;
216 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100217 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100218 sov++;
219
220 ctx->line = sol;
221 ctx->prev = old_idx;
222 ctx->idx = cur_idx;
223 ctx->val = sov - sol;
224 ctx->tws = 0;
225
Willy Tarreau2235b262016-11-05 15:50:20 +0100226 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100227 eol--;
228 ctx->tws++;
229 }
230 ctx->vlen = eol - sov;
231 return 1;
232
233 next_hdr:
234 sol = eol + idx->v[cur_idx].cr + 1;
235 old_idx = cur_idx;
236 cur_idx = idx->v[cur_idx].next;
237 }
238 return 0;
239}
240
Willy Tarreau33a7e692007-06-10 19:45:56 +0200241/* Find the first or next occurrence of header <name> in message buffer <sol>
242 * using headers index <idx>, and return it in the <ctx> structure. This
243 * structure holds everything necessary to use the header and find next
244 * occurrence. If its <idx> member is 0, the header is searched from the
245 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100246 * 1 when it finds a value, and 0 when there is no more. It is designed to work
247 * with headers defined as comma-separated lists. As a special case, if ctx->val
248 * is NULL when searching for a new values of a header, the current header is
249 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200250 */
251int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100252 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200253 struct hdr_ctx *ctx)
254{
Willy Tarreau68085d82010-01-18 14:54:04 +0100255 char *eol, *sov;
256 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200257
Willy Tarreau68085d82010-01-18 14:54:04 +0100258 cur_idx = ctx->idx;
259 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200260 /* We have previously returned a value, let's search
261 * another one on the same line.
262 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200263 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200264 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100265 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200266 eol = sol + idx->v[cur_idx].len;
267
268 if (sov >= eol)
269 /* no more values in this header */
270 goto next_hdr;
271
Willy Tarreau68085d82010-01-18 14:54:04 +0100272 /* values remaining for this header, skip the comma but save it
273 * for later use (eg: for header deletion).
274 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200275 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100276 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200277 sov++;
278
279 goto return_hdr;
280 }
281
282 /* first request for this header */
283 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100284 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200285 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200286 while (cur_idx) {
287 eol = sol + idx->v[cur_idx].len;
288
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200289 if (len == 0) {
290 /* No argument was passed, we want any header.
291 * To achieve this, we simply build a fake request. */
292 while (sol + len < eol && sol[len] != ':')
293 len++;
294 name = sol;
295 }
296
Willy Tarreau33a7e692007-06-10 19:45:56 +0200297 if ((len < eol - sol) &&
298 (sol[len] == ':') &&
299 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100300 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200301 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100302 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200303 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100304
Willy Tarreau33a7e692007-06-10 19:45:56 +0200305 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100306 ctx->prev = old_idx;
307 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200308 ctx->idx = cur_idx;
309 ctx->val = sov - sol;
310
Willy Tarreauab813a42018-09-10 18:41:28 +0200311 eol = http_find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200312 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100313 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200314 eol--;
315 ctx->tws++;
316 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200317 ctx->vlen = eol - sov;
318 return 1;
319 }
320 next_hdr:
321 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100322 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200323 cur_idx = idx->v[cur_idx].next;
324 }
325 return 0;
326}
327
Willy Tarreau68085d82010-01-18 14:54:04 +0100328/* Remove one value of a header. This only works on a <ctx> returned by one of
329 * the http_find_header functions. The value is removed, as well as surrounding
330 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100331 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100332 * message <msg>. The new index is returned. If it is zero, it means there is
333 * no more header, so any processing may stop. The ctx is always left in a form
334 * that can be handled by http_find_header2() to find next occurrence.
335 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100336int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100337{
338 int cur_idx = ctx->idx;
339 char *sol = ctx->line;
340 struct hdr_idx_elem *hdr;
341 int delta, skip_comma;
342
343 if (!cur_idx)
344 return 0;
345
346 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200347 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100348 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200349 delta = b_rep_blk(&msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100350 http_msg_move_end(msg, delta);
351 idx->used--;
352 hdr->len = 0; /* unused entry */
353 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100354 if (idx->tail == ctx->idx)
355 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100356 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100357 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100358 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200359 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100360 return ctx->idx;
361 }
362
363 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200364 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
365 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100366 */
367
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200368 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200369 delta = b_rep_blk(&msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200370 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100371 NULL, 0);
372 hdr->len += delta;
373 http_msg_move_end(msg, delta);
374 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200375 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100376 return ctx->idx;
377}
378
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100379/* This function handles a server error at the stream interface level. The
380 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100381 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100382 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100383 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200384 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200385static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau83061a82018-07-13 11:56:34 +0200386 int err, int finst, const struct buffer *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200387{
Christopher Faulet9768c262018-10-22 09:34:31 +0200388 if (IS_HTX_STRM(s))
389 return htx_server_error(s, si, err, finst, msg);
390
Willy Tarreau2019f952017-03-14 11:07:31 +0100391 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100392 channel_auto_read(si_oc(si));
393 channel_abort(si_oc(si));
394 channel_auto_close(si_oc(si));
395 channel_erase(si_oc(si));
396 channel_auto_close(si_ic(si));
397 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200398 if (msg)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200399 co_inject(si_ic(si), msg->area, msg->data);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200400 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200401 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200402 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200403 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200404}
405
Willy Tarreau87b09662015-04-03 00:22:06 +0200406/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100407 * and message.
408 */
409
Willy Tarreau83061a82018-07-13 11:56:34 +0200410struct buffer *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100411{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200412 const int msgnum = http_get_status_idx(s->txn->status);
413
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100414 if (IS_HTX_STRM(s))
415 return htx_error_message(s);
416
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200417 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200418 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200419 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200420 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100421 else
422 return &http_err_chunks[msgnum];
423}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200424
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100425void
Willy Tarreau83061a82018-07-13 11:56:34 +0200426http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100427{
Christopher Faulet9768c262018-10-22 09:34:31 +0200428 if (IS_HTX_STRM(s))
429 return htx_reply_and_close(s, status, msg);
430
Christopher Fauletd7c91962015-04-30 11:48:27 +0200431 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100432 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100433 stream_int_retnclose(&s->si[0], msg);
434}
435
Willy Tarreau21d2af32008-02-14 20:25:24 +0100436/* Parse the URI from the given transaction (which is assumed to be in request
437 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
438 * It is returned otherwise.
439 */
Willy Tarreau6b952c82018-09-10 17:45:34 +0200440char *http_txn_get_path(const struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100441{
Willy Tarreau6b952c82018-09-10 17:45:34 +0200442 struct ist ret;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100443
Willy Tarreau9d9ccdb2018-10-28 20:13:12 +0100444 if (!txn->req.chn->buf.size)
445 return NULL;
446
Willy Tarreau6b952c82018-09-10 17:45:34 +0200447 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 +0100448
Willy Tarreau6b952c82018-09-10 17:45:34 +0200449 return ret.ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +0100450}
451
Willy Tarreau71241ab2012-12-27 11:30:54 +0100452/* Returns a 302 for a redirectable request that reaches a server working in
453 * in redirect mode. This may only be called just after the stream interface
454 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
455 * follow normal proxy processing. NOTE: this function is designed to support
456 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100457 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200458void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100459{
460 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100461 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100462 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200463 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100464
Christopher Fauletfefc73d2018-10-24 21:18:04 +0200465 if (IS_HTX_STRM(s))
466 return htx_perform_server_redirect(s, si);
467
Willy Tarreauefb453c2008-10-26 20:49:47 +0100468 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200469 trash.data = strlen(HTTP_302);
470 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100471
Willy Tarreaub05e48a2018-09-20 11:12:58 +0200472 srv = __objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100473
Willy Tarreauefb453c2008-10-26 20:49:47 +0100474 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200475 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100476 return;
477
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100478 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100479 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200480 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
481 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100482 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100483
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200484 /* 3: add the request URI. Since it was already forwarded, we need
485 * to temporarily rewind the buffer.
486 */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200487 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200488 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200489
Willy Tarreau6b952c82018-09-10 17:45:34 +0200490 path = http_txn_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200491 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 +0200492
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200493 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200494
Willy Tarreauefb453c2008-10-26 20:49:47 +0100495 if (!path)
496 return;
497
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200498 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100499 return;
500
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200501 memcpy(trash.area + trash.data, path, len);
502 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100503
504 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200505 memcpy(trash.area + trash.data,
506 "\r\nProxy-Connection: close\r\n\r\n", 29);
507 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100508 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200509 memcpy(trash.area + trash.data,
510 "\r\nConnection: close\r\n\r\n", 23);
511 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100512 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100513
514 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200515 si_shutr(si);
516 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100517 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100518 si->state = SI_ST_CLO;
519
520 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +0100521 txn->status = 302;
522 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100523
524 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100525 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500526 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100527}
528
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100529/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100530 * that the server side is closed. Note that err_type is actually a
531 * bitmask, where almost only aborts may be cumulated with other
532 * values. We consider that aborted operations are more important
533 * than timeouts or errors due to the fact that nobody else in the
534 * logs might explain incomplete retries. All others should avoid
535 * being cumulated. It should normally not be possible to have multiple
536 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100537 * Note that connection errors appearing on the second request of a keep-alive
538 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100539 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200540void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100541{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100542 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100543
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200544 /* set s->txn->status for http_error_message(s) */
545 s->txn->status = 503;
546
Willy Tarreauefb453c2008-10-26 20:49:47 +0100547 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200548 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100549 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100550 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200551 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100552 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200553 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100554 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200555 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100556 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100557 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200558 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100559 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100560 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200561 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100562 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200563 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100564 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200565 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100566 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200567 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200568 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200569 http_server_error(s, si, SF_ERR_RESOURCE, 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));
572 else { /* SI_ET_CONN_OTHER and others */
573 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200574 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100575 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200576 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100577}
578
Willy Tarreau42250582007-04-01 01:30:43 +0200579extern const char sess_term_cond[8];
580extern const char sess_fin_state[8];
581extern const char *monthname[12];
Willy Tarreau8ceae722018-11-26 11:58:30 +0100582
583DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
584DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
585
586struct pool_head *pool_head_requri = NULL;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100587struct pool_head *pool_head_capture = NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100588
Willy Tarreau117f59e2007-03-04 18:17:17 +0100589/*
590 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +0200591 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +0100592 */
Christopher Faulet10079f52018-10-03 15:17:28 +0200593void http_capture_headers(char *som, struct hdr_idx *idx,
594 char **cap, struct cap_hdr *cap_hdr)
Willy Tarreau117f59e2007-03-04 18:17:17 +0100595{
596 char *eol, *sol, *col, *sov;
597 int cur_idx;
598 struct cap_hdr *h;
599 int len;
600
601 sol = som + hdr_idx_first_pos(idx);
602 cur_idx = hdr_idx_first_idx(idx);
603
604 while (cur_idx) {
605 eol = sol + idx->v[cur_idx].len;
606
607 col = sol;
608 while (col < eol && *col != ':')
609 col++;
610
611 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100612 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +0100613 sov++;
614
615 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +0200616 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +0100617 (strncasecmp(sol, h->name, h->namelen) == 0)) {
618 if (cap[h->index] == NULL)
619 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +0100620 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100621
622 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100623 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +0100624 continue;
625 }
626
627 len = eol - sov;
628 if (len > h->len)
629 len = h->len;
630
631 memcpy(cap[h->index], sov, len);
632 cap[h->index][len]=0;
633 }
634 }
635 sol = eol + idx->v[cur_idx].cr + 1;
636 cur_idx = idx->v[cur_idx].next;
637 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100638}
639
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200640/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
641 * conversion succeeded, 0 in case of error. If the request was already 1.X,
642 * nothing is done and 1 is returned.
643 */
Willy Tarreau79e57332018-10-02 16:01:16 +0200644int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200645{
646 int delta;
647 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100648 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200649
650 if (msg->sl.rq.v_l != 0)
651 return 1;
652
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300653 /* RFC 1945 allows only GET for HTTP/0.9 requests */
654 if (txn->meth != HTTP_METH_GET)
655 return 0;
656
Willy Tarreauf37954d2018-06-15 18:31:02 +0200657 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200658
659 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300660 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
661 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200662 }
663 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200664 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +0100665 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200666 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200667 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200668 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +0200669 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200670 NULL, NULL);
671 if (unlikely(!cur_end))
672 return 0;
673
674 /* we have a full HTTP/1.0 request now and we know that
675 * we have either a CR or an LF at <ptr>.
676 */
677 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
678 return 1;
679}
680
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100681/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100682 * and "keep-alive" values. If we already know that some headers may safely
683 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100684 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
685 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +0100686 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100687 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
688 * found, and TX_CON_*_SET is adjusted depending on what is left so only
689 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100690 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +0100691 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100692void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +0100693{
Willy Tarreau5b154472009-12-21 20:11:07 +0100694 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100695 const char *hdr_val = "Connection";
696 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +0100697
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100698 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +0100699 return;
700
Willy Tarreau88d349d2010-01-25 12:15:43 +0100701 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
702 hdr_val = "Proxy-Connection";
703 hdr_len = 16;
704 }
705
Willy Tarreau5b154472009-12-21 20:11:07 +0100706 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100707 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200708 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100709 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
710 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100711 if (to_del & 2)
712 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100713 else
714 txn->flags |= TX_CON_KAL_SET;
715 }
716 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
717 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100718 if (to_del & 1)
719 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100720 else
721 txn->flags |= TX_CON_CLO_SET;
722 }
Willy Tarreau50fc7772012-11-11 22:19:57 +0100723 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
724 txn->flags |= TX_HDR_CONN_UPG;
725 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100726 }
727
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100728 txn->flags |= TX_HDR_CONN_PRS;
729 return;
730}
Willy Tarreau5b154472009-12-21 20:11:07 +0100731
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100732/* Apply desired changes on the Connection: header. Values may be removed and/or
733 * added depending on the <wanted> flags, which are exclusively composed of
734 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
735 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
736 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100737void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100738{
739 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100740 const char *hdr_val = "Connection";
741 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100742
743 ctx.idx = 0;
744
Willy Tarreau88d349d2010-01-25 12:15:43 +0100745
746 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
747 hdr_val = "Proxy-Connection";
748 hdr_len = 16;
749 }
750
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100751 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200752 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100753 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
754 if (wanted & TX_CON_KAL_SET)
755 txn->flags |= TX_CON_KAL_SET;
756 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100757 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +0100758 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100759 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
760 if (wanted & TX_CON_CLO_SET)
761 txn->flags |= TX_CON_CLO_SET;
762 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100763 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +0100764 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100765 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100766
767 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
768 return;
769
770 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
771 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100772 hdr_val = "Connection: close";
773 hdr_len = 17;
774 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
775 hdr_val = "Proxy-Connection: close";
776 hdr_len = 23;
777 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100778 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100779 }
780
781 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
782 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100783 hdr_val = "Connection: keep-alive";
784 hdr_len = 22;
785 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
786 hdr_val = "Proxy-Connection: keep-alive";
787 hdr_len = 28;
788 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100789 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100790 }
791 return;
Willy Tarreau5b154472009-12-21 20:11:07 +0100792}
793
Willy Tarreau87b09662015-04-03 00:22:06 +0200794void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200795{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200796 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200797 int tmp = TX_CON_WANT_KAL;
798
Christopher Fauletf2824e62018-10-01 12:12:37 +0200799 if (IS_HTX_STRM(s))
Christopher Faulet0f226952018-10-22 09:29:56 +0200800 return htx_adjust_conn_mode(s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200801
Christopher Faulet315b39c2018-09-21 16:26:19 +0200802 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
803 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
804 tmp = TX_CON_WANT_TUN;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200805
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200806 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Faulet315b39c2018-09-21 16:26:19 +0200807 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200808 tmp = TX_CON_WANT_SCL;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200809
Christopher Faulet315b39c2018-09-21 16:26:19 +0200810 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
811 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200812 tmp = TX_CON_WANT_CLO;
813
814 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
815 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
816
817 if (!(txn->flags & TX_HDR_CONN_PRS) &&
818 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
819 /* parse the Connection header and possibly clean it */
820 int to_del = 0;
821 if ((msg->flags & HTTP_MSGF_VER_11) ||
822 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200823 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200824 to_del |= 2; /* remove "keep-alive" */
825 if (!(msg->flags & HTTP_MSGF_VER_11))
826 to_del |= 1; /* remove "close" */
827 http_parse_connection_header(txn, msg, to_del);
828 }
829
830 /* check if client or config asks for explicit close in KAL/SCL */
831 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
832 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
833 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
834 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
835 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200836 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200837 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
838}
William Lallemand82fe75c2012-10-23 10:25:10 +0200839
Willy Tarreaud787e662009-07-07 10:14:51 +0200840/* This stream analyser waits for a complete HTTP request. It returns 1 if the
841 * processing can continue on next analysers, or zero if it either needs more
842 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100843 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200844 * when it has nothing left to do, and may remove any analyser when it wants to
845 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100846 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200847int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100848{
Willy Tarreau59234e92008-11-30 23:51:27 +0100849 /*
850 * We will parse the partial (or complete) lines.
851 * We will check the request syntax, and also join multi-line
852 * headers. An index of all the lines will be elaborated while
853 * parsing.
854 *
855 * For the parsing, we use a 28 states FSM.
856 *
857 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200858 * ci_head(req) = beginning of request
859 * ci_head(req) + msg->eoh = end of processed headers / start of current one
860 * ci_tail(req) = end of input data
861 * msg->eol = end of current header or line (LF or CRLF)
862 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200863 *
864 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200865 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200866 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
867 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100868 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100869
Willy Tarreau59234e92008-11-30 23:51:27 +0100870 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200871 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200872 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100873 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200874 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100875
Christopher Faulete0768eb2018-10-03 16:38:02 +0200876 if (IS_HTX_STRM(s))
877 return htx_wait_for_request(s, req, an_bit);
878
Christopher Faulet45073512018-07-20 10:16:29 +0200879 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 +0100880 now_ms, __FUNCTION__,
881 s,
882 req,
883 req->rex, req->wex,
884 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200885 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100886 req->analysers);
887
Willy Tarreau52a0c602009-08-16 22:45:38 +0200888 /* we're speaking HTTP here, so let's speak HTTP to the client */
889 s->srv_error = http_return_srv_error;
890
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300891 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200892 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300893 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
894
Willy Tarreau83e3af02009-12-28 17:39:57 +0100895 /* There's a protected area at the end of the buffer for rewriting
896 * purposes. We don't want to start to parse the request if the
897 * protected area is affected, because we may have to move processed
898 * data later, which is much more complicated.
899 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200900 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200901 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100902 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200903 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100904 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100905 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200906 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200907 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100908 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100909 return 0;
910 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200911 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200912 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200913 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100914 }
915
Willy Tarreauf37954d2018-06-15 18:31:02 +0200916 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100917 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100918 }
919
Willy Tarreau59234e92008-11-30 23:51:27 +0100920 /* 1: we might have to print this header in debug mode */
921 if (unlikely((global.mode & MODE_DEBUG) &&
922 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200923 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100924 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100925
Willy Tarreauf37954d2018-06-15 18:31:02 +0200926 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200927 /* this is a bit complex : in case of error on the request line,
928 * we know that rq.l is still zero, so we display only the part
929 * up to the end of the line (truncated by debug_hdr).
930 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200931 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100932 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100933
Willy Tarreau59234e92008-11-30 23:51:27 +0100934 sol += hdr_idx_first_pos(&txn->hdr_idx);
935 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100936
Willy Tarreau59234e92008-11-30 23:51:27 +0100937 while (cur_idx) {
938 eol = sol + txn->hdr_idx.v[cur_idx].len;
939 debug_hdr("clihdr", s, sol, eol);
940 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
941 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100942 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100943 }
944
Willy Tarreau58f10d72006-12-04 02:26:12 +0100945
Willy Tarreau59234e92008-11-30 23:51:27 +0100946 /*
947 * Now we quickly check if we have found a full valid request.
948 * If not so, we check the FD and buffer states before leaving.
949 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100950 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100951 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200952 * on a keep-alive stream, if we encounter and error, close, t/o,
953 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100954 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200955 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200956 * Last, we may increase some tracked counters' http request errors on
957 * the cases that are deliberately the client's fault. For instance,
958 * a timeout or connection reset is not counted as an error. However
959 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100960 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100961
Willy Tarreau655dce92009-11-08 13:10:58 +0100962 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100963 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100964 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100965 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100966 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200967 stream_inc_http_req_ctr(s);
968 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200969 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100970 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100971 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100972
Willy Tarreau59234e92008-11-30 23:51:27 +0100973 /* 1: Since we are in header mode, if there's no space
974 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200975 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100976 * must terminate it now.
977 */
Willy Tarreau23752332018-06-15 14:54:53 +0200978 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100979 /* FIXME: check if URI is set and return Status
980 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100981 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200982 stream_inc_http_req_ctr(s);
983 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200984 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +0200985 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +0200986 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +0100987 goto return_bad_req;
988 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100989
Willy Tarreau59234e92008-11-30 23:51:27 +0100990 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200991 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200992 if (!(s->flags & SF_ERR_MASK))
993 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100994
Willy Tarreaufcffa692010-01-10 14:21:19 +0100995 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100996 goto failed_keep_alive;
997
Willy Tarreau0f228a02015-05-01 15:37:53 +0200998 if (sess->fe->options & PR_O_IGNORE_PRB)
999 goto failed_keep_alive;
1000
Willy Tarreau59234e92008-11-30 23:51:27 +01001001 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001002 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001003 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001004 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001005 }
1006
Willy Tarreaudc979f22012-12-04 10:39:01 +01001007 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001008 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001009 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001010 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001011 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001012 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001013 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001014 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001015 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001016 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001017
Willy Tarreaue7dff022015-04-03 01:14:29 +02001018 if (!(s->flags & SF_FINST_MASK))
1019 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001020 return 0;
1021 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001022
Willy Tarreau59234e92008-11-30 23:51:27 +01001023 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001024 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001025 if (!(s->flags & SF_ERR_MASK))
1026 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001027
Willy Tarreaufcffa692010-01-10 14:21:19 +01001028 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001029 goto failed_keep_alive;
1030
Willy Tarreau0f228a02015-05-01 15:37:53 +02001031 if (sess->fe->options & PR_O_IGNORE_PRB)
1032 goto failed_keep_alive;
1033
Willy Tarreau59234e92008-11-30 23:51:27 +01001034 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001035 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001036 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001037 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001038 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001039 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001040 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001041 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001042 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001043 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001044
Willy Tarreau87b09662015-04-03 00:22:06 +02001045 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001046 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001047 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001048 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001049 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001050
Willy Tarreaue7dff022015-04-03 01:14:29 +02001051 if (!(s->flags & SF_FINST_MASK))
1052 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001053 return 0;
1054 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001055
Willy Tarreau59234e92008-11-30 23:51:27 +01001056 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001057 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001058 if (!(s->flags & SF_ERR_MASK))
1059 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001060
Willy Tarreaufcffa692010-01-10 14:21:19 +01001061 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001062 goto failed_keep_alive;
1063
Willy Tarreau0f228a02015-05-01 15:37:53 +02001064 if (sess->fe->options & PR_O_IGNORE_PRB)
1065 goto failed_keep_alive;
1066
Willy Tarreau4076a152009-04-02 15:18:36 +02001067 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001068 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001069 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001070 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001071 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001072 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001073 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001074 stream_inc_http_err_ctr(s);
1075 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001076 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001077 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001078 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001079 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001080
Willy Tarreaue7dff022015-04-03 01:14:29 +02001081 if (!(s->flags & SF_FINST_MASK))
1082 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001083 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001084 }
1085
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001086 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001087 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001088 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +01001089
1090 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req)) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001091 /* We need more data, we have to re-enable quick-ack in case we
1092 * previously disabled it, otherwise we might cause the client
1093 * to delay next data.
1094 */
Willy Tarreau1a18b542018-12-11 16:37:42 +01001095 conn_set_quickack(objt_conn(sess->origin), 1);
Willy Tarreau5e205522011-12-17 16:34:27 +01001096 }
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001097
Willy Tarreaufcffa692010-01-10 14:21:19 +01001098 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1099 /* If the client starts to talk, let's fall back to
1100 * request timeout processing.
1101 */
1102 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001103 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001104 }
1105
Willy Tarreau59234e92008-11-30 23:51:27 +01001106 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001107 if (!tick_isset(req->analyse_exp)) {
1108 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1109 (txn->flags & TX_WAIT_NEXT_RQ) &&
1110 tick_isset(s->be->timeout.httpka))
1111 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1112 else
1113 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1114 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001115
Willy Tarreau59234e92008-11-30 23:51:27 +01001116 /* we're not ready yet */
1117 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001118
1119 failed_keep_alive:
1120 /* Here we process low-level errors for keep-alive requests. In
1121 * short, if the request is not the first one and it experiences
1122 * a timeout, read error or shutdown, we just silently close so
1123 * that the client can try again.
1124 */
1125 txn->status = 0;
1126 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001127 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001128 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001129 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001130 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001131 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001132 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001133 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001134
Willy Tarreaud787e662009-07-07 10:14:51 +02001135 /* OK now we have a complete HTTP request with indexed headers. Let's
1136 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001137 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001138 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001139 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001140 * byte after the last LF. msg->sov points to the first byte of data.
1141 * msg->eol cannot be trusted because it may have been left uninitialized
1142 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001143 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001144
Willy Tarreau87b09662015-04-03 00:22:06 +02001145 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001146 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001147
Willy Tarreaub16a5742010-01-10 14:46:16 +01001148 if (txn->flags & TX_WAIT_NEXT_RQ) {
1149 /* kill the pending keep-alive timeout */
1150 txn->flags &= ~TX_WAIT_NEXT_RQ;
1151 req->analyse_exp = TICK_ETERNITY;
1152 }
1153
1154
Willy Tarreaud787e662009-07-07 10:14:51 +02001155 /* Maybe we found in invalid header name while we were configured not
1156 * to block on that, so we have to capture it now.
1157 */
1158 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001159 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001160
Willy Tarreau59234e92008-11-30 23:51:27 +01001161 /*
1162 * 1: identify the method
1163 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001164 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001165
1166 /* we can make use of server redirect on GET and HEAD */
1167 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001168 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001169 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001170 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001171 /* PRI is reserved for the HTTP/2 preface */
1172 msg->err_pos = 0;
1173 goto return_bad_req;
1174 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001175
Willy Tarreau59234e92008-11-30 23:51:27 +01001176 /*
1177 * 2: check if the URI matches the monitor_uri.
1178 * We have to do this for every request which gets in, because
1179 * the monitor-uri is defined by the frontend.
1180 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001181 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1182 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001183 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001184 sess->fe->monitor_uri,
1185 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001186 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001187 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001188 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001189 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001190
Willy Tarreaue7dff022015-04-03 01:14:29 +02001191 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001192 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001193
Willy Tarreau59234e92008-11-30 23:51:27 +01001194 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001195 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001196 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001197
Willy Tarreau59234e92008-11-30 23:51:27 +01001198 ret = acl_pass(ret);
1199 if (cond->pol == ACL_COND_UNLESS)
1200 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001201
Willy Tarreau59234e92008-11-30 23:51:27 +01001202 if (ret) {
1203 /* we fail this request, let's return 503 service unavail */
1204 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001205 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001206 if (!(s->flags & SF_ERR_MASK))
1207 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001208 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001209 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001210 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001211
Joseph Herlant5ba80252018-11-15 09:25:36 -08001212 /* nothing to fail, let's reply normally */
Willy Tarreau59234e92008-11-30 23:51:27 +01001213 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001214 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001215 if (!(s->flags & SF_ERR_MASK))
1216 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001217 goto return_prx_cond;
1218 }
1219
1220 /*
1221 * 3: Maybe we have to copy the original REQURI for the logs ?
1222 * Note: we cannot log anymore if the request has been
1223 * classified as invalid.
1224 */
1225 if (unlikely(s->logs.logwait & LW_REQ)) {
1226 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001227 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001228 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001229
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001230 if (urilen >= global.tune.requri_len )
1231 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001232 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001233 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001234
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001235 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001236 s->do_log(s);
1237 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001238 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001239 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001240 }
Willy Tarreau06619262006-12-17 08:37:22 +01001241
Willy Tarreau91852eb2015-05-01 13:26:00 +02001242 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1243 * exactly one digit "." one digit. This check may be disabled using
1244 * option accept-invalid-http-request.
1245 */
1246 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1247 if (msg->sl.rq.v_l != 8) {
1248 msg->err_pos = msg->sl.rq.v;
1249 goto return_bad_req;
1250 }
1251
Willy Tarreauf37954d2018-06-15 18:31:02 +02001252 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1253 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1254 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1255 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001256 msg->err_pos = msg->sl.rq.v + 4;
1257 goto return_bad_req;
1258 }
1259 }
Willy Tarreau13317662015-05-01 13:47:08 +02001260 else {
1261 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1262 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1263 goto return_bad_req;
1264 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001265
Willy Tarreau5b154472009-12-21 20:11:07 +01001266 /* ... and check if the request is HTTP/1.1 or above */
1267 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001268 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
1269 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
1270 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001271 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001272
1273 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001274 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01001275
Willy Tarreau88d349d2010-01-25 12:15:43 +01001276 /* if the frontend has "option http-use-proxy-header", we'll check if
1277 * we have what looks like a proxied connection instead of a connection,
1278 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1279 * Note that this is *not* RFC-compliant, however browsers and proxies
1280 * happen to do that despite being non-standard :-(
1281 * We consider that a request not beginning with either '/' or '*' is
1282 * a proxied connection, which covers both "scheme://location" and
1283 * CONNECT ip:port.
1284 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001285 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001286 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01001287 txn->flags |= TX_USE_PX_CONN;
1288
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001289 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001290 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001291
Willy Tarreau59234e92008-11-30 23:51:27 +01001292 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001293 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02001294 http_capture_headers(ci_head(req), &txn->hdr_idx,
1295 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001296
Willy Tarreau557f1992015-05-01 10:05:17 +02001297 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1298 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001299 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001300 * The length of a message body is determined by one of the following
1301 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001302 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001303 * 1. Any response to a HEAD request and any response with a 1xx
1304 * (Informational), 204 (No Content), or 304 (Not Modified) status
1305 * code is always terminated by the first empty line after the
1306 * header fields, regardless of the header fields present in the
1307 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001308 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001309 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1310 * the connection will become a tunnel immediately after the empty
1311 * line that concludes the header fields. A client MUST ignore any
1312 * Content-Length or Transfer-Encoding header fields received in
1313 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001314 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001315 * 3. If a Transfer-Encoding header field is present and the chunked
1316 * transfer coding (Section 4.1) is the final encoding, the message
1317 * body length is determined by reading and decoding the chunked
1318 * data until the transfer coding indicates the data is complete.
1319 *
1320 * If a Transfer-Encoding header field is present in a response and
1321 * the chunked transfer coding is not the final encoding, the
1322 * message body length is determined by reading the connection until
1323 * it is closed by the server. If a Transfer-Encoding header field
1324 * is present in a request and the chunked transfer coding is not
1325 * the final encoding, the message body length cannot be determined
1326 * reliably; the server MUST respond with the 400 (Bad Request)
1327 * status code and then close the connection.
1328 *
1329 * If a message is received with both a Transfer-Encoding and a
1330 * Content-Length header field, the Transfer-Encoding overrides the
1331 * Content-Length. Such a message might indicate an attempt to
1332 * perform request smuggling (Section 9.5) or response splitting
1333 * (Section 9.4) and ought to be handled as an error. A sender MUST
1334 * remove the received Content-Length field prior to forwarding such
1335 * a message downstream.
1336 *
1337 * 4. If a message is received without Transfer-Encoding and with
1338 * either multiple Content-Length header fields having differing
1339 * field-values or a single Content-Length header field having an
1340 * invalid value, then the message framing is invalid and the
1341 * recipient MUST treat it as an unrecoverable error. If this is a
1342 * request message, the server MUST respond with a 400 (Bad Request)
1343 * status code and then close the connection. If this is a response
1344 * message received by a proxy, the proxy MUST close the connection
1345 * to the server, discard the received response, and send a 502 (Bad
1346 * Gateway) response to the client. If this is a response message
1347 * received by a user agent, the user agent MUST close the
1348 * connection to the server and discard the received response.
1349 *
1350 * 5. If a valid Content-Length header field is present without
1351 * Transfer-Encoding, its decimal value defines the expected message
1352 * body length in octets. If the sender closes the connection or
1353 * the recipient times out before the indicated number of octets are
1354 * received, the recipient MUST consider the message to be
1355 * incomplete and close the connection.
1356 *
1357 * 6. If this is a request message and none of the above are true, then
1358 * the message body length is zero (no message body is present).
1359 *
1360 * 7. Otherwise, this is a response message without a declared message
1361 * body length, so the message body length is determined by the
1362 * number of octets received prior to the server closing the
1363 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001364 */
1365
Willy Tarreau32b47f42009-10-18 20:55:02 +02001366 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001367 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001368 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001369 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001370 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001371 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001372 /* chunked not last, return badreq */
1373 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001374 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001375 }
1376
Willy Tarreau1c913912015-04-30 10:57:51 +02001377 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001378 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001379 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001380 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001381 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1382 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001383 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001384 signed long long cl;
1385
Willy Tarreauad14f752011-09-02 20:33:27 +02001386 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001387 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001388 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001389 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001390
Willy Tarreauad14f752011-09-02 20:33:27 +02001391 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001392 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001393 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001394 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001395
Willy Tarreauad14f752011-09-02 20:33:27 +02001396 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001397 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001398 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001399 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001400
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001401 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001402 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001403 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001404 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001405
Christopher Fauletbe821b92017-03-30 11:21:53 +02001406 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001407 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001408 }
1409
Willy Tarreau34dfc602015-05-01 10:09:49 +02001410 /* even bodyless requests have a known length */
1411 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001412
Willy Tarreau179085c2014-04-28 16:48:56 +02001413 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1414 * only change if both the request and the config reference something else.
1415 * Option httpclose by itself sets tunnel mode where headers are mangled.
1416 * However, if another mode is set, it will affect it (eg: server-close/
1417 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1418 * if FE and BE have the same settings (common). The method consists in
1419 * checking if options changed between the two calls (implying that either
1420 * one is non-null, or one of them is non-null and we are there for the first
1421 * time.
1422 */
1423 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001424 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001425 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001426
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001427 /* we may have to wait for the request's body */
1428 if ((s->be->options & PR_O_WREQ_BODY) &&
1429 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1430 req->analysers |= AN_REQ_HTTP_BODY;
1431
Willy Tarreau83ece462017-12-21 15:13:09 +01001432 /*
1433 * RFC7234#4:
1434 * A cache MUST write through requests with methods
1435 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1436 * the origin server; i.e., a cache is not allowed
1437 * to generate a reply to such a request before
1438 * having forwarded the request and having received
1439 * a corresponding response.
1440 *
1441 * RFC7231#4.2.1:
1442 * Of the request methods defined by this
1443 * specification, the GET, HEAD, OPTIONS, and TRACE
1444 * methods are defined to be safe.
1445 */
1446 if (likely(txn->meth == HTTP_METH_GET ||
1447 txn->meth == HTTP_METH_HEAD ||
1448 txn->meth == HTTP_METH_OPTIONS ||
1449 txn->meth == HTTP_METH_TRACE))
1450 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1451
Willy Tarreaud787e662009-07-07 10:14:51 +02001452 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001453 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001454 req->analyse_exp = TICK_ETERNITY;
1455 return 1;
1456
1457 return_bad_req:
1458 /* We centralize bad requests processing here */
1459 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1460 /* we detected a parsing error. We want to archive this request
1461 * in the dedicated proxy area for later troubleshooting.
1462 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001463 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001464 }
1465
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001466 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001467 txn->req.msg_state = HTTP_MSG_ERROR;
1468 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001469 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001470
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001471 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001472 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001473 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001474
1475 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001476 if (!(s->flags & SF_ERR_MASK))
1477 s->flags |= SF_ERR_PRXCOND;
1478 if (!(s->flags & SF_FINST_MASK))
1479 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001480
Christopher Faulet0184ea72017-01-05 14:06:34 +01001481 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001482 req->analyse_exp = TICK_ETERNITY;
1483 return 0;
1484}
1485
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001486
Willy Tarreau347a35d2013-11-22 17:51:09 +01001487/* This function prepares an applet to handle the stats. It can deal with the
1488 * "100-continue" expectation, check that admin rules are met for POST requests,
1489 * and program a response message if something was unexpected. It cannot fail
1490 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001491 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001492 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001493 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001494 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001495int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001496{
1497 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001498 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001499 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001500 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001501 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001502 struct uri_auth *uri_auth = s->be->uri_auth;
1503 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001504 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001505
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001506 appctx = si_appctx(si);
1507 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1508 appctx->st1 = appctx->st2 = 0;
1509 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1510 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001511 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001512 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001513
Willy Tarreauf37954d2018-06-15 18:31:02 +02001514 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001515 lookup = uri + uri_auth->uri_len;
1516
1517 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1518 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001519 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001520 break;
1521 }
1522 }
1523
1524 if (uri_auth->refresh) {
1525 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1526 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001527 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001528 break;
1529 }
1530 }
1531 }
1532
1533 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1534 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001535 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001536 break;
1537 }
1538 }
1539
Willy Tarreau1e62df92016-01-11 18:57:53 +01001540 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1541 if (memcmp(h, ";typed", 6) == 0) {
1542 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1543 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1544 break;
1545 }
1546 }
1547
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001548 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1549 if (memcmp(h, ";st=", 4) == 0) {
1550 int i;
1551 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001552 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001553 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1554 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001555 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001556 break;
1557 }
1558 }
1559 break;
1560 }
1561 }
1562
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001563 appctx->ctx.stats.scope_str = 0;
1564 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001565 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1566 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1567 int itx = 0;
1568 const char *h2;
1569 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1570 const char *err;
1571
1572 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1573 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001574 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001575 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1576 itx++;
1577 h++;
1578 }
1579
1580 if (itx > STAT_SCOPE_TXT_MAXLEN)
1581 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001582 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001583
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001584 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001585 memcpy(scope_txt, h2, itx);
1586 scope_txt[itx] = '\0';
1587 err = invalid_char(scope_txt);
1588 if (err) {
1589 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001590 appctx->ctx.stats.scope_str = 0;
1591 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001592 }
1593 break;
1594 }
1595 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001596
1597 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001598 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001599 int ret = 1;
1600
1601 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001602 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001603 ret = acl_pass(ret);
1604 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1605 ret = !ret;
1606 }
1607
1608 if (ret) {
1609 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001610 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001611 break;
1612 }
1613 }
1614
1615 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001616 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001617 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001618 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001619 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1620 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001621 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001622 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001623 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001624 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1625 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001626 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001627 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001628 else {
1629 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001630 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001631 }
1632
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001633 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001634 return 1;
1635}
1636
Willy Tarreau87b09662015-04-03 00:22:06 +02001637int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001638 const char* name, unsigned int name_len,
1639 const char *str, struct my_regex *re,
1640 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001641{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001642 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001643 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02001644 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001645 int (*http_find_hdr_func)(const char *name, int len, char *sol,
1646 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02001647 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001648
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001649 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06001650
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001651 /* Choose the header browsing function. */
1652 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001653 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001654 http_find_hdr_func = http_find_header2;
1655 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001656 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001657 http_find_hdr_func = http_find_full_header2;
1658 break;
1659 default: /* impossible */
1660 return -1;
1661 }
1662
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001663 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
1664 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02001665 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001666 char *val = ctx.line + ctx.val;
1667 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06001668
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001669 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
1670 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06001671
Willy Tarreau6e27be12018-08-22 04:46:47 +02001672 len = exp_replace(output->area, output->size, val, str, pmatch);
1673 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06001674 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06001675
Willy Tarreau6e27be12018-08-22 04:46:47 +02001676 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06001677
1678 hdr->len += delta;
1679 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001680
1681 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001682 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06001683 }
1684
1685 return 0;
1686}
1687
Willy Tarreau87b09662015-04-03 00:22:06 +02001688static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001689 const char* name, unsigned int name_len,
1690 struct list *fmt, struct my_regex *re,
1691 int action)
1692{
Willy Tarreau83061a82018-07-13 11:56:34 +02001693 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001694 int ret = -1;
1695
1696 replace = alloc_trash_chunk();
1697 if (!replace)
1698 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001699
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001700 replace->data = build_logline(s, replace->area, replace->size, fmt);
1701 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001702 goto leave;
1703
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001704 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1705 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001706
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001707 leave:
1708 free_trash_chunk(replace);
1709 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001710}
1711
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001712/*
1713 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
1714 * built according to <fmt> log line format.
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001715 * If <early_hints> is NULL, it is allocated and the HTTP 103 response first
1716 * line is inserted before the header. If an error occurred <early_hints> is
1717 * released and NULL is returned. On success the updated buffer is returned.
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001718 */
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001719static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints,
1720 const char* name, unsigned int name_len,
1721 struct list *fmt)
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001722{
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001723 if (!early_hints) {
1724 early_hints = alloc_trash_chunk();
1725 if (!early_hints)
1726 goto fail;
1727 if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len))
1728 goto fail;
1729 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001730
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001731 if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2))
1732 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001733
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001734 early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt);
1735 if (!chunk_memcat(early_hints, "\r\n", 2))
1736 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001737
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001738 return early_hints;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001739
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001740 fail:
1741 free_trash_chunk(early_hints);
1742 return NULL;
1743}
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001744
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001745/* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the
1746 * response is added. If an error occurred or if another response was already
1747 * sent, this function does nothing.
1748 */
1749static void http_send_early_hints(struct stream *s, struct buffer *early_hints)
1750{
1751 struct channel *chn = s->txn->rsp.chn;
1752 char *cur_ptr = ci_head(chn);
1753 int ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001754
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001755 /* If a response was already sent, skip early hints */
1756 if (s->txn->status > 0)
1757 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001758
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001759 if (!chunk_memcat(early_hints, "\r\n", 2))
1760 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001761
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001762 ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints));
1763 c_adv(chn, ret);
1764 chn->total += ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001765}
1766
Willy Tarreau87b09662015-04-03 00:22:06 +02001767/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001768 * transaction <txn>. Returns the verdict of the first rule that prevents
1769 * further processing of the request (auth, deny, ...), and defaults to
1770 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1771 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001772 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1773 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1774 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001775 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001776enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001777http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001778{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001779 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001780 struct http_txn *txn = s->txn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001781 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001782 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001783 const char *auth_realm;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001784 struct buffer *early_hints = NULL;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001785 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001786 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001787 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001788
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001789 /* If "the current_rule_list" match the executed rule list, we are in
1790 * resume condition. If a resume is needed it is always in the action
1791 * and never in the ACL or converters. In this case, we initialise the
1792 * current rule, and go to the action execution point.
1793 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001794 if (s->current_rule) {
1795 rule = s->current_rule;
1796 s->current_rule = NULL;
1797 if (s->current_rule_list == rules)
1798 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001799 }
1800 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001801
Willy Tarreauff011f22011-01-06 17:51:27 +01001802 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001803
Willy Tarreau96257ec2012-12-27 10:46:37 +01001804 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001805 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001806 int ret;
1807
Willy Tarreau192252e2015-04-04 01:47:55 +02001808 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001809 ret = acl_pass(ret);
1810
Willy Tarreauff011f22011-01-06 17:51:27 +01001811 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001812 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001813
1814 if (!ret) /* condition not matched */
1815 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001816 }
1817
Willy Tarreauacc98002015-09-27 23:34:39 +02001818 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001819resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001820 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001821 case ACT_ACTION_ALLOW:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001822 rule_ret = HTTP_RULE_RES_STOP;
1823 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001824
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001825 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001826 if (deny_status)
1827 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001828 rule_ret = HTTP_RULE_RES_DENY;
1829 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001830
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001831 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001832 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001833 if (deny_status)
1834 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001835 rule_ret = HTTP_RULE_RES_DENY;
1836 goto end;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001837
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001838 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001839 /* Be sure to send any pending HTTP 103 response first */
1840 if (early_hints) {
1841 http_send_early_hints(s, early_hints);
1842 free_trash_chunk(early_hints);
1843 early_hints = NULL;
1844 }
Willy Tarreauae3c0102014-04-28 23:22:08 +02001845 /* Auth might be performed on regular http-req rules as well as on stats */
1846 auth_realm = rule->arg.auth.realm;
1847 if (!auth_realm) {
1848 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1849 auth_realm = STATS_DEFAULT_REALM;
1850 else
1851 auth_realm = px->id;
1852 }
1853 /* send 401/407 depending on whether we use a proxy or not. We still
1854 * count one error, because normal browsing won't significantly
1855 * increase the counter but brute force attempts will.
1856 */
1857 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1858 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001859 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001860 stream_inc_http_err_ctr(s);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001861 rule_ret = HTTP_RULE_RES_ABRT;
1862 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001863
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001864 case ACT_HTTP_REDIR:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001865 /* Be sure to send any pending HTTP 103 response first */
1866 if (early_hints) {
1867 http_send_early_hints(s, early_hints);
1868 free_trash_chunk(early_hints);
1869 early_hints = NULL;
1870 }
Christopher Fauletea827bd2018-11-15 15:34:11 +01001871 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau0b748332014-04-29 00:13:29 +02001872 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Fauletea827bd2018-11-15 15:34:11 +01001873 rule_ret = HTTP_RULE_RES_BADREQ;
1874 goto end;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001875
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001876 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001877 s->task->nice = rule->arg.nice;
1878 break;
1879
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001880 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001881 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001882 break;
1883
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001884 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001885 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Willy Tarreau51347ed2013-06-11 19:34:13 +02001886 break;
1887
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001888 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001889 s->logs.level = rule->arg.loglevel;
1890 break;
1891
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001892 case ACT_HTTP_REPLACE_HDR:
1893 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001894 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1895 rule->arg.hdr_add.name_len,
1896 &rule->arg.hdr_add.fmt,
Christopher Fauletea827bd2018-11-15 15:34:11 +01001897 &rule->arg.hdr_add.re, rule->action)) {
1898 rule_ret = HTTP_RULE_RES_BADREQ;
1899 goto end;
1900 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001901 break;
1902
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001903 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001904 ctx.idx = 0;
1905 /* remove all occurrences of the header */
1906 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001907 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001908 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001909 }
Willy Tarreau85603282015-01-21 20:39:27 +01001910 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001911
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001912 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001913 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001914 /* The scope of the trash buffer must be limited to this function. The
1915 * build_logline() function can execute a lot of other function which
1916 * can use the trash buffer. So for limiting the scope of this global
1917 * buffer, we build first the header value using build_logline, and
1918 * after we store the header name.
1919 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001920 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001921
1922 replace = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001923 if (!replace) {
1924 rule_ret = HTTP_RULE_RES_BADREQ;
1925 goto end;
1926 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001927
Thierry Fournier4b788f72016-06-01 13:35:36 +02001928 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001929 len += build_logline(s, replace->area + len,
1930 replace->size - len,
1931 &rule->arg.hdr_add.fmt);
1932 memcpy(replace->area, rule->arg.hdr_add.name,
1933 rule->arg.hdr_add.name_len);
1934 replace->area[rule->arg.hdr_add.name_len] = ':';
1935 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1936 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001937
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001938 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001939 /* remove all occurrences of the header */
1940 ctx.idx = 0;
1941 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001942 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001943 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1944 }
1945 }
1946
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001947 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001948 static unsigned char rate_limit = 0;
1949
1950 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001951 replace->area[rule->arg.hdr_add.name_len] = 0;
1952 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,
1953 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001954 }
1955
1956 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1957 if (sess->fe != s->be)
1958 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1959 if (sess->listener->counters)
1960 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1961 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001962
1963 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001964 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001965 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001966
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001967 case ACT_HTTP_DEL_ACL:
1968 case ACT_HTTP_DEL_MAP: {
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();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001979 if (!key) {
1980 rule_ret = HTTP_RULE_RES_BADREQ;
1981 goto end;
1982 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001983
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001984 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001985 key->data = build_logline(s, key->area, key->size,
1986 &rule->arg.map.key);
1987 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001988
1989 /* perform update */
1990 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001991 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001992 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001993 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001994
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001995 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001996 break;
1997 }
1998
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001999 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002000 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002001 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002002
2003 /* collect reference */
2004 ref = pat_ref_lookup(rule->arg.map.ref);
2005 if (!ref)
2006 continue;
2007
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002008 /* allocate key */
2009 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002010 if (!key) {
2011 rule_ret = HTTP_RULE_RES_BADREQ;
2012 goto end;
2013 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002014
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002015 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002016 key->data = build_logline(s, key->area, key->size,
2017 &rule->arg.map.key);
2018 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002019
2020 /* perform update */
2021 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002022 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002023 if (pat_ref_find_elt(ref, key->area) == NULL)
2024 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002025 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002026
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002027 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002028 break;
2029 }
2030
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002031 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002032 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002033 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002034
2035 /* collect reference */
2036 ref = pat_ref_lookup(rule->arg.map.ref);
2037 if (!ref)
2038 continue;
2039
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002040 /* allocate key */
2041 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01002042 if (!key) {
2043 rule_ret = HTTP_RULE_RES_BADREQ;
2044 goto end;
2045 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002046
2047 /* allocate value */
2048 value = alloc_trash_chunk();
2049 if (!value) {
2050 free_trash_chunk(key);
Christopher Fauletea827bd2018-11-15 15:34:11 +01002051 rule_ret = HTTP_RULE_RES_BADREQ;
2052 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002053 }
2054
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002055 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002056 key->data = build_logline(s, key->area, key->size,
2057 &rule->arg.map.key);
2058 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002059
2060 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002061 value->data = build_logline(s, value->area,
2062 value->size,
2063 &rule->arg.map.value);
2064 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002065
2066 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002067 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002068 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002069 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002070 else
2071 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002072 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002073
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002074 free_trash_chunk(key);
2075 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002076 break;
2077 }
William Lallemand73025dd2014-04-24 14:38:37 +02002078
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002079 case ACT_HTTP_EARLY_HINT:
2080 if (!(txn->req.flags & HTTP_MSGF_VER_11))
2081 break;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002082 early_hints = http_apply_early_hint_rule(s, early_hints,
2083 rule->arg.early_hint.name,
2084 rule->arg.early_hint.name_len,
2085 &rule->arg.early_hint.fmt);
2086 if (!early_hints) {
Christopher Fauletea827bd2018-11-15 15:34:11 +01002087 rule_ret = HTTP_RULE_RES_DONE;
2088 goto end;
2089 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002090 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002091 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002092 if ((s->req.flags & CF_READ_ERROR) ||
2093 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2094 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2095 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002096 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002097
Willy Tarreauacc98002015-09-27 23:34:39 +02002098 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002099 case ACT_RET_ERR:
2100 case ACT_RET_CONT:
2101 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002102 case ACT_RET_STOP:
Christopher Fauletea827bd2018-11-15 15:34:11 +01002103 rule_ret = HTTP_RULE_RES_DONE;
2104 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002105 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002106 s->current_rule = rule;
Christopher Fauletea827bd2018-11-15 15:34:11 +01002107 rule_ret = HTTP_RULE_RES_YIELD;
2108 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002109 }
William Lallemand73025dd2014-04-24 14:38:37 +02002110 break;
2111
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002112 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002113 /* Note: only the first valid tracking parameter of each
2114 * applies.
2115 */
2116
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002117 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002118 struct stktable *t;
2119 struct stksess *ts;
2120 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002121 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002122
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002123 t = rule->arg.trk_ctr.table.t;
2124 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 +02002125
2126 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002127 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002128
2129 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002130 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2131 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2132 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002133 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002134
Emeric Brun819fc6f2017-06-13 19:37:32 +02002135 if (ptr1)
2136 stktable_data_cast(ptr1, http_req_cnt)++;
2137
2138 if (ptr2)
2139 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2140 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2141
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002142 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002143
2144 /* If data was modified, we need to touch to re-schedule sync */
2145 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002146 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002147
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002148 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002149 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002150 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002151 }
2152 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002153 break;
2154
Joseph Herlant5ba80252018-11-15 09:25:36 -08002155 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002156 default:
2157 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002158 }
2159 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002160
Christopher Fauletea827bd2018-11-15 15:34:11 +01002161 end:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01002162 if (early_hints) {
2163 http_send_early_hints(s, early_hints);
2164 free_trash_chunk(early_hints);
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01002165 }
2166
Willy Tarreau96257ec2012-12-27 10:46:37 +01002167 /* we reached the end of the rules, nothing to report */
Christopher Fauletea827bd2018-11-15 15:34:11 +01002168 return rule_ret;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002169}
2170
Willy Tarreau71241ab2012-12-27 11:30:54 +01002171
Willy Tarreau51d861a2015-05-22 17:30:48 +02002172/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2173 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2174 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2175 * is returned, the process can continue the evaluation of next rule list. If
2176 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2177 * is returned, it means the operation could not be processed and a server error
2178 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2179 * deny rule. If *YIELD is returned, the caller must call again the function
2180 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002181 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002182enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002183http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002184{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002185 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002186 struct http_txn *txn = s->txn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002187 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002188 struct hdr_ctx ctx;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002189 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02002190 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002191
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002192 /* If "the current_rule_list" match the executed rule list, we are in
2193 * resume condition. If a resume is needed it is always in the action
2194 * and never in the ACL or converters. In this case, we initialise the
2195 * current rule, and go to the action execution point.
2196 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002197 if (s->current_rule) {
2198 rule = s->current_rule;
2199 s->current_rule = NULL;
2200 if (s->current_rule_list == rules)
2201 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002202 }
2203 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002204
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002205 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002206
2207 /* check optional condition */
2208 if (rule->cond) {
2209 int ret;
2210
Willy Tarreau192252e2015-04-04 01:47:55 +02002211 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002212 ret = acl_pass(ret);
2213
2214 if (rule->cond->pol == ACL_COND_UNLESS)
2215 ret = !ret;
2216
2217 if (!ret) /* condition not matched */
2218 continue;
2219 }
2220
Willy Tarreauacc98002015-09-27 23:34:39 +02002221 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002222resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002223 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002224 case ACT_ACTION_ALLOW:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002225 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
2226 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002227
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002228 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002229 txn->flags |= TX_SVDENY;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002230 rule_ret = HTTP_RULE_RES_STOP;
2231 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002232
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002233 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002234 s->task->nice = rule->arg.nice;
2235 break;
2236
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002237 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01002238 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002239 break;
2240
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002241 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01002242 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Willy Tarreau51347ed2013-06-11 19:34:13 +02002243 break;
2244
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002245 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002246 s->logs.level = rule->arg.loglevel;
2247 break;
2248
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002249 case ACT_HTTP_REPLACE_HDR:
2250 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002251 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2252 rule->arg.hdr_add.name_len,
2253 &rule->arg.hdr_add.fmt,
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002254 &rule->arg.hdr_add.re, rule->action)) {
2255 rule_ret = HTTP_RULE_RES_BADREQ;
2256 goto end;
2257 }
Sasha Pachev218f0642014-06-16 12:05:59 -06002258 break;
2259
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002260 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002261 ctx.idx = 0;
2262 /* remove all occurrences of the header */
2263 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002264 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002265 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2266 }
Willy Tarreau85603282015-01-21 20:39:27 +01002267 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002268
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002269 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002270 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002271 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002272
2273 replace = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002274 if (!replace) {
2275 rule_ret = HTTP_RULE_RES_BADREQ;
2276 goto end;
2277 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002278
2279 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002280 memcpy(replace->area, rule->arg.hdr_add.name,
2281 rule->arg.hdr_add.name_len);
2282 replace->data = rule->arg.hdr_add.name_len;
2283 replace->area[replace->data++] = ':';
2284 replace->area[replace->data++] = ' ';
2285 replace->data += build_logline(s,
2286 replace->area + replace->data,
2287 replace->size - replace->data,
2288 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002289
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002290 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002291 /* remove all occurrences of the header */
2292 ctx.idx = 0;
2293 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002294 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002295 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2296 }
2297 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002298
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002299 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002300 static unsigned char rate_limit = 0;
2301
2302 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002303 replace->area[rule->arg.hdr_add.name_len] = 0;
2304 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,
2305 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002306 }
2307
2308 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2309 if (sess->fe != s->be)
2310 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2311 if (sess->listener->counters)
2312 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2313 if (objt_server(s->target))
2314 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2315 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002316
2317 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002318 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002319 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002320
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002321 case ACT_HTTP_DEL_ACL:
2322 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002323 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002324 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002325
2326 /* collect reference */
2327 ref = pat_ref_lookup(rule->arg.map.ref);
2328 if (!ref)
2329 continue;
2330
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002331 /* allocate key */
2332 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002333 if (!key) {
2334 rule_ret = HTTP_RULE_RES_BADREQ;
2335 goto end;
2336 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002337
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002338 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002339 key->data = build_logline(s, key->area, key->size,
2340 &rule->arg.map.key);
2341 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002342
2343 /* perform update */
2344 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002345 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002346 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002347 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002348
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002349 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002350 break;
2351 }
2352
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002353 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002354 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002355 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002356
2357 /* collect reference */
2358 ref = pat_ref_lookup(rule->arg.map.ref);
2359 if (!ref)
2360 continue;
2361
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002362 /* allocate key */
2363 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002364 if (!key) {
2365 rule_ret = HTTP_RULE_RES_BADREQ;
2366 goto end;
2367 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002368
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002369 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002370 key->data = build_logline(s, key->area, key->size,
2371 &rule->arg.map.key);
2372 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002373
2374 /* perform update */
2375 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002376 if (pat_ref_find_elt(ref, key->area) == NULL)
2377 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002378
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002379 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002380 break;
2381 }
2382
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002383 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002384 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002385 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002386
2387 /* collect reference */
2388 ref = pat_ref_lookup(rule->arg.map.ref);
2389 if (!ref)
2390 continue;
2391
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002392 /* allocate key */
2393 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002394 if (!key) {
2395 rule_ret = HTTP_RULE_RES_BADREQ;
2396 goto end;
2397 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002398
2399 /* allocate value */
2400 value = alloc_trash_chunk();
2401 if (!value) {
2402 free_trash_chunk(key);
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002403 rule_ret = HTTP_RULE_RES_BADREQ;
2404 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002405 }
2406
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002407 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002408 key->data = build_logline(s, key->area, key->size,
2409 &rule->arg.map.key);
2410 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002411
2412 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002413 value->data = build_logline(s, value->area,
2414 value->size,
2415 &rule->arg.map.value);
2416 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002417
2418 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002419 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002420 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002421 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002422 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002423 else
2424 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002425 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002426 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002427 free_trash_chunk(key);
2428 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002429 break;
2430 }
William Lallemand73025dd2014-04-24 14:38:37 +02002431
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002432 case ACT_HTTP_REDIR:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002433 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002434 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002435 rule_ret = HTTP_RULE_RES_BADREQ;
2436 goto end;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002437
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002438 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2439 /* Note: only the first valid tracking parameter of each
2440 * applies.
2441 */
2442
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002443 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002444 struct stktable *t;
2445 struct stksess *ts;
2446 struct stktable_key *key;
2447 void *ptr;
2448
2449 t = rule->arg.trk_ctr.table.t;
2450 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2451
2452 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002453 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002454
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002455 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002456
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002457 /* let's count a new HTTP request as it's the first time we do it */
2458 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2459 if (ptr)
2460 stktable_data_cast(ptr, http_req_cnt)++;
2461
2462 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2463 if (ptr)
2464 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2465 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2466
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002467 /* When the client triggers a 4xx from the server, it's most often due
2468 * to a missing object or permission. These events should be tracked
2469 * because if they happen often, it may indicate a brute force or a
2470 * vulnerability scan. Normally this is done when receiving the response
2471 * but here we're tracking after this ought to have been done so we have
2472 * to do it on purpose.
2473 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002474 if ((unsigned)(txn->status - 400) < 100) {
2475 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2476 if (ptr)
2477 stktable_data_cast(ptr, http_err_cnt)++;
2478
2479 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2480 if (ptr)
2481 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2482 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2483 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002484
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002485 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002486
Emeric Brun0fed0b02017-11-29 16:15:07 +01002487 /* If data was modified, we need to touch to re-schedule sync */
2488 stktable_touch_local(t, ts, 0);
2489
Emeric Brun819fc6f2017-06-13 19:37:32 +02002490 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2491 if (sess->fe != s->be)
2492 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2493
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002494 }
2495 }
2496 break;
2497
Thierry FOURNIER42148732015-09-02 17:17:33 +02002498 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002499 if ((s->req.flags & CF_READ_ERROR) ||
2500 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2501 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2502 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002503 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002504
Willy Tarreauacc98002015-09-27 23:34:39 +02002505 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002506 case ACT_RET_ERR:
2507 case ACT_RET_CONT:
2508 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002509 case ACT_RET_STOP:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002510 rule_ret = HTTP_RULE_RES_STOP;
2511 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002512 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002513 s->current_rule = rule;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002514 rule_ret = HTTP_RULE_RES_YIELD;
2515 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002516 }
William Lallemand73025dd2014-04-24 14:38:37 +02002517 break;
2518
Joseph Herlant5ba80252018-11-15 09:25:36 -08002519 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002520 default:
2521 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002522 }
2523 }
2524
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002525 end:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002526 /* we reached the end of the rules, nothing to report */
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002527 return rule_ret;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002528}
2529
2530
Willy Tarreau71241ab2012-12-27 11:30:54 +01002531/* Perform an HTTP redirect based on the information in <rule>. The function
2532 * returns non-zero on success, or zero in case of a, irrecoverable error such
2533 * as too large a request to build a valid response.
2534 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002535int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002536{
Willy Tarreaub329a312015-05-22 16:27:37 +02002537 struct http_msg *req = &txn->req;
2538 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002539 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002540 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002541 int ret = 0;
2542
Christopher Fauletf2824e62018-10-01 12:12:37 +02002543 if (IS_HTX_STRM(s))
2544 return htx_apply_redirect_rule(rule, s, txn);
2545
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002546 chunk = alloc_trash_chunk();
2547 if (!chunk)
2548 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002549
2550 /* build redirect message */
2551 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002552 case 308:
2553 msg_fmt = HTTP_308;
2554 break;
2555 case 307:
2556 msg_fmt = HTTP_307;
2557 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002558 case 303:
2559 msg_fmt = HTTP_303;
2560 break;
2561 case 301:
2562 msg_fmt = HTTP_301;
2563 break;
2564 case 302:
2565 default:
2566 msg_fmt = HTTP_302;
2567 break;
2568 }
2569
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002570 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2571 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002572
2573 switch(rule->type) {
2574 case REDIRECT_TYPE_SCHEME: {
2575 const char *path;
2576 const char *host;
2577 struct hdr_ctx ctx;
2578 int pathlen;
2579 int hostlen;
2580
2581 host = "";
2582 hostlen = 0;
2583 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002584 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002585 host = ctx.line + ctx.val;
2586 hostlen = ctx.vlen;
2587 }
2588
Willy Tarreau6b952c82018-09-10 17:45:34 +02002589 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002590 /* build message using path */
2591 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002592 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002593 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2594 int qs = 0;
2595 while (qs < pathlen) {
2596 if (path[qs] == '?') {
2597 pathlen = qs;
2598 break;
2599 }
2600 qs++;
2601 }
2602 }
2603 } else {
2604 path = "/";
2605 pathlen = 1;
2606 }
2607
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002608 if (rule->rdr_str) { /* this is an old "redirect" rule */
2609 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002610 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002611 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002612
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002613 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002614 memcpy(chunk->area + chunk->data, rule->rdr_str,
2615 rule->rdr_len);
2616 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002617 }
2618 else {
2619 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002620 chunk->data += build_logline(s,
2621 chunk->area + chunk->data,
2622 chunk->size - chunk->data,
2623 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002624
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002625 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002626 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002627 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002628 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002629 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002630 memcpy(chunk->area + chunk->data, "://", 3);
2631 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002632
2633 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002634 memcpy(chunk->area + chunk->data, host, hostlen);
2635 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002636
2637 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002638 memcpy(chunk->area + chunk->data, path, pathlen);
2639 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002640
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002641 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002642 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002643 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002644 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002645 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002646 chunk->area[chunk->data] = '/';
2647 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002648 }
2649
2650 break;
2651 }
2652 case REDIRECT_TYPE_PREFIX: {
2653 const char *path;
2654 int pathlen;
2655
Willy Tarreau6b952c82018-09-10 17:45:34 +02002656 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002657 /* build message using path */
2658 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002659 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002660 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2661 int qs = 0;
2662 while (qs < pathlen) {
2663 if (path[qs] == '?') {
2664 pathlen = qs;
2665 break;
2666 }
2667 qs++;
2668 }
2669 }
2670 } else {
2671 path = "/";
2672 pathlen = 1;
2673 }
2674
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002675 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002676 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002677 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002678
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002679 /* add prefix. Note that if prefix == "/", we don't want to
2680 * add anything, otherwise it makes it hard for the user to
2681 * configure a self-redirection.
2682 */
2683 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002684 memcpy(chunk->area + chunk->data,
2685 rule->rdr_str, rule->rdr_len);
2686 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002687 }
2688 }
2689 else {
2690 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002691 chunk->data += build_logline(s,
2692 chunk->area + chunk->data,
2693 chunk->size - chunk->data,
2694 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002695
2696 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002697 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002698 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002699 }
2700
2701 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002702 memcpy(chunk->area + chunk->data, path, pathlen);
2703 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002704
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002705 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002706 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002707 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002708 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002709 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002710 chunk->area[chunk->data] = '/';
2711 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002712 }
2713
2714 break;
2715 }
2716 case REDIRECT_TYPE_LOCATION:
2717 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002718 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002719 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002720 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002721
2722 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002723 memcpy(chunk->area + chunk->data, rule->rdr_str,
2724 rule->rdr_len);
2725 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002726 }
2727 else {
2728 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002729 chunk->data += build_logline(s,
2730 chunk->area + chunk->data,
2731 chunk->size - chunk->data,
2732 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002733
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002734 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002735 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002736 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002737 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002738 break;
2739 }
2740
2741 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002742 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2743 chunk->data += 14;
2744 memcpy(chunk->area + chunk->data, rule->cookie_str,
2745 rule->cookie_len);
2746 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002747 }
2748
Willy Tarreau19b14122017-02-28 09:48:11 +01002749 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002750 txn->status = rule->code;
2751 /* let's log the request time */
2752 s->logs.tv_request = now;
2753
Christopher Fauletbe821b92017-03-30 11:21:53 +02002754 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002755 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2756 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2757 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002758 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002759 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002760 memcpy(chunk->area + chunk->data,
2761 "\r\nProxy-Connection: keep-alive", 30);
2762 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002763 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002764 memcpy(chunk->area + chunk->data,
2765 "\r\nConnection: keep-alive", 24);
2766 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002767 }
2768 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002769 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2770 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002771 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002772 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002773 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002774 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002775 req->next -= req->sov;
2776 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002777 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002778 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002779 req->msg_state = HTTP_MSG_CLOSED;
2780 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002781 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002782 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002783 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002784 /* let the server side turn to SI_ST_CLO */
2785 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002786 } else {
2787 /* keep-alive not possible */
2788 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002789 memcpy(chunk->area + chunk->data,
2790 "\r\nProxy-Connection: close\r\n\r\n", 29);
2791 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002792 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002793 memcpy(chunk->area + chunk->data,
2794 "\r\nConnection: close\r\n\r\n", 23);
2795 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002796 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002797 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002798 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002799 }
2800
Willy Tarreaue7dff022015-04-03 01:14:29 +02002801 if (!(s->flags & SF_ERR_MASK))
2802 s->flags |= SF_ERR_LOCAL;
2803 if (!(s->flags & SF_FINST_MASK))
2804 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002805
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002806 ret = 1;
2807 leave:
2808 free_trash_chunk(chunk);
2809 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002810}
2811
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002812/* This stream analyser runs all HTTP request processing which is common to
2813 * frontends and backends, which means blocking ACLs, filters, connection-close,
2814 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002815 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002816 * either needs more data or wants to immediately abort the request (eg: deny,
2817 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002818 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002819int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002820{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002821 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002822 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002823 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002824 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002825 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002826 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002827 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002828 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002829
Christopher Faulete0768eb2018-10-03 16:38:02 +02002830 if (IS_HTX_STRM(s))
2831 return htx_process_req_common(s, req, an_bit, px);
2832
Willy Tarreau655dce92009-11-08 13:10:58 +01002833 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002834 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002835 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002836 }
2837
Christopher Faulet45073512018-07-20 10:16:29 +02002838 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 +02002839 now_ms, __FUNCTION__,
2840 s,
2841 req,
2842 req->rex, req->wex,
2843 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002844 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002845 req->analysers);
2846
Willy Tarreau65410832014-04-28 21:25:43 +02002847 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002848 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002849
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002850 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002851 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002852 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002853
Willy Tarreau0b748332014-04-29 00:13:29 +02002854 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002855 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2856 goto return_prx_yield;
2857
Willy Tarreau0b748332014-04-29 00:13:29 +02002858 case HTTP_RULE_RES_CONT:
2859 case HTTP_RULE_RES_STOP: /* nothing to do */
2860 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002861
Willy Tarreau0b748332014-04-29 00:13:29 +02002862 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2863 if (txn->flags & TX_CLTARPIT)
2864 goto tarpit;
2865 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002866
Willy Tarreau0b748332014-04-29 00:13:29 +02002867 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2868 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002869
Willy Tarreau0b748332014-04-29 00:13:29 +02002870 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002871 goto done;
2872
Willy Tarreau0b748332014-04-29 00:13:29 +02002873 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2874 goto return_bad_req;
2875 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002876 }
2877
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002878 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2879 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002880 struct hdr_ctx ctx;
2881
2882 ctx.idx = 0;
2883 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002884 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002885 if (unlikely(http_header_add_tail2(&txn->req,
2886 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002887 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002888 goto return_bad_req;
2889 }
2890 }
2891
2892 }
2893
Willy Tarreau52542592014-04-28 18:33:26 +02002894 /* OK at this stage, we know that the request was accepted according to
2895 * the http-request rules, we can check for the stats. Note that the
2896 * URI is detected *before* the req* rules in order not to be affected
2897 * by a possible reqrep, while they are processed *after* so that a
2898 * reqdeny can still block them. This clearly needs to change in 1.6!
2899 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002900 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002901 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01002902 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002903 txn->status = 500;
2904 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002905 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002906
Willy Tarreaue7dff022015-04-03 01:14:29 +02002907 if (!(s->flags & SF_ERR_MASK))
2908 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002909 goto return_prx_cond;
2910 }
2911
2912 /* parse the whole stats request and extract the relevant information */
2913 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002914 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002915 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002916
Willy Tarreau0b748332014-04-29 00:13:29 +02002917 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2918 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002919
Willy Tarreau0b748332014-04-29 00:13:29 +02002920 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2921 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002922 }
2923
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002924 /* evaluate the req* rules except reqadd */
2925 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002926 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002927 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002928
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002929 if (txn->flags & TX_CLDENY)
2930 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002931
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002932 if (txn->flags & TX_CLTARPIT) {
2933 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002934 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002935 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002936 }
Willy Tarreau06619262006-12-17 08:37:22 +01002937
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002938 /* add request headers from the rule sets in the same order */
2939 list_for_each_entry(wl, &px->req_add, list) {
2940 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002941 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002942 ret = acl_pass(ret);
2943 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2944 ret = !ret;
2945 if (!ret)
2946 continue;
2947 }
2948
Christopher Faulet10079f52018-10-03 15:17:28 +02002949 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002950 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002951 }
2952
Willy Tarreau52542592014-04-28 18:33:26 +02002953
2954 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002955 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2956 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002957 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002958 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002959 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002960
Willy Tarreaue7dff022015-04-03 01:14:29 +02002961 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2962 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2963 if (!(s->flags & SF_FINST_MASK))
2964 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002965
Willy Tarreau70730dd2014-04-24 18:06:27 +02002966 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002967 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2968 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002969 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002970 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002971 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002972
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002973 /* check whether we have some ACLs set to redirect this request */
2974 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002975 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002976 int ret;
2977
Willy Tarreau192252e2015-04-04 01:47:55 +02002978 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002979 ret = acl_pass(ret);
2980 if (rule->cond->pol == ACL_COND_UNLESS)
2981 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002982 if (!ret)
2983 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01002984 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002985 if (!http_apply_redirect_rule(rule, s, txn))
2986 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002987 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002988 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002989
Willy Tarreau2be39392010-01-03 17:24:51 +01002990 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2991 * If this happens, then the data will not come immediately, so we must
2992 * send all what we have without waiting. Note that due to the small gain
2993 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002994 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01002995 * itself once used.
2996 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002997 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01002998
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002999 done: /* done with this analyser, continue with next ones that the calling
3000 * points will have set, if any.
3001 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003002 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003003 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3004 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003005 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003006
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003007 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003008 /* Allow cookie logging
3009 */
3010 if (s->be->cookie_name || sess->fe->capture_name)
3011 manage_client_side_cookies(s, req);
3012
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003013 /* When a connection is tarpitted, we use the tarpit timeout,
3014 * which may be the same as the connect timeout if unspecified.
3015 * If unset, then set it to zero because we really want it to
3016 * eventually expire. We build the tarpit as an analyser.
3017 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003018 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003019
3020 /* wipe the request out so that we can drop the connection early
3021 * if the client closes first.
3022 */
3023 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003024
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003025 txn->status = http_err_codes[deny_status];
3026
Christopher Faulet0184ea72017-01-05 14:06:34 +01003027 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003028 req->analysers |= AN_REQ_HTTP_TARPIT;
3029 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3030 if (!req->analyse_exp)
3031 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003032 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003033 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003034 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003035 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003036 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003037 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003038 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003039
3040 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003041
3042 /* Allow cookie logging
3043 */
3044 if (s->be->cookie_name || sess->fe->capture_name)
3045 manage_client_side_cookies(s, req);
3046
Willy Tarreau0b748332014-04-29 00:13:29 +02003047 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003048 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003049 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003050 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003051 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003052 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003053 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003054 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003055 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003056 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003057 goto return_prx_cond;
3058
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003059 return_bad_req:
3060 /* We centralize bad requests processing here */
3061 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3062 /* we detected a parsing error. We want to archive this request
3063 * in the dedicated proxy area for later troubleshooting.
3064 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003065 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003066 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003067
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003068 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003069 txn->req.msg_state = HTTP_MSG_ERROR;
3070 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003071 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003072
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003073 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003074 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003075 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003076
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003077 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003078 if (!(s->flags & SF_ERR_MASK))
3079 s->flags |= SF_ERR_PRXCOND;
3080 if (!(s->flags & SF_FINST_MASK))
3081 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003082
Christopher Faulet0184ea72017-01-05 14:06:34 +01003083 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003084 req->analyse_exp = TICK_ETERNITY;
3085 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003086
3087 return_prx_yield:
3088 channel_dont_connect(req);
3089 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003090}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003091
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003092/* This function performs all the processing enabled for the current request.
3093 * It returns 1 if the processing can continue on next analysers, or zero if it
3094 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003095 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003096 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003097int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003098{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003099 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003100 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003101 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003102 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003103
Christopher Faulete0768eb2018-10-03 16:38:02 +02003104 if (IS_HTX_STRM(s))
3105 return htx_process_request(s, req, an_bit);
3106
Willy Tarreau655dce92009-11-08 13:10:58 +01003107 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003108 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003109 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003110 return 0;
3111 }
3112
Christopher Faulet45073512018-07-20 10:16:29 +02003113 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 +02003114 now_ms, __FUNCTION__,
3115 s,
3116 req,
3117 req->rex, req->wex,
3118 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003119 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003120 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003121
Willy Tarreau59234e92008-11-30 23:51:27 +01003122 /*
3123 * Right now, we know that we have processed the entire headers
3124 * and that unwanted requests have been filtered out. We can do
3125 * whatever we want with the remaining request. Also, now we
3126 * may have separate values for ->fe, ->be.
3127 */
Willy Tarreau06619262006-12-17 08:37:22 +01003128
Willy Tarreau59234e92008-11-30 23:51:27 +01003129 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003130 * If HTTP PROXY is set we simply get remote server address parsing
3131 * incoming request. Note that this requires that a connection is
3132 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003133 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003134 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003135 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003136 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003137
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003138 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003139 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003140 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003141 txn->req.msg_state = HTTP_MSG_ERROR;
3142 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003143 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003144 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003145
Willy Tarreaue7dff022015-04-03 01:14:29 +02003146 if (!(s->flags & SF_ERR_MASK))
3147 s->flags |= SF_ERR_RESOURCE;
3148 if (!(s->flags & SF_FINST_MASK))
3149 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003150
3151 return 0;
3152 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003153
Willy Tarreau6b952c82018-09-10 17:45:34 +02003154 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003155 if (url2sa(ci_head(req) + msg->sl.rq.u,
3156 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003157 &conn->addr.to, NULL) == -1)
3158 goto return_bad_req;
3159
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003160 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003161 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3162 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003163 * u_l characters by a single "/".
3164 */
3165 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003166 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003167 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3168 int delta;
3169
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003170 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003171 http_msg_move_end(&txn->req, delta);
3172 cur_end += delta;
3173 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3174 goto return_bad_req;
3175 }
3176 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003177 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003178 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3179 int delta;
3180
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003181 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003182 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003183 http_msg_move_end(&txn->req, delta);
3184 cur_end += delta;
3185 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3186 goto return_bad_req;
3187 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003188 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003189
Willy Tarreau59234e92008-11-30 23:51:27 +01003190 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003191 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003192 * Note that doing so might move headers in the request, but
3193 * the fields will stay coherent and the URI will not move.
3194 * This should only be performed in the backend.
3195 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003196 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003197 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003198
William Lallemanda73203e2012-03-12 12:48:57 +01003199 /* add unique-id if "header-unique-id" is specified */
3200
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003201 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003202 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003203 goto return_bad_req;
3204 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003205 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003206 }
William Lallemanda73203e2012-03-12 12:48:57 +01003207
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003208 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003209 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003210 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003211 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003212 goto return_bad_req;
3213 }
3214
Cyril Bontéb21570a2009-11-29 20:04:48 +01003215 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003216 * 9: add X-Forwarded-For if either the frontend or the backend
3217 * asks for it.
3218 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003219 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003220 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003221 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3222 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3223 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003224 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003225 /* The header is set to be added only if none is present
3226 * and we found it, so don't do anything.
3227 */
3228 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003229 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003230 /* Add an X-Forwarded-For header unless the source IP is
3231 * in the 'except' network range.
3232 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003233 if ((!sess->fe->except_mask.s_addr ||
3234 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3235 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003236 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003237 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003238 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003239 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003240 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003241 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003242
3243 /* Note: we rely on the backend to get the header name to be used for
3244 * x-forwarded-for, because the header is really meant for the backends.
3245 * However, if the backend did not specify any option, we have to rely
3246 * on the frontend's header name.
3247 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003248 if (s->be->fwdfor_hdr_len) {
3249 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003250 memcpy(trash.area,
3251 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003252 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003253 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003254 memcpy(trash.area,
3255 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003256 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003257 len += snprintf(trash.area + len,
3258 trash.size - len,
3259 ": %d.%d.%d.%d", pn[0], pn[1],
3260 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003261
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003262 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003263 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003264 }
3265 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003266 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003267 /* FIXME: for the sake of completeness, we should also support
3268 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003269 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003270 int len;
3271 char pn[INET6_ADDRSTRLEN];
3272 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003273 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003274 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003275
Willy Tarreau59234e92008-11-30 23:51:27 +01003276 /* Note: we rely on the backend to get the header name to be used for
3277 * x-forwarded-for, because the header is really meant for the backends.
3278 * However, if the backend did not specify any option, we have to rely
3279 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003280 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003281 if (s->be->fwdfor_hdr_len) {
3282 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003283 memcpy(trash.area, s->be->fwdfor_hdr_name,
3284 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003285 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003286 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003287 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3288 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003289 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003290 len += snprintf(trash.area + len, trash.size - len,
3291 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003292
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003293 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003294 goto return_bad_req;
3295 }
3296 }
3297
3298 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003299 * 10: add X-Original-To if either the frontend or the backend
3300 * asks for it.
3301 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003302 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003303
3304 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003305 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003306 /* Add an X-Original-To header unless the destination IP is
3307 * in the 'except' network range.
3308 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003309 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003310
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003311 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003312 ((!sess->fe->except_mask_to.s_addr ||
3313 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3314 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003315 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003316 (((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 +02003317 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003318 int len;
3319 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003320 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003321
3322 /* Note: we rely on the backend to get the header name to be used for
3323 * x-original-to, because the header is really meant for the backends.
3324 * However, if the backend did not specify any option, we have to rely
3325 * on the frontend's header name.
3326 */
3327 if (s->be->orgto_hdr_len) {
3328 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003329 memcpy(trash.area,
3330 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003331 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003332 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003333 memcpy(trash.area,
3334 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003335 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003336 len += snprintf(trash.area + len,
3337 trash.size - len,
3338 ": %d.%d.%d.%d", pn[0], pn[1],
3339 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003340
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003341 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003342 goto return_bad_req;
3343 }
3344 }
3345 }
3346
Willy Tarreau50fc7772012-11-11 22:19:57 +01003347 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3348 * If an "Upgrade" token is found, the header is left untouched in order not to have
3349 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3350 * "Upgrade" is present in the Connection header.
3351 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003352 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003353 unsigned int want_flags = 0;
3354
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003355 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003356 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003357 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003358 want_flags |= TX_CON_CLO_SET;
3359 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003360 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003361 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003362 want_flags |= TX_CON_KAL_SET;
3363 }
3364
3365 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003366 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003367 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003368
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003369
Willy Tarreau522d6c02009-12-06 18:49:18 +01003370 /* If we have no server assigned yet and we're balancing on url_param
3371 * with a POST request, we may be interested in checking the body for
3372 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003373 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003374 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003375 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003376 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003377 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003378 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003379 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003380
Christopher Fauletbe821b92017-03-30 11:21:53 +02003381 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3382 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +01003383
Christopher Fauletbe821b92017-03-30 11:21:53 +02003384 /* We expect some data from the client. Unless we know for sure
3385 * we already have a full request, we have to re-enable quick-ack
3386 * in case we previously disabled it, otherwise we might cause
3387 * the client to delay further data.
3388 */
3389 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletbe821b92017-03-30 11:21:53 +02003390 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003391 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau1a18b542018-12-11 16:37:42 +01003392 conn_set_quickack(cli_conn, 1);
Willy Tarreau03945942009-12-22 16:50:27 +01003393
Willy Tarreau59234e92008-11-30 23:51:27 +01003394 /*************************************************************
3395 * OK, that's finished for the headers. We have done what we *
3396 * could. Let's switch to the DATA state. *
3397 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003398 req->analyse_exp = TICK_ETERNITY;
3399 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003400
Willy Tarreau59234e92008-11-30 23:51:27 +01003401 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003402 /* OK let's go on with the BODY now */
3403 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003404
Willy Tarreau59234e92008-11-30 23:51:27 +01003405 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003406 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003407 /* we detected a parsing error. We want to archive this request
3408 * in the dedicated proxy area for later troubleshooting.
3409 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003410 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003411 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003412
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003413 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003414 txn->req.msg_state = HTTP_MSG_ERROR;
3415 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003416 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003417 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003418
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003419 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003420 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003421 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003422
Willy Tarreaue7dff022015-04-03 01:14:29 +02003423 if (!(s->flags & SF_ERR_MASK))
3424 s->flags |= SF_ERR_PRXCOND;
3425 if (!(s->flags & SF_FINST_MASK))
3426 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003427 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003428}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003429
Willy Tarreau60b85b02008-11-30 23:28:40 +01003430/* This function is an analyser which processes the HTTP tarpit. It always
3431 * returns zero, at the beginning because it prevents any other processing
3432 * from occurring, and at the end because it terminates the request.
3433 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003434int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003435{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003436 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003437
Christopher Faulete0768eb2018-10-03 16:38:02 +02003438 if (IS_HTX_STRM(s))
3439 return htx_process_tarpit(s, req, an_bit);
3440
Willy Tarreau60b85b02008-11-30 23:28:40 +01003441 /* This connection is being tarpitted. The CLIENT side has
3442 * already set the connect expiration date to the right
3443 * timeout. We just have to check that the client is still
3444 * there and that the timeout has not expired.
3445 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003446 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003447 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003448 !tick_is_expired(req->analyse_exp, now_ms))
3449 return 0;
3450
3451 /* We will set the queue timer to the time spent, just for
3452 * logging purposes. We fake a 500 server error, so that the
3453 * attacker will not suspect his connection has been tarpitted.
3454 * It will not cause trouble to the logs because we can exclude
3455 * the tarpitted connections by filtering on the 'PT' status flags.
3456 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003457 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3458
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003459 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003460 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003461
Christopher Faulet0184ea72017-01-05 14:06:34 +01003462 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003463 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003464
Willy Tarreaue7dff022015-04-03 01:14:29 +02003465 if (!(s->flags & SF_ERR_MASK))
3466 s->flags |= SF_ERR_PRXCOND;
3467 if (!(s->flags & SF_FINST_MASK))
3468 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003469 return 0;
3470}
3471
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003472/* This function is an analyser which waits for the HTTP request body. It waits
3473 * for either the buffer to be full, or the full advertised contents to have
3474 * reached the buffer. It must only be called after the standard HTTP request
3475 * processing has occurred, because it expects the request to be parsed and will
3476 * look for the Expect header. It may send a 100-Continue interim response. It
3477 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3478 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3479 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003480 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003481int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003482{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003483 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003484 struct http_txn *txn = s->txn;
3485 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003486
Christopher Faulete0768eb2018-10-03 16:38:02 +02003487 if (IS_HTX_STRM(s))
3488 return htx_wait_for_request_body(s, req, an_bit);
3489
Willy Tarreaud34af782008-11-30 23:36:37 +01003490 /* We have to parse the HTTP request body to find any required data.
3491 * "balance url_param check_post" should have been the only way to get
3492 * into this. We were brought here after HTTP header analysis, so all
3493 * related structures are ready.
3494 */
3495
Willy Tarreau890988f2014-04-10 11:59:33 +02003496 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3497 /* This is the first call */
3498 if (msg->msg_state < HTTP_MSG_BODY)
3499 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003500
Willy Tarreau890988f2014-04-10 11:59:33 +02003501 if (msg->msg_state < HTTP_MSG_100_SENT) {
3502 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3503 * send an HTTP/1.1 100 Continue intermediate response.
3504 */
3505 if (msg->flags & HTTP_MSGF_VER_11) {
3506 struct hdr_ctx ctx;
3507 ctx.idx = 0;
3508 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003509 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003510 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003511 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003512 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003513 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003514 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003515 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003516 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003517
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003518 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003519 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003520 * must save the body in msg->next because it survives buffer
3521 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003522 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003523 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003524
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003525 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003526 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3527 else
3528 msg->msg_state = HTTP_MSG_DATA;
3529 }
3530
Willy Tarreau890988f2014-04-10 11:59:33 +02003531 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3532 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003533 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003534 goto missing_data;
3535
3536 /* OK we have everything we need now */
3537 goto http_end;
3538 }
3539
3540 /* OK here we're parsing a chunked-encoded message */
3541
Willy Tarreau522d6c02009-12-06 18:49:18 +01003542 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003543 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003544 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003545 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003546 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003547 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003548 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003549
Willy Tarreau115acb92009-12-26 13:56:06 +01003550 if (!ret)
3551 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003552 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003553 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003554 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003555 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003556 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003557 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003558 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003559
3560 msg->chunk_len = chunk;
3561 msg->body_len += chunk;
3562
3563 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003564 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003565 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003566 }
3567
Willy Tarreaud98cf932009-12-27 22:54:55 +01003568 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003569 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3570 * for at least a whole chunk or the whole content length bytes after
3571 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003572 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003573 if (msg->msg_state == HTTP_MSG_TRAILERS)
3574 goto http_end;
3575
Willy Tarreaue115b492015-05-01 23:05:14 +02003576 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003577 goto http_end;
3578
3579 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003580 /* we get here if we need to wait for more data. If the buffer is full,
3581 * we have the maximum we can expect.
3582 */
Willy Tarreau23752332018-06-15 14:54:53 +02003583 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003584 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003585
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003586 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003587 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003588 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003589
Willy Tarreaue7dff022015-04-03 01:14:29 +02003590 if (!(s->flags & SF_ERR_MASK))
3591 s->flags |= SF_ERR_CLITO;
3592 if (!(s->flags & SF_FINST_MASK))
3593 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003594 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003595 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003596
3597 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003598 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003599 /* Not enough data. We'll re-use the http-request
3600 * timeout here. Ideally, we should set the timeout
3601 * relative to the accept() date. We just set the
3602 * request timeout once at the beginning of the
3603 * request.
3604 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003605 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003606 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003607 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003608 return 0;
3609 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003610
3611 http_end:
3612 /* The situation will not evolve, so let's give up on the analysis. */
3613 s->logs.tv_request = now; /* update the request timer to reflect full request */
3614 req->analysers &= ~an_bit;
3615 req->analyse_exp = TICK_ETERNITY;
3616 return 1;
3617
3618 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003619 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003620 txn->req.msg_state = HTTP_MSG_ERROR;
3621 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003622 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003623
Willy Tarreaue7dff022015-04-03 01:14:29 +02003624 if (!(s->flags & SF_ERR_MASK))
3625 s->flags |= SF_ERR_PRXCOND;
3626 if (!(s->flags & SF_FINST_MASK))
3627 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003628
Willy Tarreau522d6c02009-12-06 18:49:18 +01003629 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003630 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003631 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003632 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003633 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003634 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003635}
3636
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003637/* send a server's name with an outgoing request over an established connection.
3638 * Note: this function is designed to be called once the request has been scheduled
3639 * for being forwarded. This is the reason why it rewinds the buffer before
3640 * proceeding.
3641 */
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003642int http_send_name_header(struct stream *s, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003643
3644 struct hdr_ctx ctx;
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003645 struct http_txn *txn = s->txn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003646 char *hdr_name = be->server_id_hdr_name;
3647 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003648 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003649 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003650 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003651
Christopher Faulet64159df2018-10-24 21:15:35 +02003652 if (IS_HTX_STRM(s))
3653 return htx_send_name_header(s, be, srv_name);
William Lallemandd9e90662012-01-30 17:27:17 +01003654 ctx.idx = 0;
3655
Willy Tarreau211cdec2014-04-17 20:18:08 +02003656 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003657 if (old_o) {
3658 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003659 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003660 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003661 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003662 }
3663
Willy Tarreauf37954d2018-06-15 18:31:02 +02003664 old_i = ci_data(chn);
3665 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 -05003666 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003667 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003668 }
3669
3670 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003671 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003672 memcpy(hdr_val, hdr_name, hdr_name_len);
3673 hdr_val += hdr_name_len;
3674 *hdr_val++ = ':';
3675 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003676 hdr_val += strlcpy2(hdr_val, srv_name,
3677 trash.area + trash.size - hdr_val);
3678 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3679 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003680
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003681 if (old_o) {
3682 /* If this was a forwarded request, we must readjust the amount of
3683 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003684 * variations. Note that the current state is >= HTTP_MSG_BODY,
3685 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003686 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003687 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003688 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003689 txn->req.next -= old_o;
3690 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003691 }
3692
Mark Lamourinec2247f02012-01-04 13:02:01 -05003693 return 0;
3694}
3695
Willy Tarreau610ecce2010-01-04 21:15:02 +01003696/* Terminate current transaction and prepare a new one. This is very tricky
3697 * right now but it works.
3698 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003699void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003700{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003701 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003702 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003703 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003704 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003705 struct connection *srv_conn;
3706 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003707 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003708
Willy Tarreau610ecce2010-01-04 21:15:02 +01003709 /* FIXME: We need a more portable way of releasing a backend's and a
3710 * server's connections. We need a safer way to reinitialize buffer
3711 * flags. We also need a more accurate method for computing per-request
3712 * data.
3713 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003714 cs = objt_cs(s->si[1].end);
3715 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003716
Willy Tarreau4213a112013-12-15 10:25:42 +01003717 /* unless we're doing keep-alive, we want to quickly close the connection
3718 * to the server.
3719 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003720 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003721 !si_conn_ready(&s->si[1]) || !srv_conn->owner) {
Willy Tarreau350f4872014-11-28 14:42:25 +01003722 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3723 si_shutr(&s->si[1]);
3724 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003725 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003726
Willy Tarreaue7dff022015-04-03 01:14:29 +02003727 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003728 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003729 if (unlikely(s->srv_conn))
3730 sess_change_server(s, NULL);
3731 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003732
3733 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003734 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003735
Willy Tarreaueee5b512015-04-03 23:46:31 +02003736 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003737 int n;
3738
Willy Tarreaueee5b512015-04-03 23:46:31 +02003739 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003740 if (n < 1 || n > 5)
3741 n = 0;
3742
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003743 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003744 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003745 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003746 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003747 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003748 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3749 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003750 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003751 }
3752
3753 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003754 s->logs.bytes_in -= ci_data(&s->req);
3755 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003756
Willy Tarreau66425e32018-07-25 06:55:12 +02003757 /* we may need to know the position in the queue */
3758 pendconn_free(s);
3759
Willy Tarreau610ecce2010-01-04 21:15:02 +01003760 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003761 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003762 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003763 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003764 s->do_log(s);
3765 }
3766
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003767 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003768 stream_stop_content_counters(s);
3769 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003770
Willy Tarreau9efd7452018-05-31 14:48:54 +02003771 /* reset the profiling counter */
3772 s->task->calls = 0;
3773 s->task->cpu_time = 0;
3774 s->task->lat_time = 0;
3775 s->task->call_date = (profiling & HA_PROF_TASKS) ? now_mono_time() : 0;
3776
Willy Tarreau610ecce2010-01-04 21:15:02 +01003777 s->logs.accept_date = date; /* user-visible date for logging */
3778 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003779 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3780 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003781 tv_zero(&s->logs.tv_request);
3782 s->logs.t_queue = -1;
3783 s->logs.t_connect = -1;
3784 s->logs.t_data = -1;
3785 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003786 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3787 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003788
Willy Tarreauf37954d2018-06-15 18:31:02 +02003789 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3790 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003791
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003792 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003793 if (s->flags & SF_CURR_SESS) {
3794 s->flags &= ~SF_CURR_SESS;
Willy Tarreaub54c40a2018-12-02 19:28:41 +01003795 HA_ATOMIC_SUB(&__objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003796 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003797 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003798 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003799 }
3800
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003801 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003802
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003803
3804 /* If we're doing keepalive, first call the mux detach() method
3805 * to let it know we want to detach without freing the connection.
3806 * We then can call si_release_endpoint() to destroy the conn_stream
Willy Tarreau4213a112013-12-15 10:25:42 +01003807 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003808 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard985f1392018-11-30 17:31:52 +01003809 !si_conn_ready(&s->si[1]))
Willy Tarreau323a2d92015-08-04 19:00:17 +02003810 srv_conn = NULL;
Olivier Houchard985f1392018-11-30 17:31:52 +01003811 else if (!srv_conn->owner) {
3812 srv_conn->owner = s->sess;
3813 session_add_conn(s->sess, srv_conn, s->target);
3814 }
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003815 si_release_endpoint(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003816
Willy Tarreau350f4872014-11-28 14:42:25 +01003817 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3818 s->si[1].err_type = SI_ET_NONE;
3819 s->si[1].conn_retries = 0; /* used for logging too */
3820 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003821 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 +01003822 s->req.flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WAKE_CONNECT|CF_WROTE_DATA);
Willy Tarreauede3d882018-10-24 17:17:56 +02003823 s->res.flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA);
Willy Tarreaue7dff022015-04-03 01:14:29 +02003824 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3825 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3826 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003827
Patrick Hemmere3faf022018-08-22 10:02:00 -04003828 hlua_ctx_destroy(s->hlua);
3829 s->hlua = NULL;
3830
Willy Tarreaueee5b512015-04-03 23:46:31 +02003831 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003832 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003833 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003834
3835 if (prev_status == 401 || prev_status == 407) {
3836 /* In HTTP keep-alive mode, if we receive a 401, we still have
3837 * a chance of being able to send the visitor again to the same
3838 * server over the same connection. This is required by some
3839 * broken protocols such as NTLM, and anyway whenever there is
3840 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003841 * it's better to do it (at least it helps with debugging), at
3842 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003843 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003844 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003845 }
3846
Willy Tarreau53f96852016-02-02 18:50:47 +01003847 /* Never ever allow to reuse a connection from a non-reuse backend */
3848 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3849 srv_conn->flags |= CO_FL_PRIVATE;
3850
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003851 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003852 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003853
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003854 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003855 s->req.flags |= CF_NEVER_WAIT;
3856 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003857 }
3858
Willy Tarreau714ea782015-11-25 20:11:11 +01003859 /* we're removing the analysers, we MUST re-enable events detection.
3860 * We don't enable close on the response channel since it's either
3861 * already closed, or in keep-alive with an idle connection handler.
3862 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003863 channel_auto_read(&s->req);
3864 channel_auto_close(&s->req);
3865 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003866
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003867 /* we're in keep-alive with an idle connection, monitor it if not already done */
3868 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003869 srv = objt_server(srv_conn->target);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003870 if (srv) {
3871 if (srv_conn->flags & CO_FL_PRIVATE)
3872 LIST_ADD(&srv->priv_conns[tid], &srv_conn->list);
3873 else if (prev_flags & TX_NOT_FIRST)
3874 /* note: we check the request, not the connection, but
3875 * this is valid for strategies SAFE and AGGR, and in
3876 * case of ALWS, we don't care anyway.
3877 */
3878 LIST_ADD(&srv->safe_conns[tid], &srv_conn->list);
3879 else
3880 LIST_ADD(&srv->idle_conns[tid], &srv_conn->list);
3881 }
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003882 }
Christopher Faulete6006242017-03-10 11:52:44 +01003883 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3884 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003885}
3886
3887
3888/* This function updates the request state machine according to the response
3889 * state machine and buffer flags. It returns 1 if it changes anything (flag
3890 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3891 * it is only used to find when a request/response couple is complete. Both
3892 * this function and its equivalent should loop until both return zero. It
3893 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3894 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003895int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003896{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003897 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003898 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003899 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003900 unsigned int old_state = txn->req.msg_state;
3901
Christopher Faulet4be98032017-07-18 10:48:24 +02003902 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003903 return 0;
3904
3905 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003906 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003907 * We can shut the read side unless we want to abort_on_close,
3908 * or we have a POST request. The issue with POST requests is
3909 * that some browsers still send a CRLF after the request, and
3910 * this CRLF must be read so that it does not remain in the kernel
3911 * buffers, otherwise a close could cause an RST on some systems
3912 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003913 * Note that if we're using keep-alive on the client side, we'd
3914 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003915 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003916 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003917 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003918 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3919 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003920 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3921 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003922 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003923 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003924
Willy Tarreau40f151a2012-12-20 12:10:09 +01003925 /* if the server closes the connection, we want to immediately react
3926 * and close the socket to save packets and syscalls.
3927 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003928 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003929
Willy Tarreau7f876a12015-11-18 11:59:55 +01003930 /* In any case we've finished parsing the request so we must
3931 * disable Nagle when sending data because 1) we're not going
3932 * to shut this side, and 2) the server is waiting for us to
3933 * send pending data.
3934 */
3935 chn->flags |= CF_NEVER_WAIT;
3936
Willy Tarreau610ecce2010-01-04 21:15:02 +01003937 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3938 goto wait_other_side;
3939
3940 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3941 /* The server has not finished to respond, so we
3942 * don't want to move in order not to upset it.
3943 */
3944 goto wait_other_side;
3945 }
3946
Willy Tarreau610ecce2010-01-04 21:15:02 +01003947 /* When we get here, it means that both the request and the
3948 * response have finished receiving. Depending on the connection
3949 * mode, we'll have to wait for the last bytes to leave in either
3950 * direction, and sometimes for a close to be effective.
3951 */
3952
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003953 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3954 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003955 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3956 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003957 }
3958 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3959 /* Option forceclose is set, or either side wants to close,
3960 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003961 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003962 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003963 *
3964 * However, there is an exception if the response
3965 * length is undefined. In this case, we need to wait
3966 * the close from the server. The response will be
3967 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003968 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003969 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3970 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3971 goto check_channel_flags;
3972
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003973 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3974 channel_shutr_now(chn);
3975 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003976 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003977 }
3978 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003979 /* The last possible modes are keep-alive and tunnel. Tunnel mode
3980 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003981 */
Willy Tarreau4213a112013-12-15 10:25:42 +01003982 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
3983 channel_auto_read(chn);
3984 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003985 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003986 }
3987
Christopher Faulet4be98032017-07-18 10:48:24 +02003988 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003989 }
3990
3991 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3992 http_msg_closing:
3993 /* nothing else to forward, just waiting for the output buffer
3994 * to be empty and for the shutw_now to take effect.
3995 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003996 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003997 txn->req.msg_state = HTTP_MSG_CLOSED;
3998 goto http_msg_closed;
3999 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004000 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004001 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004002 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004003 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004004 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004005 }
4006
4007 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4008 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01004009 /* if we don't know whether the server will close, we need to hard close */
4010 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4011 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4012
Willy Tarreau3988d932013-12-27 23:03:08 +01004013 /* see above in MSG_DONE why we only do this in these states */
4014 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4015 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004016 (!(s->be->options & PR_O_ABRT_CLOSE) ||
4017 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004018 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004019 goto wait_other_side;
4020 }
4021
Christopher Faulet4be98032017-07-18 10:48:24 +02004022 check_channel_flags:
4023 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4024 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4025 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02004026 txn->req.msg_state = HTTP_MSG_CLOSING;
4027 goto http_msg_closing;
4028 }
4029
4030
Willy Tarreau610ecce2010-01-04 21:15:02 +01004031 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004032 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004033}
4034
4035
4036/* This function updates the response state machine according to the request
4037 * state machine and buffer flags. It returns 1 if it changes anything (flag
4038 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4039 * it is only used to find when a request/response couple is complete. Both
4040 * this function and its equivalent should loop until both return zero. It
4041 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4042 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004043int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004044{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004045 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004046 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004047 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004048 unsigned int old_state = txn->rsp.msg_state;
4049
Christopher Faulet4be98032017-07-18 10:48:24 +02004050 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004051 return 0;
4052
4053 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4054 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004055 * still monitor the server connection for a possible close
4056 * while the request is being uploaded, so we don't disable
4057 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004058 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004059 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004060
4061 if (txn->req.msg_state == HTTP_MSG_ERROR)
4062 goto wait_other_side;
4063
4064 if (txn->req.msg_state < HTTP_MSG_DONE) {
4065 /* The client seems to still be sending data, probably
4066 * because we got an error response during an upload.
4067 * We have the choice of either breaking the connection
4068 * or letting it pass through. Let's do the later.
4069 */
4070 goto wait_other_side;
4071 }
4072
Willy Tarreau610ecce2010-01-04 21:15:02 +01004073 /* When we get here, it means that both the request and the
4074 * response have finished receiving. Depending on the connection
4075 * mode, we'll have to wait for the last bytes to leave in either
4076 * direction, and sometimes for a close to be effective.
4077 */
4078
4079 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4080 /* Server-close mode : shut read and wait for the request
4081 * side to close its output buffer. The caller will detect
4082 * when we're in DONE and the other is in CLOSED and will
4083 * catch that for the final cleanup.
4084 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004085 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4086 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004087 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004088 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4089 /* Option forceclose is set, or either side wants to close,
4090 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004091 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004092 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004093 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01004094 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004095 channel_shutr_now(chn);
4096 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004097 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004098 }
4099 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004100 /* The last possible modes are keep-alive and tunnel. Tunnel will
4101 * need to forward remaining data. Keep-alive will need to monitor
4102 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004103 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004104 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004105 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004106 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4107 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004108 }
4109
Christopher Faulet4be98032017-07-18 10:48:24 +02004110 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004111 }
4112
4113 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4114 http_msg_closing:
4115 /* nothing else to forward, just waiting for the output buffer
4116 * to be empty and for the shutw_now to take effect.
4117 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004118 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004119 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4120 goto http_msg_closed;
4121 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004122 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004123 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004124 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004125 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004126 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004127 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004128 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004129 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004130 }
4131
4132 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4133 http_msg_closed:
4134 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004135 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004136 channel_auto_close(chn);
4137 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004138 goto wait_other_side;
4139 }
4140
Christopher Faulet4be98032017-07-18 10:48:24 +02004141 check_channel_flags:
4142 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4143 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4144 /* if we've just closed an output, let's switch */
4145 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4146 goto http_msg_closing;
4147 }
4148
Willy Tarreau610ecce2010-01-04 21:15:02 +01004149 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004150 /* We force the response to leave immediately if we're waiting for the
4151 * other side, since there is no pending shutdown to push it out.
4152 */
4153 if (!channel_is_empty(chn))
4154 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004155 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004156}
4157
4158
Christopher Faulet894da4c2017-07-18 11:29:07 +02004159/* Resync the request and response state machines. */
4160void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004161{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004162 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004163#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004164 int old_req_state = txn->req.msg_state;
4165 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004166#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004167
Willy Tarreau610ecce2010-01-04 21:15:02 +01004168 http_sync_req_state(s);
4169 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004170 if (!http_sync_res_state(s))
4171 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004172 if (!http_sync_req_state(s))
4173 break;
4174 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004175
Christopher Faulet894da4c2017-07-18 11:29:07 +02004176 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4177 "req->analysers=0x%08x res->analysers=0x%08x\n",
4178 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004179 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4180 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004181 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004182
4183
Willy Tarreau610ecce2010-01-04 21:15:02 +01004184 /* OK, both state machines agree on a compatible state.
4185 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004186 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4187 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004188 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4189 * means we must abort the request.
4190 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4191 * corresponding channel.
4192 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4193 * on the response with server-close mode means we've completed one
4194 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004195 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004196 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4197 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004198 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004199 channel_auto_close(&s->req);
4200 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004201 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004202 channel_auto_close(&s->res);
4203 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004204 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004205 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4206 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004207 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004208 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004209 channel_auto_close(&s->res);
4210 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004211 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004212 channel_abort(&s->req);
4213 channel_auto_close(&s->req);
4214 channel_auto_read(&s->req);
4215 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004216 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004217 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4218 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4219 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4220 s->req.analysers &= AN_REQ_FLT_END;
4221 if (HAS_REQ_DATA_FILTERS(s))
4222 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4223 }
4224 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4225 s->res.analysers &= AN_RES_FLT_END;
4226 if (HAS_RSP_DATA_FILTERS(s))
4227 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4228 }
4229 channel_auto_close(&s->req);
4230 channel_auto_read(&s->req);
4231 channel_auto_close(&s->res);
4232 channel_auto_read(&s->res);
4233 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004234 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4235 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004236 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004237 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4238 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4239 /* server-close/keep-alive: terminate this transaction,
4240 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004241 * a fresh-new transaction, but only once we're sure there's
4242 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004243 * another request. They must not hold any pending output data
4244 * and the response buffer must realigned
4245 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004246 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004247 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004248 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004249 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004250 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004251 else {
4252 s->req.analysers = AN_REQ_FLT_END;
4253 s->res.analysers = AN_RES_FLT_END;
4254 txn->flags |= TX_WAIT_CLEANUP;
4255 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004256 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004257}
4258
Willy Tarreaud98cf932009-12-27 22:54:55 +01004259/* This function is an analyser which forwards request body (including chunk
4260 * sizes if any). It is called as soon as we must forward, even if we forward
4261 * zero byte. The only situation where it must not be called is when we're in
4262 * tunnel mode and we want to forward till the close. It's used both to forward
4263 * remaining data and to resync after end of body. It expects the msg_state to
4264 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004265 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004266 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004267 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004268 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004269int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004270{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004271 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004272 struct http_txn *txn = s->txn;
4273 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004274 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004275
Christopher Faulete0768eb2018-10-03 16:38:02 +02004276 if (IS_HTX_STRM(s))
4277 return htx_request_forward_body(s, req, an_bit);
4278
Christopher Faulet45073512018-07-20 10:16:29 +02004279 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 +02004280 now_ms, __FUNCTION__,
4281 s,
4282 req,
4283 req->rex, req->wex,
4284 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004285 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004286 req->analysers);
4287
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004288 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4289 return 0;
4290
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004291 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004292 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004293 /* Output closed while we were sending data. We must abort and
4294 * wake the other side up.
4295 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004296 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004297 msg->msg_state = HTTP_MSG_ERROR;
4298 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004299 return 1;
4300 }
4301
Willy Tarreaud98cf932009-12-27 22:54:55 +01004302 /* Note that we don't have to send 100-continue back because we don't
4303 * need the data to complete our job, and it's up to the server to
4304 * decide whether to return 100, 417 or anything else in return of
4305 * an "Expect: 100-continue" header.
4306 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004307 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004308 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4309 ? HTTP_MSG_CHUNK_SIZE
4310 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004311
4312 /* TODO/filters: when http-buffer-request option is set or if a
4313 * rule on url_param exists, the first chunk size could be
4314 * already parsed. In that case, msg->next is after the chunk
4315 * size (including the CRLF after the size). So this case should
4316 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004317 }
4318
Willy Tarreau7ba23542014-04-17 21:50:00 +02004319 /* Some post-connect processing might want us to refrain from starting to
4320 * forward data. Currently, the only reason for this is "balance url_param"
4321 * whichs need to parse/process the request after we've enabled forwarding.
4322 */
4323 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004324 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004325 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004326 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004327 channel_dont_close(req); /* don't fail on early shutr */
4328 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004329 }
4330 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4331 }
4332
Willy Tarreau80a92c02014-03-12 10:41:13 +01004333 /* in most states, we should abort in case of early close */
4334 channel_auto_close(req);
4335
Willy Tarreauefdf0942014-04-24 20:08:57 +02004336 if (req->to_forward) {
4337 /* We can't process the buffer's contents yet */
4338 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004339 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004340 }
4341
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004342 if (msg->msg_state < HTTP_MSG_DONE) {
4343 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4344 ? http_msg_forward_chunked_body(s, msg)
4345 : http_msg_forward_body(s, msg));
4346 if (!ret)
4347 goto missing_data_or_waiting;
4348 if (ret < 0)
4349 goto return_bad_req;
4350 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004351
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004352 /* other states, DONE...TUNNEL */
4353 /* we don't want to forward closes on DONE except in tunnel mode. */
4354 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4355 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004356
Christopher Faulet894da4c2017-07-18 11:29:07 +02004357 http_resync_states(s);
4358 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004359 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4360 if (req->flags & CF_SHUTW) {
4361 /* request errors are most likely due to the
4362 * server aborting the transfer. */
4363 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004364 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004365 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004366 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004367 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004368 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004369 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004370 }
4371
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004372 /* If "option abortonclose" is set on the backend, we want to monitor
4373 * the client's connection and forward any shutdown notification to the
4374 * server, which will decide whether to close or to go on processing the
4375 * request. We only do that in tunnel mode, and not in other modes since
4376 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004377 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004378 channel_auto_read(req);
4379 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4380 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4381 s->si[1].flags |= SI_FL_NOLINGER;
4382 channel_auto_close(req);
4383 }
4384 else if (s->txn->meth == HTTP_METH_POST) {
4385 /* POST requests may require to read extra CRLF sent by broken
4386 * browsers and which could cause an RST to be sent upon close
4387 * on some systems (eg: Linux). */
4388 channel_auto_read(req);
4389 }
4390 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004391
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004392 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004393 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004394 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004395 if (!(s->flags & SF_ERR_MASK))
4396 s->flags |= SF_ERR_CLICL;
4397 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004398 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004399 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004400 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004401 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004402 }
4403
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004404 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4405 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004406 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004407 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004408
4409 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004410 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004411
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004412 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004413 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004414 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004415 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004416
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004417 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004418 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004419 * And when content-length is used, we never want to let the possible
4420 * shutdown be forwarded to the other side, as the state machine will
4421 * take care of it once the client responds. It's also important to
4422 * prevent TIME_WAITs from accumulating on the backend side, and for
4423 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004424 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004425 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004426 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004427
Willy Tarreau5c620922011-05-11 19:56:11 +02004428 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004429 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004430 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004431 * modes are already handled by the stream sock layer. We must not do
4432 * this in content-length mode because it could present the MSG_MORE
4433 * flag with the last block of forwarded data, which would cause an
4434 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004435 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004436 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004437 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004438
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004439 return 0;
4440
Willy Tarreaud98cf932009-12-27 22:54:55 +01004441 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004442 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004443 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004444 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004445
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004446 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004447 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004448 txn->req.msg_state = HTTP_MSG_ERROR;
4449 if (txn->status) {
4450 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004451 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004452 } else {
4453 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004454 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004455 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004456 req->analysers &= AN_REQ_FLT_END;
4457 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 +01004458
Willy Tarreaue7dff022015-04-03 01:14:29 +02004459 if (!(s->flags & SF_ERR_MASK))
4460 s->flags |= SF_ERR_PRXCOND;
4461 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004462 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004463 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004464 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004465 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004466 }
4467 return 0;
4468
4469 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004470 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004471 txn->req.msg_state = HTTP_MSG_ERROR;
4472 if (txn->status) {
4473 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004474 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004475 } else {
4476 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004477 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004478 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004479 req->analysers &= AN_REQ_FLT_END;
4480 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 +01004481
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004482 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4483 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004484 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004485 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004486
Willy Tarreaue7dff022015-04-03 01:14:29 +02004487 if (!(s->flags & SF_ERR_MASK))
4488 s->flags |= SF_ERR_SRVCL;
4489 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004490 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004491 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004492 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004493 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004494 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004495 return 0;
4496}
4497
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004498/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4499 * processing can continue on next analysers, or zero if it either needs more
4500 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004501 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004502 * when it has nothing left to do, and may remove any analyser when it wants to
4503 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004504 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004505int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004506{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004507 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004508 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004509 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004510 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004511 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004512 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004513 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004514 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004515
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004516 srv_conn = cs_conn(objt_cs(s->si[1].end));
4517
Christopher Faulete0768eb2018-10-03 16:38:02 +02004518 if (IS_HTX_STRM(s))
4519 return htx_wait_for_response(s, rep, an_bit);
4520
Christopher Faulet45073512018-07-20 10:16:29 +02004521 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 +02004522 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004523 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004524 rep,
4525 rep->rex, rep->wex,
4526 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004527 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004528 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004529
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004530 /*
4531 * Now parse the partial (or complete) lines.
4532 * We will check the response syntax, and also join multi-line
4533 * headers. An index of all the lines will be elaborated while
4534 * parsing.
4535 *
4536 * For the parsing, we use a 28 states FSM.
4537 *
4538 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004539 * ci_head(rep) = beginning of response
4540 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4541 * ci_tail(rep) = end of input data
4542 * msg->eol = end of current header or line (LF or CRLF)
4543 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004544 */
4545
Willy Tarreau628c40c2014-04-24 19:11:26 +02004546 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004547 /* There's a protected area at the end of the buffer for rewriting
4548 * purposes. We don't want to start to parse the request if the
4549 * protected area is affected, because we may have to move processed
4550 * data later, which is much more complicated.
4551 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004552 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004553 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004554 /* some data has still not left the buffer, wake us once that's done */
4555 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4556 goto abort_response;
4557 channel_dont_close(rep);
4558 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004559 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004560 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004561 }
4562
Willy Tarreau188e2302018-06-15 11:11:53 +02004563 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004564 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004565 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004566
Willy Tarreauf37954d2018-06-15 18:31:02 +02004567 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004568 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004569 }
4570
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004571 /* 1: we might have to print this header in debug mode */
4572 if (unlikely((global.mode & MODE_DEBUG) &&
4573 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004574 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004575 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004576
Willy Tarreauf37954d2018-06-15 18:31:02 +02004577 sol = ci_head(rep);
4578 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004579 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004580
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004581 sol += hdr_idx_first_pos(&txn->hdr_idx);
4582 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004583
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004584 while (cur_idx) {
4585 eol = sol + txn->hdr_idx.v[cur_idx].len;
4586 debug_hdr("srvhdr", s, sol, eol);
4587 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4588 cur_idx = txn->hdr_idx.v[cur_idx].next;
4589 }
4590 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004591
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004592 /*
4593 * Now we quickly check if we have found a full valid response.
4594 * If not so, we check the FD and buffer states before leaving.
4595 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004596 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004597 * responses are checked first.
4598 *
4599 * Depending on whether the client is still there or not, we
4600 * may send an error response back or not. Note that normally
4601 * we should only check for HTTP status there, and check I/O
4602 * errors somewhere else.
4603 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004604
Willy Tarreau655dce92009-11-08 13:10:58 +01004605 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004606 /* Invalid response */
4607 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4608 /* we detected a parsing error. We want to archive this response
4609 * in the dedicated proxy area for later troubleshooting.
4610 */
4611 hdr_response_bad:
4612 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004613 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004614
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004615 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004616 if (objt_server(s->target)) {
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004617 HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
4618 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004619 }
Willy Tarreau64648412010-03-05 10:41:54 +01004620 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004621 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004622 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004623 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004624 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004625 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004626 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004627
Willy Tarreaue7dff022015-04-03 01:14:29 +02004628 if (!(s->flags & SF_ERR_MASK))
4629 s->flags |= SF_ERR_PRXCOND;
4630 if (!(s->flags & SF_FINST_MASK))
4631 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004632
4633 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004634 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004635
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004636 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004637 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004638 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004639 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004640 goto hdr_response_bad;
4641 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004642
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004643 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004644 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004645 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004646 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004647 else if (txn->flags & TX_NOT_FIRST)
4648 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004649
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004650 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004651 if (objt_server(s->target)) {
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004652 HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
4653 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004654 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004655
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004656 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004657 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004658 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004659
4660 /* Check to see if the server refused the early data.
4661 * If so, just send a 425
4662 */
4663 if (objt_cs(s->si[1].end)) {
4664 struct connection *conn = objt_cs(s->si[1].end)->conn;
4665
4666 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4667 txn->status = 425;
4668 }
4669
Willy Tarreau350f4872014-11-28 14:42:25 +01004670 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004671 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004672 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004673
Willy Tarreaue7dff022015-04-03 01:14:29 +02004674 if (!(s->flags & SF_ERR_MASK))
4675 s->flags |= SF_ERR_SRVCL;
4676 if (!(s->flags & SF_FINST_MASK))
4677 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004678 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004679 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004680
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004681 /* read timeout : return a 504 to the client. */
4682 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004683 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004684 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004685
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004686 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004687 if (objt_server(s->target)) {
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004688 HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
4689 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004690 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004691
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004692 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004693 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004694 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004695 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004696 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004697 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004698
Willy Tarreaue7dff022015-04-03 01:14:29 +02004699 if (!(s->flags & SF_ERR_MASK))
4700 s->flags |= SF_ERR_SRVTO;
4701 if (!(s->flags & SF_FINST_MASK))
4702 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004703 return 0;
4704 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004705
Willy Tarreauf003d372012-11-26 13:35:37 +01004706 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004707 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004708 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4709 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004710 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004711 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004712
Christopher Faulet0184ea72017-01-05 14:06:34 +01004713 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004714 channel_auto_close(rep);
4715
4716 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004717 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004718 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004719
Willy Tarreaue7dff022015-04-03 01:14:29 +02004720 if (!(s->flags & SF_ERR_MASK))
4721 s->flags |= SF_ERR_CLICL;
4722 if (!(s->flags & SF_FINST_MASK))
4723 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004724
Willy Tarreau87b09662015-04-03 00:22:06 +02004725 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004726 return 0;
4727 }
4728
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004729 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004730 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004731 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004732 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004733 else if (txn->flags & TX_NOT_FIRST)
4734 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004735
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004736 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004737 if (objt_server(s->target)) {
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004738 HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
4739 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004740 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004741
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004742 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004743 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004744 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004745 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004746 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004747 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004748
Willy Tarreaue7dff022015-04-03 01:14:29 +02004749 if (!(s->flags & SF_ERR_MASK))
4750 s->flags |= SF_ERR_SRVCL;
4751 if (!(s->flags & SF_FINST_MASK))
4752 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004753 return 0;
4754 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004755
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004756 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004757 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004758 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004759 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004760 else if (txn->flags & TX_NOT_FIRST)
4761 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004762
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004763 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004764 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004765 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004766
Willy Tarreaue7dff022015-04-03 01:14:29 +02004767 if (!(s->flags & SF_ERR_MASK))
4768 s->flags |= SF_ERR_CLICL;
4769 if (!(s->flags & SF_FINST_MASK))
4770 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004771
Willy Tarreau87b09662015-04-03 00:22:06 +02004772 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004773 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004774 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004775
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004776 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004777 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004778 return 0;
4779 }
4780
4781 /* More interesting part now : we know that we have a complete
4782 * response which at least looks like HTTP. We have an indicator
4783 * of each header's length, so we can parse them quickly.
4784 */
4785
4786 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004787 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004788
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004789 /*
4790 * 1: get the status code
4791 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004792 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004793 if (n < 1 || n > 5)
4794 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004795 /* when the client triggers a 4xx from the server, it's most often due
4796 * to a missing object or permission. These events should be tracked
4797 * because if they happen often, it may indicate a brute force or a
4798 * vulnerability scan.
4799 */
4800 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004801 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004802
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004803 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004804 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004805
Willy Tarreau91852eb2015-05-01 13:26:00 +02004806 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4807 * exactly one digit "." one digit. This check may be disabled using
4808 * option accept-invalid-http-response.
4809 */
4810 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4811 if (msg->sl.st.v_l != 8) {
4812 msg->err_pos = 0;
4813 goto hdr_response_bad;
4814 }
4815
Willy Tarreauf37954d2018-06-15 18:31:02 +02004816 if (ci_head(rep)[4] != '/' ||
4817 !isdigit((unsigned char)ci_head(rep)[5]) ||
4818 ci_head(rep)[6] != '.' ||
4819 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004820 msg->err_pos = 4;
4821 goto hdr_response_bad;
4822 }
4823 }
4824
Willy Tarreau5b154472009-12-21 20:11:07 +01004825 /* check if the response is HTTP/1.1 or above */
4826 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004827 ((ci_head(rep)[5] > '1') ||
4828 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004829 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004830
4831 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004832 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 +01004833
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004834 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004835 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004836
Willy Tarreauf37954d2018-06-15 18:31:02 +02004837 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004838
Willy Tarreau39650402010-03-15 19:44:39 +01004839 /* Adjust server's health based on status code. Note: status codes 501
4840 * and 505 are triggered on demand by client request, so we must not
4841 * count them as server failures.
4842 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004843 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004844 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004845 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004846 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004847 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004848 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004849
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004850 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004851 * We may be facing a 100-continue response, or any other informational
4852 * 1xx response which is non-final, in which case this is not the right
4853 * response, and we're waiting for the next one. Let's allow this response
4854 * to go to the client and wait for the next one. There's an exception for
4855 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004856 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004857 if (txn->status < 200 &&
4858 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004859 hdr_idx_init(&txn->hdr_idx);
4860 msg->next -= channel_forward(rep, msg->next);
4861 msg->msg_state = HTTP_MSG_RPBEFORE;
4862 txn->status = 0;
4863 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004864 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004865 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004866 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004867
Willy Tarreaua14ad722017-07-07 11:36:32 +02004868 /*
4869 * 2: check for cacheability.
4870 */
4871
4872 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004873 case 200:
4874 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004875 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004876 case 206:
4877 case 300:
4878 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004879 case 404:
4880 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004881 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004882 case 414:
4883 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004884 break;
4885 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004886 /* RFC7231#6.1:
4887 * Responses with status codes that are defined as
4888 * cacheable by default (e.g., 200, 203, 204, 206,
4889 * 300, 301, 404, 405, 410, 414, and 501 in this
4890 * specification) can be reused by a cache with
4891 * heuristic expiration unless otherwise indicated
4892 * by the method definition or explicit cache
4893 * controls [RFC7234]; all other status codes are
4894 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004895 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004896 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004897 break;
4898 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004899
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004900 /*
4901 * 3: we may need to capture headers
4902 */
4903 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004904 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004905 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4906 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004907
Willy Tarreau557f1992015-05-01 10:05:17 +02004908 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4909 * by RFC7230#3.3.3 :
4910 *
4911 * The length of a message body is determined by one of the following
4912 * (in order of precedence):
4913 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004914 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4915 * the connection will become a tunnel immediately after the empty
4916 * line that concludes the header fields. A client MUST ignore
4917 * any Content-Length or Transfer-Encoding header fields received
4918 * in such a message. Any 101 response (Switching Protocols) is
4919 * managed in the same manner.
4920 *
4921 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004922 * (Informational), 204 (No Content), or 304 (Not Modified) status
4923 * code is always terminated by the first empty line after the
4924 * header fields, regardless of the header fields present in the
4925 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004926 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004927 * 3. If a Transfer-Encoding header field is present and the chunked
4928 * transfer coding (Section 4.1) is the final encoding, the message
4929 * body length is determined by reading and decoding the chunked
4930 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004931 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004932 * If a Transfer-Encoding header field is present in a response and
4933 * the chunked transfer coding is not the final encoding, the
4934 * message body length is determined by reading the connection until
4935 * it is closed by the server. If a Transfer-Encoding header field
4936 * is present in a request and the chunked transfer coding is not
4937 * the final encoding, the message body length cannot be determined
4938 * reliably; the server MUST respond with the 400 (Bad Request)
4939 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004940 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004941 * If a message is received with both a Transfer-Encoding and a
4942 * Content-Length header field, the Transfer-Encoding overrides the
4943 * Content-Length. Such a message might indicate an attempt to
4944 * perform request smuggling (Section 9.5) or response splitting
4945 * (Section 9.4) and ought to be handled as an error. A sender MUST
4946 * remove the received Content-Length field prior to forwarding such
4947 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004948 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004949 * 4. If a message is received without Transfer-Encoding and with
4950 * either multiple Content-Length header fields having differing
4951 * field-values or a single Content-Length header field having an
4952 * invalid value, then the message framing is invalid and the
4953 * recipient MUST treat it as an unrecoverable error. If this is a
4954 * request message, the server MUST respond with a 400 (Bad Request)
4955 * status code and then close the connection. If this is a response
4956 * message received by a proxy, the proxy MUST close the connection
4957 * to the server, discard the received response, and send a 502 (Bad
4958 * Gateway) response to the client. If this is a response message
4959 * received by a user agent, the user agent MUST close the
4960 * connection to the server and discard the received response.
4961 *
4962 * 5. If a valid Content-Length header field is present without
4963 * Transfer-Encoding, its decimal value defines the expected message
4964 * body length in octets. If the sender closes the connection or
4965 * the recipient times out before the indicated number of octets are
4966 * received, the recipient MUST consider the message to be
4967 * incomplete and close the connection.
4968 *
4969 * 6. If this is a request message and none of the above are true, then
4970 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004971 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004972 * 7. Otherwise, this is a response message without a declared message
4973 * body length, so the message body length is determined by the
4974 * number of octets received prior to the server closing the
4975 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004976 */
4977
4978 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004979 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004980 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004981 * FIXME: should we parse anyway and return an error on chunked encoding ?
4982 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004983 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4984 txn->status == 101)) {
4985 /* Either we've established an explicit tunnel, or we're
4986 * switching the protocol. In both cases, we're very unlikely
4987 * to understand the next protocols. We have to switch to tunnel
4988 * mode, so that we transfer the request and responses then let
4989 * this protocol pass unmodified. When we later implement specific
4990 * parsers for such protocols, we'll want to check the Upgrade
4991 * header which contains information about that protocol for
4992 * responses with status 101 (eg: see RFC2817 about TLS).
4993 */
4994 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4995 msg->flags |= HTTP_MSGF_XFER_LEN;
4996 goto end;
4997 }
4998
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004999 if (txn->meth == HTTP_METH_HEAD ||
5000 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005001 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005002 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005003 goto skip_content_length;
5004 }
5005
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005006 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005007 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005008 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005009 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005010 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5011 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005012 /* bad transfer-encoding (chunked followed by something else) */
5013 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005014 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005015 break;
5016 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005017 }
5018
Willy Tarreau1c913912015-04-30 10:57:51 +02005019 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005020 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005021 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005022 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02005023 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5024 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02005025 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005026 signed long long cl;
5027
Willy Tarreauad14f752011-09-02 20:33:27 +02005028 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005029 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005030 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005031 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005032
Willy Tarreauad14f752011-09-02 20:33:27 +02005033 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005034 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005035 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005036 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005037
Willy Tarreauad14f752011-09-02 20:33:27 +02005038 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005039 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005040 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005041 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005042
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005043 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005044 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005045 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005046 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005047
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005048 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005049 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005050 }
5051
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02005052 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
5053 * 407 (Proxy-Authenticate) responses and set the connection to private
5054 */
5055 if (srv_conn && txn->status == 401) {
5056 /* check for Negotiate/NTLM WWW-Authenticate headers */
5057 ctx.idx = 0;
5058 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
5059 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5060 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5061 srv_conn->flags |= CO_FL_PRIVATE;
5062 }
5063 } else if (srv_conn && txn->status == 407) {
5064 /* check for Negotiate/NTLM Proxy-Authenticate headers */
5065 ctx.idx = 0;
5066 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
5067 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
5068 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
5069 srv_conn->flags |= CO_FL_PRIVATE;
5070 }
5071 }
5072
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005073 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005074 /* Now we have to check if we need to modify the Connection header.
5075 * This is more difficult on the response than it is on the request,
5076 * because we can have two different HTTP versions and we don't know
5077 * how the client will interprete a response. For instance, let's say
5078 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5079 * HTTP/1.1 response without any header. Maybe it will bound itself to
5080 * HTTP/1.0 because it only knows about it, and will consider the lack
5081 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5082 * the lack of header as a keep-alive. Thus we will use two flags
5083 * indicating how a request MAY be understood by the client. In case
5084 * of multiple possibilities, we'll fix the header to be explicit. If
5085 * ambiguous cases such as both close and keepalive are seen, then we
5086 * will fall back to explicit close. Note that we won't take risks with
5087 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005088 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005089 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005090 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005091 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01005092 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005093
Willy Tarreau60466522010-01-18 19:08:45 +01005094 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005095 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01005096 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005097
Willy Tarreau60466522010-01-18 19:08:45 +01005098 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02005099 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01005100 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005101 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005102 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005103 }
Willy Tarreau60466522010-01-18 19:08:45 +01005104 else { /* SCL / KAL */
5105 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005106 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005107 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005108 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005109
Willy Tarreau60466522010-01-18 19:08:45 +01005110 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005111 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005112
Willy Tarreau60466522010-01-18 19:08:45 +01005113 /* Some keep-alive responses are converted to Server-close if
5114 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005115 */
Willy Tarreau60466522010-01-18 19:08:45 +01005116 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5117 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005118 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005119 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005120 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005121 }
5122
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005123 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005124 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005125 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005126
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005127 /* end of job, return OK */
5128 rep->analysers &= ~an_bit;
5129 rep->analyse_exp = TICK_ETERNITY;
5130 channel_auto_close(rep);
5131 return 1;
5132
5133 abort_keep_alive:
5134 /* A keep-alive request to the server failed on a network error.
5135 * The client is required to retry. We need to close without returning
5136 * any other information so that the client retries.
5137 */
5138 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005139 rep->analysers &= AN_RES_FLT_END;
5140 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005141 channel_auto_close(rep);
5142 s->logs.logwait = 0;
5143 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005144 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005145 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005146 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005147 return 0;
5148}
5149
5150/* This function performs all the processing enabled for the current response.
5151 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005152 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005153 * other functions. It works like process_request (see indications above).
5154 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005155int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005156{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005157 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005158 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005159 struct http_msg *msg = &txn->rsp;
5160 struct proxy *cur_proxy;
5161 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005162 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005163
Christopher Faulete0768eb2018-10-03 16:38:02 +02005164 if (IS_HTX_STRM(s))
5165 return htx_process_res_common(s, rep, an_bit, px);
5166
Christopher Faulet45073512018-07-20 10:16:29 +02005167 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 +02005168 now_ms, __FUNCTION__,
5169 s,
5170 rep,
5171 rep->rex, rep->wex,
5172 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005173 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005174 rep->analysers);
5175
5176 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5177 return 0;
5178
Willy Tarreau70730dd2014-04-24 18:06:27 +02005179 /* The stats applet needs to adjust the Connection header but we don't
5180 * apply any filter there.
5181 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005182 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5183 rep->analysers &= ~an_bit;
5184 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005185 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005186 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005187
Willy Tarreau58975672014-04-24 21:13:57 +02005188 /*
5189 * We will have to evaluate the filters.
5190 * As opposed to version 1.2, now they will be evaluated in the
5191 * filters order and not in the header order. This means that
5192 * each filter has to be validated among all headers.
5193 *
5194 * Filters are tried with ->be first, then with ->fe if it is
5195 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005196 *
5197 * Maybe we are in resume condiion. In this case I choose the
5198 * "struct proxy" which contains the rule list matching the resume
5199 * pointer. If none of theses "struct proxy" match, I initialise
5200 * the process with the first one.
5201 *
5202 * In fact, I check only correspondance betwwen the current list
5203 * pointer and the ->fe rule list. If it doesn't match, I initialize
5204 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005205 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005206 if (s->current_rule_list == &sess->fe->http_res_rules)
5207 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005208 else
5209 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005210 while (1) {
5211 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005212
Willy Tarreau58975672014-04-24 21:13:57 +02005213 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005214 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005215 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005216
Willy Tarreau51d861a2015-05-22 17:30:48 +02005217 if (ret == HTTP_RULE_RES_BADREQ)
5218 goto return_srv_prx_502;
5219
5220 if (ret == HTTP_RULE_RES_DONE) {
5221 rep->analysers &= ~an_bit;
5222 rep->analyse_exp = TICK_ETERNITY;
5223 return 1;
5224 }
5225 }
5226
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005227 /* we need to be called again. */
5228 if (ret == HTTP_RULE_RES_YIELD) {
5229 channel_dont_close(rep);
5230 return 0;
5231 }
5232
Willy Tarreau58975672014-04-24 21:13:57 +02005233 /* try headers filters */
5234 if (rule_set->rsp_exp != NULL) {
5235 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5236 return_bad_resp:
5237 if (objt_server(s->target)) {
Willy Tarreaub54c40a2018-12-02 19:28:41 +01005238 HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
5239 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005240 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005241 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005242 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005243 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005244 txn->status = 502;
5245 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005246 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005247 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005248 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005249 if (!(s->flags & SF_ERR_MASK))
5250 s->flags |= SF_ERR_PRXCOND;
5251 if (!(s->flags & SF_FINST_MASK))
5252 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005253 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005254 }
Willy Tarreau58975672014-04-24 21:13:57 +02005255 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005256
Willy Tarreau58975672014-04-24 21:13:57 +02005257 /* has the response been denied ? */
5258 if (txn->flags & TX_SVDENY) {
5259 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005260 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005261
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005262 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5263 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005264 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005265 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005266
Willy Tarreau58975672014-04-24 21:13:57 +02005267 goto return_srv_prx_502;
5268 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005269
Willy Tarreau58975672014-04-24 21:13:57 +02005270 /* add response headers from the rule sets in the same order */
5271 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005272 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005273 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005274 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005275 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005276 ret = acl_pass(ret);
5277 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5278 ret = !ret;
5279 if (!ret)
5280 continue;
5281 }
Christopher Faulet10079f52018-10-03 15:17:28 +02005282 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005283 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005284 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005285
Willy Tarreau58975672014-04-24 21:13:57 +02005286 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005287 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005288 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005289 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005290 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005291
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005292 /* After this point, this anayzer can't return yield, so we can
5293 * remove the bit corresponding to this analyzer from the list.
5294 *
5295 * Note that the intermediate returns and goto found previously
5296 * reset the analyzers.
5297 */
5298 rep->analysers &= ~an_bit;
5299 rep->analyse_exp = TICK_ETERNITY;
5300
Willy Tarreau58975672014-04-24 21:13:57 +02005301 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005302 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005303 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005304
Willy Tarreau58975672014-04-24 21:13:57 +02005305 /*
5306 * Now check for a server cookie.
5307 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005308 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005309 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005310
Willy Tarreau58975672014-04-24 21:13:57 +02005311 /*
5312 * Check for cache-control or pragma headers if required.
5313 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005314 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005315 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005316
Willy Tarreau58975672014-04-24 21:13:57 +02005317 /*
5318 * Add server cookie in the response if needed
5319 */
5320 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5321 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005322 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005323 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5324 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5325 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5326 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5327 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005328 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005329 /* the server is known, it's not the one the client requested, or the
5330 * cookie's last seen date needs to be refreshed. We have to
5331 * insert a set-cookie here, except if we want to insert only on POST
5332 * requests and this one isn't. Note that servers which don't have cookies
5333 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005334 */
Willy Tarreau58975672014-04-24 21:13:57 +02005335 if (!objt_server(s->target)->cookie) {
5336 chunk_printf(&trash,
5337 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5338 s->be->cookie_name);
5339 }
5340 else {
5341 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005342
Willy Tarreau58975672014-04-24 21:13:57 +02005343 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5344 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005345 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5346 s30tob64((date.tv_sec+3) >> 2,
5347 trash.area + trash.data);
5348 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005349
Willy Tarreau58975672014-04-24 21:13:57 +02005350 if (s->be->cookie_maxlife) {
5351 /* emit first_date, which is either the original one or
5352 * the current date.
5353 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005354 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005355 s30tob64(txn->cookie_first_date ?
5356 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005357 (date.tv_sec+3) >> 2,
5358 trash.area + trash.data);
5359 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005360 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005361 }
Willy Tarreau58975672014-04-24 21:13:57 +02005362 chunk_appendf(&trash, "; path=/");
5363 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005364
Willy Tarreau58975672014-04-24 21:13:57 +02005365 if (s->be->cookie_domain)
5366 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005367
Willy Tarreau58975672014-04-24 21:13:57 +02005368 if (s->be->ck_opts & PR_CK_HTTPONLY)
5369 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005370
Willy Tarreau58975672014-04-24 21:13:57 +02005371 if (s->be->ck_opts & PR_CK_SECURE)
5372 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005373
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005374 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005375 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005376
Willy Tarreau58975672014-04-24 21:13:57 +02005377 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005378 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005379 /* the server did not change, only the date was updated */
5380 txn->flags |= TX_SCK_UPDATED;
5381 else
5382 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005383
Willy Tarreau58975672014-04-24 21:13:57 +02005384 /* Here, we will tell an eventual cache on the client side that we don't
5385 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5386 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5387 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005388 */
Willy Tarreau58975672014-04-24 21:13:57 +02005389 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005390
Willy Tarreau58975672014-04-24 21:13:57 +02005391 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005392
Willy Tarreau58975672014-04-24 21:13:57 +02005393 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5394 "Cache-control: private", 22) < 0))
5395 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005396 }
Willy Tarreau58975672014-04-24 21:13:57 +02005397 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005398
Willy Tarreau58975672014-04-24 21:13:57 +02005399 /*
5400 * Check if result will be cacheable with a cookie.
5401 * We'll block the response if security checks have caught
5402 * nasty things such as a cacheable cookie.
5403 */
5404 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5405 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5406 (s->be->options & PR_O_CHK_CACHE)) {
5407 /* we're in presence of a cacheable response containing
5408 * a set-cookie header. We'll block it as requested by
5409 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005410 */
Willy Tarreau58975672014-04-24 21:13:57 +02005411 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005412 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005413
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005414 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5415 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005416 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005417 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005418
Christopher Faulet767a84b2017-11-24 16:50:31 +01005419 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5420 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005421 send_log(s->be, LOG_ALERT,
5422 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5423 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5424 goto return_srv_prx_502;
5425 }
Willy Tarreau03945942009-12-22 16:50:27 +01005426
Willy Tarreau70730dd2014-04-24 18:06:27 +02005427 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005428 /*
5429 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5430 * If an "Upgrade" token is found, the header is left untouched in order
5431 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005432 * if anything but "Upgrade" is present in the Connection header. We don't
5433 * want to touch any 101 response either since it's switching to another
5434 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005435 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005436 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005437 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005438 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005439
Willy Tarreau58975672014-04-24 21:13:57 +02005440 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5441 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5442 /* we want a keep-alive response here. Keep-alive header
5443 * required if either side is not 1.1.
5444 */
5445 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5446 want_flags |= TX_CON_KAL_SET;
5447 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005448 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005449 /* we want a close response here. Close header required if
5450 * the server is 1.1, regardless of the client.
5451 */
5452 if (msg->flags & HTTP_MSGF_VER_11)
5453 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005454 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005455
Willy Tarreau58975672014-04-24 21:13:57 +02005456 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5457 http_change_connection_header(txn, msg, want_flags);
5458 }
5459
5460 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005461 /* Always enter in the body analyzer */
5462 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5463 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005464
Willy Tarreau58975672014-04-24 21:13:57 +02005465 /* if the user wants to log as soon as possible, without counting
5466 * bytes from the server, then this is the right moment. We have
5467 * to temporarily assign bytes_out to log what we currently have.
5468 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005469 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005470 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5471 s->logs.bytes_out = txn->rsp.eoh;
5472 s->do_log(s);
5473 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005474 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005475 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005476}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005477
Willy Tarreaud98cf932009-12-27 22:54:55 +01005478/* This function is an analyser which forwards response body (including chunk
5479 * sizes if any). It is called as soon as we must forward, even if we forward
5480 * zero byte. The only situation where it must not be called is when we're in
5481 * tunnel mode and we want to forward till the close. It's used both to forward
5482 * remaining data and to resync after end of body. It expects the msg_state to
5483 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005484 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005485 *
5486 * It is capable of compressing response data both in content-length mode and
5487 * in chunked mode. The state machines follows different flows depending on
5488 * whether content-length and chunked modes are used, since there are no
5489 * trailers in content-length :
5490 *
5491 * chk-mode cl-mode
5492 * ,----- BODY -----.
5493 * / \
5494 * V size > 0 V chk-mode
5495 * .--> SIZE -------------> DATA -------------> CRLF
5496 * | | size == 0 | last byte |
5497 * | v final crlf v inspected |
5498 * | TRAILERS -----------> DONE |
5499 * | |
5500 * `----------------------------------------------'
5501 *
5502 * Compression only happens in the DATA state, and must be flushed in final
5503 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5504 * is performed at once on final states for all bytes parsed, or when leaving
5505 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005506 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005507int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005508{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005509 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005510 struct http_txn *txn = s->txn;
5511 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005512 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005513
Christopher Faulete0768eb2018-10-03 16:38:02 +02005514 if (IS_HTX_STRM(s))
5515 return htx_response_forward_body(s, res, an_bit);
5516
Christopher Faulet45073512018-07-20 10:16:29 +02005517 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 +02005518 now_ms, __FUNCTION__,
5519 s,
5520 res,
5521 res->rex, res->wex,
5522 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005523 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005524 res->analysers);
5525
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005526 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5527 return 0;
5528
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005529 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005530 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005531 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005532 /* Output closed while we were sending data. We must abort and
5533 * wake the other side up.
5534 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005535 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005536 msg->msg_state = HTTP_MSG_ERROR;
5537 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005538 return 1;
5539 }
5540
Willy Tarreau4fe41902010-06-07 22:27:41 +02005541 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005542 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005543
Christopher Fauletd7c91962015-04-30 11:48:27 +02005544 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005545 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5546 ? HTTP_MSG_CHUNK_SIZE
5547 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005548 }
5549
Willy Tarreauefdf0942014-04-24 20:08:57 +02005550 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005551 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005552 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005553 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005554 }
5555
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005556 if (msg->msg_state < HTTP_MSG_DONE) {
5557 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5558 ? http_msg_forward_chunked_body(s, msg)
5559 : http_msg_forward_body(s, msg));
5560 if (!ret)
5561 goto missing_data_or_waiting;
5562 if (ret < 0)
5563 goto return_bad_res;
5564 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005565
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005566 /* other states, DONE...TUNNEL */
5567 /* for keep-alive we don't want to forward closes on DONE */
5568 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5569 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5570 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005571
Christopher Faulet894da4c2017-07-18 11:29:07 +02005572 http_resync_states(s);
5573 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005574 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5575 if (res->flags & CF_SHUTW) {
5576 /* response errors are most likely due to the
5577 * client aborting the transfer. */
5578 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005579 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005580 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005581 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005582 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005583 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005584 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005585 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005586 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005587
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005588 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005589 if (res->flags & CF_SHUTW)
5590 goto aborted_xfer;
5591
5592 /* stop waiting for data if the input is closed before the end. If the
5593 * client side was already closed, it means that the client has aborted,
5594 * so we don't want to count this as a server abort. Otherwise it's a
5595 * server abort.
5596 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005597 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005598 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005599 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005600 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005601 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005602 if (!(s->flags & SF_ERR_MASK))
5603 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005604 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005605 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005606 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005607 goto return_bad_res_stats_ok;
5608 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005609 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005610
Willy Tarreau40dba092010-03-04 18:14:51 +01005611 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005612 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005613 goto return_bad_res;
5614
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005615 /* When TE: chunked is used, we need to get there again to parse
5616 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005617 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5618 * or if there are filters registered on the stream, we don't want to
5619 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005620 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005621 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005622 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005623 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5624 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005625 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005626
Willy Tarreau5c620922011-05-11 19:56:11 +02005627 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005628 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005629 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005630 * modes are already handled by the stream sock layer. We must not do
5631 * this in content-length mode because it could present the MSG_MORE
5632 * flag with the last block of forwarded data, which would cause an
5633 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005634 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005635 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005636 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005637
Willy Tarreau87b09662015-04-03 00:22:06 +02005638 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005639 return 0;
5640
Willy Tarreau40dba092010-03-04 18:14:51 +01005641 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005642 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005643 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005644 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005645
5646 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005647 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005648 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005649 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005650 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005651 res->analysers &= AN_RES_FLT_END;
5652 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 +01005653 if (objt_server(s->target))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01005654 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005655
Willy Tarreaue7dff022015-04-03 01:14:29 +02005656 if (!(s->flags & SF_ERR_MASK))
5657 s->flags |= SF_ERR_PRXCOND;
5658 if (!(s->flags & SF_FINST_MASK))
5659 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005660 return 0;
5661
5662 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005663 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005664 txn->rsp.msg_state = HTTP_MSG_ERROR;
5665 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005666 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005667 res->analysers &= AN_RES_FLT_END;
5668 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 +01005669
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005670 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5671 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005672 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005673 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005674
Willy Tarreaue7dff022015-04-03 01:14:29 +02005675 if (!(s->flags & SF_ERR_MASK))
5676 s->flags |= SF_ERR_CLICL;
5677 if (!(s->flags & SF_FINST_MASK))
5678 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005679 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005680}
5681
5682
Christopher Faulet10079f52018-10-03 15:17:28 +02005683int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005684{
5685 struct channel *chn = msg->chn;
5686 int ret;
5687
5688 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5689
5690 if (msg->msg_state == HTTP_MSG_ENDING)
5691 goto ending;
5692
5693 /* Neither content-length, nor transfer-encoding was found, so we must
5694 * read the body until the server connection is closed. In that case, we
5695 * eat data as they come. Of course, this happens for response only. */
5696 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005697 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005698 msg->chunk_len += len;
5699 msg->body_len += len;
5700 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005701 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005702 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005703 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005704 msg->next += ret;
5705 msg->chunk_len -= ret;
5706 if (msg->chunk_len) {
5707 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005708 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005709 chn->flags |= CF_WAKE_WRITE;
5710 goto missing_data_or_waiting;
5711 }
5712
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005713 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5714 * always set for a request. */
5715 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5716 /* The server still sending data that should be filtered */
5717 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5718 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005719 msg->msg_state = HTTP_MSG_TUNNEL;
5720 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005721 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005722
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005723 msg->msg_state = HTTP_MSG_ENDING;
5724
5725 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005726 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005727 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005728 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5729 /* default_ret */ msg->next,
5730 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005731 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005732 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005733 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5734 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005735 if (msg->next)
5736 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005737
Christopher Fauletda02e172015-12-04 09:25:05 +01005738 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5739 /* default_ret */ 1,
5740 /* on_error */ goto error,
5741 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005742 if (msg->msg_state == HTTP_MSG_ENDING)
5743 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005744 return 1;
5745
5746 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005747 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005748 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5749 /* default_ret */ msg->next,
5750 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005751 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005752 msg->next -= ret;
5753 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5754 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005755 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005756 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005757 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005758 return 0;
5759 error:
5760 return -1;
5761}
5762
Christopher Faulet10079f52018-10-03 15:17:28 +02005763int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005764{
5765 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005766 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005767 int ret;
5768
5769 /* Here we have the guarantee to be in one of the following state:
5770 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5771 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5772
Christopher Fauletca874b82018-09-20 11:31:01 +02005773 if (msg->msg_state == HTTP_MSG_ENDING)
5774 goto ending;
5775
5776 /* Don't parse chunks if there is no input data */
5777 if (!ci_data(chn))
5778 goto waiting;
5779
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005780 switch_states:
5781 switch (msg->msg_state) {
5782 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005783 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005784 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005785 /* on_error */ goto error);
5786 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005787 msg->chunk_len -= ret;
5788 if (msg->chunk_len) {
5789 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005790 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005791 chn->flags |= CF_WAKE_WRITE;
5792 goto missing_data_or_waiting;
5793 }
5794
5795 /* nothing left to forward for this chunk*/
5796 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5797 /* fall through for HTTP_MSG_CHUNK_CRLF */
5798
5799 case HTTP_MSG_CHUNK_CRLF:
5800 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005801 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005802 if (ret == 0)
5803 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005804 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005805 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005806 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005807 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005808 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005809 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005810 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005811 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5812 /* fall through for HTTP_MSG_CHUNK_SIZE */
5813
5814 case HTTP_MSG_CHUNK_SIZE:
5815 /* read the chunk size and assign it to ->chunk_len,
5816 * then set ->next to point to the body and switch to
5817 * DATA or TRAILERS state.
5818 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005819 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005820 if (ret == 0)
5821 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005822 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005823 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005824 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005825 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005826 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005827 }
5828
5829 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005830 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005831 msg->chunk_len = chunk;
5832 msg->body_len += chunk;
5833
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005834 if (msg->chunk_len) {
5835 msg->msg_state = HTTP_MSG_DATA;
5836 goto switch_states;
5837 }
5838 msg->msg_state = HTTP_MSG_TRAILERS;
5839 /* fall through for HTTP_MSG_TRAILERS */
5840
5841 case HTTP_MSG_TRAILERS:
5842 ret = http_forward_trailers(msg);
5843 if (ret < 0)
5844 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005845 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5846 /* default_ret */ 1,
5847 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005848 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005849 if (!ret)
5850 goto missing_data_or_waiting;
5851 break;
5852
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005853 default:
5854 /* This should no happen in this function */
5855 goto error;
5856 }
5857
5858 msg->msg_state = HTTP_MSG_ENDING;
5859 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005860 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005861 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005862 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005863 /* default_ret */ msg->next,
5864 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005865 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005866 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005867 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5868 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005869 if (msg->next)
5870 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005871
Christopher Fauletda02e172015-12-04 09:25:05 +01005872 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005873 /* default_ret */ 1,
5874 /* on_error */ goto error,
5875 /* on_wait */ goto waiting);
5876 msg->msg_state = HTTP_MSG_DONE;
5877 return 1;
5878
5879 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005880 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005881 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005882 /* default_ret */ msg->next,
5883 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005884 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005885 msg->next -= ret;
5886 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5887 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005888 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005889 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005890 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005891 return 0;
5892
5893 chunk_parsing_error:
5894 if (msg->err_pos >= 0) {
5895 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005896 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005897 msg->msg_state, strm_fe(s));
5898 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005899 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005900 msg, msg->msg_state, s->be);
5901 }
5902 error:
5903 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005904}
5905
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005906
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005907/* Iterate the same filter through all request headers.
5908 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005909 * Since it can manage the switch to another backend, it updates the per-proxy
5910 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005911 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005912int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005913{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005914 char *cur_ptr, *cur_end, *cur_next;
5915 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005916 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005917 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005918 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005919
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005920 last_hdr = 0;
5921
Willy Tarreauf37954d2018-06-15 18:31:02 +02005922 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005923 old_idx = 0;
5924
5925 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005926 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005927 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005928 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005929 (exp->action == ACT_ALLOW ||
5930 exp->action == ACT_DENY ||
5931 exp->action == ACT_TARPIT))
5932 return 0;
5933
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005934 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005935 if (!cur_idx)
5936 break;
5937
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005938 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005939 cur_ptr = cur_next;
5940 cur_end = cur_ptr + cur_hdr->len;
5941 cur_next = cur_end + cur_hdr->cr + 1;
5942
5943 /* Now we have one header between cur_ptr and cur_end,
5944 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005945 */
5946
Willy Tarreau15a53a42015-01-21 13:39:42 +01005947 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005948 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005949 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005950 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005951 last_hdr = 1;
5952 break;
5953
5954 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005955 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005956 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005957 break;
5958
5959 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005960 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005961 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005962 break;
5963
5964 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005965 len = exp_replace(trash.area,
5966 trash.size, cur_ptr,
5967 exp->replace, pmatch);
5968 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005969 return -1;
5970
Willy Tarreau6e27be12018-08-22 04:46:47 +02005971 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5972
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005973 /* FIXME: if the user adds a newline in the replacement, the
5974 * index will not be recalculated for now, and the new line
5975 * will not be counted as a new header.
5976 */
5977
5978 cur_end += delta;
5979 cur_next += delta;
5980 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005981 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005982 break;
5983
5984 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005985 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005986 cur_next += delta;
5987
Willy Tarreaufa355d42009-11-29 18:12:29 +01005988 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005989 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5990 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005991 cur_hdr->len = 0;
5992 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005993 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005994 break;
5995
5996 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005997 }
5998
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005999 /* keep the link from this header to next one in case of later
6000 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006001 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006002 old_idx = cur_idx;
6003 }
6004 return 0;
6005}
6006
6007
6008/* Apply the filter to the request line.
6009 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6010 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006011 * Since it can manage the switch to another backend, it updates the per-proxy
6012 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006013 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006014int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006015{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006016 char *cur_ptr, *cur_end;
6017 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006018 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006019 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006020
Willy Tarreau3d300592007-03-18 18:34:41 +01006021 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006022 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006023 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006024 (exp->action == ACT_ALLOW ||
6025 exp->action == ACT_DENY ||
6026 exp->action == ACT_TARPIT))
6027 return 0;
6028 else if (exp->action == ACT_REMOVE)
6029 return 0;
6030
6031 done = 0;
6032
Willy Tarreauf37954d2018-06-15 18:31:02 +02006033 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006034 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006035
6036 /* Now we have the request line between cur_ptr and cur_end */
6037
Willy Tarreau15a53a42015-01-21 13:39:42 +01006038 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006039 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006040 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006041 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006042 done = 1;
6043 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006044
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006045 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006046 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006047 done = 1;
6048 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006049
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006050 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006051 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006052 done = 1;
6053 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006054
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006055 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006056 len = exp_replace(trash.area, trash.size,
6057 cur_ptr, exp->replace, pmatch);
6058 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006059 return -1;
6060
Willy Tarreau6e27be12018-08-22 04:46:47 +02006061 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
6062
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006063 /* FIXME: if the user adds a newline in the replacement, the
6064 * index will not be recalculated for now, and the new line
6065 * will not be counted as a new header.
6066 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006067
Willy Tarreaufa355d42009-11-29 18:12:29 +01006068 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006069 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006070 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006071 HTTP_MSG_RQMETH,
6072 cur_ptr, cur_end + 1,
6073 NULL, NULL);
6074 if (unlikely(!cur_end))
6075 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006076
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006077 /* we have a full request and we know that we have either a CR
6078 * or an LF at <ptr>.
6079 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006080 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6081 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006082 /* there is no point trying this regex on headers */
6083 return 1;
6084 }
6085 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006086 return done;
6087}
Willy Tarreau97de6242006-12-27 17:18:38 +01006088
Willy Tarreau58f10d72006-12-04 02:26:12 +01006089
Willy Tarreau58f10d72006-12-04 02:26:12 +01006090
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006091/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006092 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006093 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006094 * unparsable request. Since it can manage the switch to another backend, it
6095 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006096 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006097int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006098{
Willy Tarreau192252e2015-04-04 01:47:55 +02006099 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006100 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006101 struct hdr_exp *exp;
6102
6103 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006104 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006105
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006106 /*
6107 * The interleaving of transformations and verdicts
6108 * makes it difficult to decide to continue or stop
6109 * the evaluation.
6110 */
6111
Willy Tarreau6c123b12010-01-28 20:22:06 +01006112 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6113 break;
6114
Willy Tarreau3d300592007-03-18 18:34:41 +01006115 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006116 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006117 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006118 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006119
6120 /* if this filter had a condition, evaluate it now and skip to
6121 * next filter if the condition does not match.
6122 */
6123 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006124 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006125 ret = acl_pass(ret);
6126 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6127 ret = !ret;
6128
6129 if (!ret)
6130 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006131 }
6132
6133 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006134 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006135 if (unlikely(ret < 0))
6136 return -1;
6137
6138 if (likely(ret == 0)) {
6139 /* The filter did not match the request, it can be
6140 * iterated through all headers.
6141 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006142 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6143 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006144 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006145 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006146 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006147}
6148
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006149
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006150/* Delete a value in a header between delimiters <from> and <next> in buffer
6151 * <buf>. The number of characters displaced is returned, and the pointer to
6152 * the first delimiter is updated if required. The function tries as much as
6153 * possible to respect the following principles :
6154 * - replace <from> delimiter by the <next> one unless <from> points to a
6155 * colon, in which case <next> is simply removed
6156 * - set exactly one space character after the new first delimiter, unless
6157 * there are not enough characters in the block being moved to do so.
6158 * - remove unneeded spaces before the previous delimiter and after the new
6159 * one.
6160 *
6161 * It is the caller's responsibility to ensure that :
6162 * - <from> points to a valid delimiter or the colon ;
6163 * - <next> points to a valid delimiter or the final CR/LF ;
6164 * - there are non-space chars before <from> ;
6165 * - there is a CR/LF at or after <next>.
6166 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006167int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006168{
6169 char *prev = *from;
6170
6171 if (*prev == ':') {
6172 /* We're removing the first value, preserve the colon and add a
6173 * space if possible.
6174 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006175 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006176 next++;
6177 prev++;
6178 if (prev < next)
6179 *prev++ = ' ';
6180
Willy Tarreau2235b262016-11-05 15:50:20 +01006181 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006182 next++;
6183 } else {
6184 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006185 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006186 prev--;
6187 *from = prev;
6188
6189 /* copy the delimiter and if possible a space if we're
6190 * not at the end of the line.
6191 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006192 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006193 *prev++ = *next++;
6194 if (prev + 1 < next)
6195 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006196 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006197 next++;
6198 }
6199 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006200 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006201}
6202
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006203/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006204 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006205 * desirable to call it only when needed. This code is quite complex because
6206 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6207 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006208 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006209void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006210{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006211 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006212 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006213 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006214 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006215 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6216 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006217
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006218 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006219 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006220 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006221
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006222 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006223 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006224 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006225
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006226 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006227 hdr_beg = hdr_next;
6228 hdr_end = hdr_beg + cur_hdr->len;
6229 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006230
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006231 /* We have one full header between hdr_beg and hdr_end, and the
6232 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006233 * "Cookie:" headers.
6234 */
6235
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006236 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006237 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006238 old_idx = cur_idx;
6239 continue;
6240 }
6241
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006242 del_from = NULL; /* nothing to be deleted */
6243 preserve_hdr = 0; /* assume we may kill the whole header */
6244
Willy Tarreau58f10d72006-12-04 02:26:12 +01006245 /* Now look for cookies. Conforming to RFC2109, we have to support
6246 * attributes whose name begin with a '$', and associate them with
6247 * the right cookie, if we want to delete this cookie.
6248 * So there are 3 cases for each cookie read :
6249 * 1) it's a special attribute, beginning with a '$' : ignore it.
6250 * 2) it's a server id cookie that we *MAY* want to delete : save
6251 * some pointers on it (last semi-colon, beginning of cookie...)
6252 * 3) it's an application cookie : we *MAY* have to delete a previous
6253 * "special" cookie.
6254 * At the end of loop, if a "special" cookie remains, we may have to
6255 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006256 * *MUST* delete it.
6257 *
6258 * Note: RFC2965 is unclear about the processing of spaces around
6259 * the equal sign in the ATTR=VALUE form. A careful inspection of
6260 * the RFC explicitly allows spaces before it, and not within the
6261 * tokens (attrs or values). An inspection of RFC2109 allows that
6262 * too but section 10.1.3 lets one think that spaces may be allowed
6263 * after the equal sign too, resulting in some (rare) buggy
6264 * implementations trying to do that. So let's do what servers do.
6265 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6266 * allowed quoted strings in values, with any possible character
6267 * after a backslash, including control chars and delimitors, which
6268 * causes parsing to become ambiguous. Browsers also allow spaces
6269 * within values even without quotes.
6270 *
6271 * We have to keep multiple pointers in order to support cookie
6272 * removal at the beginning, middle or end of header without
6273 * corrupting the header. All of these headers are valid :
6274 *
6275 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6276 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6277 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6278 * | | | | | | | | |
6279 * | | | | | | | | hdr_end <--+
6280 * | | | | | | | +--> next
6281 * | | | | | | +----> val_end
6282 * | | | | | +-----------> val_beg
6283 * | | | | +--------------> equal
6284 * | | | +----------------> att_end
6285 * | | +---------------------> att_beg
6286 * | +--------------------------> prev
6287 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006288 */
6289
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006290 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6291 /* Iterate through all cookies on this line */
6292
6293 /* find att_beg */
6294 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006295 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006296 att_beg++;
6297
6298 /* find att_end : this is the first character after the last non
6299 * space before the equal. It may be equal to hdr_end.
6300 */
6301 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006302
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006303 while (equal < hdr_end) {
6304 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006305 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006306 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006307 continue;
6308 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006309 }
6310
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006311 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6312 * is between <att_beg> and <equal>, both may be identical.
6313 */
6314
6315 /* look for end of cookie if there is an equal sign */
6316 if (equal < hdr_end && *equal == '=') {
6317 /* look for the beginning of the value */
6318 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006319 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006320 val_beg++;
6321
6322 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006323 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006324
6325 /* make val_end point to the first white space or delimitor after the value */
6326 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006327 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006328 val_end--;
6329 } else {
6330 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006331 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006332
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006333 /* We have nothing to do with attributes beginning with '$'. However,
6334 * they will automatically be removed if a header before them is removed,
6335 * since they're supposed to be linked together.
6336 */
6337 if (*att_beg == '$')
6338 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006339
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006340 /* Ignore cookies with no equal sign */
6341 if (equal == next) {
6342 /* This is not our cookie, so we must preserve it. But if we already
6343 * scheduled another cookie for removal, we cannot remove the
6344 * complete header, but we can remove the previous block itself.
6345 */
6346 preserve_hdr = 1;
6347 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006348 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006349 val_end += delta;
6350 next += delta;
6351 hdr_end += delta;
6352 hdr_next += delta;
6353 cur_hdr->len += delta;
6354 http_msg_move_end(&txn->req, delta);
6355 prev = del_from;
6356 del_from = NULL;
6357 }
6358 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006359 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006360
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006361 /* if there are spaces around the equal sign, we need to
6362 * strip them otherwise we'll get trouble for cookie captures,
6363 * or even for rewrites. Since this happens extremely rarely,
6364 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006365 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006366 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6367 int stripped_before = 0;
6368 int stripped_after = 0;
6369
6370 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006371 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006372 equal += stripped_before;
6373 val_beg += stripped_before;
6374 }
6375
6376 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006377 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006378 val_beg += stripped_after;
6379 stripped_before += stripped_after;
6380 }
6381
6382 val_end += stripped_before;
6383 next += stripped_before;
6384 hdr_end += stripped_before;
6385 hdr_next += stripped_before;
6386 cur_hdr->len += stripped_before;
6387 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006388 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006389 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006390
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006391 /* First, let's see if we want to capture this cookie. We check
6392 * that we don't already have a client side cookie, because we
6393 * can only capture one. Also as an optimisation, we ignore
6394 * cookies shorter than the declared name.
6395 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006396 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6397 (val_end - att_beg >= sess->fe->capture_namelen) &&
6398 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006399 int log_len = val_end - att_beg;
6400
Willy Tarreaubafbe012017-11-24 17:34:44 +01006401 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006402 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006403 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006404 if (log_len > sess->fe->capture_len)
6405 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006406 memcpy(txn->cli_cookie, att_beg, log_len);
6407 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006408 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006409 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006410
Willy Tarreaubca99692010-10-06 19:25:55 +02006411 /* Persistence cookies in passive, rewrite or insert mode have the
6412 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006413 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006414 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006415 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006416 * For cookies in prefix mode, the form is :
6417 *
6418 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006419 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006420 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6421 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6422 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006423 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006424
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006425 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6426 * have the server ID between val_beg and delim, and the original cookie between
6427 * delim+1 and val_end. Otherwise, delim==val_end :
6428 *
6429 * Cookie: NAME=SRV; # in all but prefix modes
6430 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6431 * | || || | |+-> next
6432 * | || || | +--> val_end
6433 * | || || +---------> delim
6434 * | || |+------------> val_beg
6435 * | || +-------------> att_end = equal
6436 * | |+-----------------> att_beg
6437 * | +------------------> prev
6438 * +-------------------------> hdr_beg
6439 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006440
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006441 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006442 for (delim = val_beg; delim < val_end; delim++)
6443 if (*delim == COOKIE_DELIM)
6444 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006445 } else {
6446 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006447 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006448 /* Now check if the cookie contains a date field, which would
6449 * appear after a vertical bar ('|') just after the server name
6450 * and before the delimiter.
6451 */
6452 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6453 if (vbar1) {
6454 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006455 * right is the last seen date. It is a base64 encoded
6456 * 30-bit value representing the UNIX date since the
6457 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006458 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006459 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006460 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006461 if (val_end - vbar1 >= 5) {
6462 val = b64tos30(vbar1);
6463 if (val > 0)
6464 txn->cookie_last_date = val << 2;
6465 }
6466 /* look for a second vertical bar */
6467 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6468 if (vbar1 && (val_end - vbar1 > 5)) {
6469 val = b64tos30(vbar1 + 1);
6470 if (val > 0)
6471 txn->cookie_first_date = val << 2;
6472 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006473 }
6474 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006475
Willy Tarreauf64d1412010-10-07 20:06:11 +02006476 /* if the cookie has an expiration date and the proxy wants to check
6477 * it, then we do that now. We first check if the cookie is too old,
6478 * then only if it has expired. We detect strict overflow because the
6479 * time resolution here is not great (4 seconds). Cookies with dates
6480 * in the future are ignored if their offset is beyond one day. This
6481 * allows an admin to fix timezone issues without expiring everyone
6482 * and at the same time avoids keeping unwanted side effects for too
6483 * long.
6484 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006485 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6486 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006487 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006488 txn->flags &= ~TX_CK_MASK;
6489 txn->flags |= TX_CK_OLD;
6490 delim = val_beg; // let's pretend we have not found the cookie
6491 txn->cookie_first_date = 0;
6492 txn->cookie_last_date = 0;
6493 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006494 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6495 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006496 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006497 txn->flags &= ~TX_CK_MASK;
6498 txn->flags |= TX_CK_EXPIRED;
6499 delim = val_beg; // let's pretend we have not found the cookie
6500 txn->cookie_first_date = 0;
6501 txn->cookie_last_date = 0;
6502 }
6503
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006504 /* Here, we'll look for the first running server which supports the cookie.
6505 * This allows to share a same cookie between several servers, for example
6506 * to dedicate backup servers to specific servers only.
6507 * However, to prevent clients from sticking to cookie-less backup server
6508 * when they have incidentely learned an empty cookie, we simply ignore
6509 * empty cookies and mark them as invalid.
6510 * The same behaviour is applied when persistence must be ignored.
6511 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006512 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006513 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006514
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006515 while (srv) {
6516 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6517 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006518 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006519 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006520 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006521 /* we found the server and we can use it */
6522 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006523 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006524 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006525 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006526 break;
6527 } else {
6528 /* we found a server, but it's down,
6529 * mark it as such and go on in case
6530 * another one is available.
6531 */
6532 txn->flags &= ~TX_CK_MASK;
6533 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006534 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006535 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006536 srv = srv->next;
6537 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006538
Willy Tarreauf64d1412010-10-07 20:06:11 +02006539 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006540 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006541 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006542 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006543 txn->flags |= TX_CK_UNUSED;
6544 else
6545 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006546 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006547
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006548 /* depending on the cookie mode, we may have to either :
6549 * - delete the complete cookie if we're in insert+indirect mode, so that
6550 * the server never sees it ;
6551 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlant5ba80252018-11-15 09:25:36 -08006552 * application cookie so that it does not get accidently removed later,
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006553 * if we're in cookie prefix mode
6554 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006555 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006556 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006557
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006558 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006559 val_end += delta;
6560 next += delta;
6561 hdr_end += delta;
6562 hdr_next += delta;
6563 cur_hdr->len += delta;
6564 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006565
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006566 del_from = NULL;
6567 preserve_hdr = 1; /* we want to keep this cookie */
6568 }
6569 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006570 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006571 del_from = prev;
6572 }
6573 } else {
6574 /* This is not our cookie, so we must preserve it. But if we already
6575 * scheduled another cookie for removal, we cannot remove the
6576 * complete header, but we can remove the previous block itself.
6577 */
6578 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006579
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006580 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006581 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006582 if (att_beg >= del_from)
6583 att_beg += delta;
6584 if (att_end >= del_from)
6585 att_end += delta;
6586 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006587 val_end += delta;
6588 next += delta;
6589 hdr_end += delta;
6590 hdr_next += delta;
6591 cur_hdr->len += delta;
6592 http_msg_move_end(&txn->req, delta);
6593 prev = del_from;
6594 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006595 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006596 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006597
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006598 /* continue with next cookie on this header line */
6599 att_beg = next;
6600 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006601
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006602 /* There are no more cookies on this line.
6603 * We may still have one (or several) marked for deletion at the
6604 * end of the line. We must do this now in two ways :
6605 * - if some cookies must be preserved, we only delete from the
6606 * mark to the end of line ;
6607 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006608 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006609 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006610 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006611 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006612 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006613 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006614 cur_hdr->len += delta;
6615 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006616 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006617
6618 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006619 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6620 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006621 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006622 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006623 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006624 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006625 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006626 }
6627
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006628 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006629 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006630 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006631}
6632
6633
Willy Tarreaua15645d2007-03-18 16:22:39 +01006634/* Iterate the same filter through all response headers contained in <rtr>.
6635 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6636 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006637int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006638{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006639 char *cur_ptr, *cur_end, *cur_next;
6640 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006641 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006642 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006643 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006644
6645 last_hdr = 0;
6646
Willy Tarreauf37954d2018-06-15 18:31:02 +02006647 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006648 old_idx = 0;
6649
6650 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006651 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006652 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006653 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006654 (exp->action == ACT_ALLOW ||
6655 exp->action == ACT_DENY))
6656 return 0;
6657
6658 cur_idx = txn->hdr_idx.v[old_idx].next;
6659 if (!cur_idx)
6660 break;
6661
6662 cur_hdr = &txn->hdr_idx.v[cur_idx];
6663 cur_ptr = cur_next;
6664 cur_end = cur_ptr + cur_hdr->len;
6665 cur_next = cur_end + cur_hdr->cr + 1;
6666
6667 /* Now we have one header between cur_ptr and cur_end,
6668 * and the next header starts at cur_next.
6669 */
6670
Willy Tarreau15a53a42015-01-21 13:39:42 +01006671 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006672 switch (exp->action) {
6673 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006674 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006675 last_hdr = 1;
6676 break;
6677
6678 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006679 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006680 last_hdr = 1;
6681 break;
6682
6683 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006684 len = exp_replace(trash.area,
6685 trash.size, cur_ptr,
6686 exp->replace, pmatch);
6687 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006688 return -1;
6689
Willy Tarreau6e27be12018-08-22 04:46:47 +02006690 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6691
Willy Tarreaua15645d2007-03-18 16:22:39 +01006692 /* FIXME: if the user adds a newline in the replacement, the
6693 * index will not be recalculated for now, and the new line
6694 * will not be counted as a new header.
6695 */
6696
6697 cur_end += delta;
6698 cur_next += delta;
6699 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006700 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006701 break;
6702
6703 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006704 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006705 cur_next += delta;
6706
Willy Tarreaufa355d42009-11-29 18:12:29 +01006707 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006708 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6709 txn->hdr_idx.used--;
6710 cur_hdr->len = 0;
6711 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006712 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006713 break;
6714
6715 }
6716 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006717
6718 /* keep the link from this header to next one in case of later
6719 * removal of next header.
6720 */
6721 old_idx = cur_idx;
6722 }
6723 return 0;
6724}
6725
6726
6727/* Apply the filter to the status line in the response buffer <rtr>.
6728 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6729 * or -1 if a replacement resulted in an invalid status line.
6730 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006731int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006732{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006733 char *cur_ptr, *cur_end;
6734 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006735 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006736 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006737
Willy Tarreau3d300592007-03-18 18:34:41 +01006738 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006739 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006740 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006741 (exp->action == ACT_ALLOW ||
6742 exp->action == ACT_DENY))
6743 return 0;
6744 else if (exp->action == ACT_REMOVE)
6745 return 0;
6746
6747 done = 0;
6748
Willy Tarreauf37954d2018-06-15 18:31:02 +02006749 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006750 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006751
6752 /* Now we have the status line between cur_ptr and cur_end */
6753
Willy Tarreau15a53a42015-01-21 13:39:42 +01006754 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006755 switch (exp->action) {
6756 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006757 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006758 done = 1;
6759 break;
6760
6761 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006762 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006763 done = 1;
6764 break;
6765
6766 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006767 len = exp_replace(trash.area, trash.size,
6768 cur_ptr, exp->replace, pmatch);
6769 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006770 return -1;
6771
Willy Tarreau6e27be12018-08-22 04:46:47 +02006772 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6773
Willy Tarreaua15645d2007-03-18 16:22:39 +01006774 /* FIXME: if the user adds a newline in the replacement, the
6775 * index will not be recalculated for now, and the new line
6776 * will not be counted as a new header.
6777 */
6778
Willy Tarreaufa355d42009-11-29 18:12:29 +01006779 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006780 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006781 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006782 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006783 cur_ptr, cur_end + 1,
6784 NULL, NULL);
6785 if (unlikely(!cur_end))
6786 return -1;
6787
6788 /* we have a full respnse and we know that we have either a CR
6789 * or an LF at <ptr>.
6790 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006791 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006792 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006793 /* there is no point trying this regex on headers */
6794 return 1;
6795 }
6796 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006797 return done;
6798}
6799
6800
6801
6802/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006803 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006804 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6805 * unparsable response.
6806 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006807int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006808{
Willy Tarreau192252e2015-04-04 01:47:55 +02006809 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006810 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006811 struct hdr_exp *exp;
6812
6813 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006814 int ret;
6815
6816 /*
6817 * The interleaving of transformations and verdicts
6818 * makes it difficult to decide to continue or stop
6819 * the evaluation.
6820 */
6821
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006822 if (txn->flags & TX_SVDENY)
6823 break;
6824
Willy Tarreau3d300592007-03-18 18:34:41 +01006825 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006826 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6827 exp->action == ACT_PASS)) {
6828 exp = exp->next;
6829 continue;
6830 }
6831
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006832 /* if this filter had a condition, evaluate it now and skip to
6833 * next filter if the condition does not match.
6834 */
6835 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006836 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006837 ret = acl_pass(ret);
6838 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6839 ret = !ret;
6840 if (!ret)
6841 continue;
6842 }
6843
Willy Tarreaua15645d2007-03-18 16:22:39 +01006844 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006845 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006846 if (unlikely(ret < 0))
6847 return -1;
6848
6849 if (likely(ret == 0)) {
6850 /* The filter did not match the response, it can be
6851 * iterated through all headers.
6852 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006853 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6854 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006855 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006856 }
6857 return 0;
6858}
6859
6860
Willy Tarreaua15645d2007-03-18 16:22:39 +01006861/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006862 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006863 * desirable to call it only when needed. This function is also used when we
6864 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006865 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006866void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006867{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006868 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006869 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006870 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006871 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006872 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006873 char *hdr_beg, *hdr_end, *hdr_next;
6874 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006875
Willy Tarreaua15645d2007-03-18 16:22:39 +01006876 /* Iterate through the headers.
6877 * we start with the start line.
6878 */
6879 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006880 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006881
6882 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6883 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006884 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006885
6886 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006887 hdr_beg = hdr_next;
6888 hdr_end = hdr_beg + cur_hdr->len;
6889 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006890
Willy Tarreau24581ba2010-08-31 22:39:35 +02006891 /* We have one full header between hdr_beg and hdr_end, and the
6892 * next header starts at hdr_next. We're only interested in
6893 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006894 */
6895
Willy Tarreau24581ba2010-08-31 22:39:35 +02006896 is_cookie2 = 0;
6897 prev = hdr_beg + 10;
6898 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006899 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006900 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6901 if (!val) {
6902 old_idx = cur_idx;
6903 continue;
6904 }
6905 is_cookie2 = 1;
6906 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006907 }
6908
Willy Tarreau24581ba2010-08-31 22:39:35 +02006909 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6910 * <prev> points to the colon.
6911 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006912 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006913
Willy Tarreau24581ba2010-08-31 22:39:35 +02006914 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6915 * check-cache is enabled) and we are not interested in checking
6916 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006917 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006918 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006919 return;
6920
Willy Tarreau24581ba2010-08-31 22:39:35 +02006921 /* OK so now we know we have to process this response cookie.
6922 * The format of the Set-Cookie header is slightly different
6923 * from the format of the Cookie header in that it does not
6924 * support the comma as a cookie delimiter (thus the header
6925 * cannot be folded) because the Expires attribute described in
6926 * the original Netscape's spec may contain an unquoted date
6927 * with a comma inside. We have to live with this because
6928 * many browsers don't support Max-Age and some browsers don't
6929 * support quoted strings. However the Set-Cookie2 header is
6930 * clean.
6931 *
6932 * We have to keep multiple pointers in order to support cookie
6933 * removal at the beginning, middle or end of header without
6934 * corrupting the header (in case of set-cookie2). A special
6935 * pointer, <scav> points to the beginning of the set-cookie-av
6936 * fields after the first semi-colon. The <next> pointer points
6937 * either to the end of line (set-cookie) or next unquoted comma
6938 * (set-cookie2). All of these headers are valid :
6939 *
6940 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6941 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6942 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6943 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6944 * | | | | | | | | | |
6945 * | | | | | | | | +-> next hdr_end <--+
6946 * | | | | | | | +------------> scav
6947 * | | | | | | +--------------> val_end
6948 * | | | | | +--------------------> val_beg
6949 * | | | | +----------------------> equal
6950 * | | | +------------------------> att_end
6951 * | | +----------------------------> att_beg
6952 * | +------------------------------> prev
6953 * +-----------------------------------------> hdr_beg
6954 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006955
Willy Tarreau24581ba2010-08-31 22:39:35 +02006956 for (; prev < hdr_end; prev = next) {
6957 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006958
Willy Tarreau24581ba2010-08-31 22:39:35 +02006959 /* find att_beg */
6960 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006961 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006962 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006963
Willy Tarreau24581ba2010-08-31 22:39:35 +02006964 /* find att_end : this is the first character after the last non
6965 * space before the equal. It may be equal to hdr_end.
6966 */
6967 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006968
Willy Tarreau24581ba2010-08-31 22:39:35 +02006969 while (equal < hdr_end) {
6970 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6971 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006972 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006973 continue;
6974 att_end = equal;
6975 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006976
Willy Tarreau24581ba2010-08-31 22:39:35 +02006977 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6978 * is between <att_beg> and <equal>, both may be identical.
6979 */
6980
6981 /* look for end of cookie if there is an equal sign */
6982 if (equal < hdr_end && *equal == '=') {
6983 /* look for the beginning of the value */
6984 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006985 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006986 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006987
Willy Tarreau24581ba2010-08-31 22:39:35 +02006988 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006989 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006990
6991 /* make val_end point to the first white space or delimitor after the value */
6992 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006993 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006994 val_end--;
6995 } else {
6996 /* <equal> points to next comma, semi-colon or EOL */
6997 val_beg = val_end = next = equal;
6998 }
6999
7000 if (next < hdr_end) {
7001 /* Set-Cookie2 supports multiple cookies, and <next> points to
7002 * a colon or semi-colon before the end. So skip all attr-value
7003 * pairs and look for the next comma. For Set-Cookie, since
7004 * commas are permitted in values, skip to the end.
7005 */
7006 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02007007 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007008 else
7009 next = hdr_end;
7010 }
7011
7012 /* Now everything is as on the diagram above */
7013
7014 /* Ignore cookies with no equal sign */
7015 if (equal == val_end)
7016 continue;
7017
7018 /* If there are spaces around the equal sign, we need to
7019 * strip them otherwise we'll get trouble for cookie captures,
7020 * or even for rewrites. Since this happens extremely rarely,
7021 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007022 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007023 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7024 int stripped_before = 0;
7025 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007026
Willy Tarreau24581ba2010-08-31 22:39:35 +02007027 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007028 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007029 equal += stripped_before;
7030 val_beg += stripped_before;
7031 }
7032
7033 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007034 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007035 val_beg += stripped_after;
7036 stripped_before += stripped_after;
7037 }
7038
7039 val_end += stripped_before;
7040 next += stripped_before;
7041 hdr_end += stripped_before;
7042 hdr_next += stripped_before;
7043 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007044 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007045 }
7046
7047 /* First, let's see if we want to capture this cookie. We check
7048 * that we don't already have a server side cookie, because we
7049 * can only capture one. Also as an optimisation, we ignore
7050 * cookies shorter than the declared name.
7051 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007052 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007053 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007054 (val_end - att_beg >= sess->fe->capture_namelen) &&
7055 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007056 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007057 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007058 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01007059 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007060 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007061 if (log_len > sess->fe->capture_len)
7062 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007063 memcpy(txn->srv_cookie, att_beg, log_len);
7064 txn->srv_cookie[log_len] = 0;
7065 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007066 }
7067
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007068 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007069 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007070 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007071 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7072 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007073 /* assume passive cookie by default */
7074 txn->flags &= ~TX_SCK_MASK;
7075 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007076
7077 /* If the cookie is in insert mode on a known server, we'll delete
7078 * this occurrence because we'll insert another one later.
7079 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007080 * a direct access.
7081 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007082 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007083 /* The "preserve" flag was set, we don't want to touch the
7084 * server's cookie.
7085 */
7086 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007087 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007088 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007089 /* this cookie must be deleted */
7090 if (*prev == ':' && next == hdr_end) {
7091 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007092 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007093 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7094 txn->hdr_idx.used--;
7095 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007096 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007097 hdr_next += delta;
7098 http_msg_move_end(&txn->rsp, delta);
7099 /* note: while both invalid now, <next> and <hdr_end>
7100 * are still equal, so the for() will stop as expected.
7101 */
7102 } else {
7103 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007104 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007105 next = prev;
7106 hdr_end += delta;
7107 hdr_next += delta;
7108 cur_hdr->len += delta;
7109 http_msg_move_end(&txn->rsp, delta);
7110 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007111 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007112 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007113 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007114 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007115 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007116 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007117 * with this server since we know it.
7118 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007119 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007120 next += delta;
7121 hdr_end += delta;
7122 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007123 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007124 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007125
Willy Tarreauf1348312010-10-07 15:54:11 +02007126 txn->flags &= ~TX_SCK_MASK;
7127 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007128 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007129 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007130 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007131 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007132 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007133 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007134 next += delta;
7135 hdr_end += delta;
7136 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007137 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007138 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007139
Willy Tarreau827aee92011-03-10 16:55:02 +01007140 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007141 txn->flags &= ~TX_SCK_MASK;
7142 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007143 }
7144 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007145 /* that's done for this cookie, check the next one on the same
7146 * line when next != hdr_end (only if is_cookie2).
7147 */
7148 }
7149 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007150 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007151 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007152}
7153
7154
Willy Tarreaua15645d2007-03-18 16:22:39 +01007155/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007156 * Parses the Cache-Control and Pragma request header fields to determine if
7157 * the request may be served from the cache and/or if it is cacheable. Updates
7158 * s->txn->flags.
7159 */
7160void check_request_for_cacheability(struct stream *s, struct channel *chn)
7161{
7162 struct http_txn *txn = s->txn;
7163 char *p1, *p2;
7164 char *cur_ptr, *cur_end, *cur_next;
7165 int pragma_found;
7166 int cc_found;
7167 int cur_idx;
7168
Christopher Faulet25a02f62018-10-24 12:00:25 +02007169 if (IS_HTX_STRM(s))
7170 return htx_check_request_for_cacheability(s, chn);
7171
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007172 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7173 return; /* nothing more to do here */
7174
7175 cur_idx = 0;
7176 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007177 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007178
7179 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7180 struct hdr_idx_elem *cur_hdr;
7181 int val;
7182
7183 cur_hdr = &txn->hdr_idx.v[cur_idx];
7184 cur_ptr = cur_next;
7185 cur_end = cur_ptr + cur_hdr->len;
7186 cur_next = cur_end + cur_hdr->cr + 1;
7187
7188 /* We have one full header between cur_ptr and cur_end, and the
7189 * next header starts at cur_next.
7190 */
7191
7192 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7193 if (val) {
7194 if ((cur_end - (cur_ptr + val) >= 8) &&
7195 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7196 pragma_found = 1;
7197 continue;
7198 }
7199 }
7200
William Lallemand8a16fe02018-05-22 11:04:33 +02007201 /* Don't use the cache and don't try to store if we found the
7202 * Authorization header */
7203 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7204 if (val) {
7205 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7206 txn->flags |= TX_CACHE_IGNORE;
7207 continue;
7208 }
7209
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007210 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7211 if (!val)
7212 continue;
7213
7214 /* OK, right now we know we have a cache-control header at cur_ptr */
7215 cc_found = 1;
7216 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7217
7218 if (p1 >= cur_end) /* no more info */
7219 continue;
7220
7221 /* p1 is at the beginning of the value */
7222 p2 = p1;
7223 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7224 p2++;
7225
7226 /* we have a complete value between p1 and p2. We don't check the
7227 * values after max-age, max-stale nor min-fresh, we simply don't
7228 * use the cache when they're specified.
7229 */
7230 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7231 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7232 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7233 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7234 txn->flags |= TX_CACHE_IGNORE;
7235 continue;
7236 }
7237
7238 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7239 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7240 continue;
7241 }
7242 }
7243
7244 /* RFC7234#5.4:
7245 * When the Cache-Control header field is also present and
7246 * understood in a request, Pragma is ignored.
7247 * When the Cache-Control header field is not present in a
7248 * request, caches MUST consider the no-cache request
7249 * pragma-directive as having the same effect as if
7250 * "Cache-Control: no-cache" were present.
7251 */
7252 if (!cc_found && pragma_found)
7253 txn->flags |= TX_CACHE_IGNORE;
7254}
7255
7256/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007257 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007258 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007259void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007260{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007261 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007262 char *p1, *p2;
7263
7264 char *cur_ptr, *cur_end, *cur_next;
7265 int cur_idx;
7266
Christopher Faulet25a02f62018-10-24 12:00:25 +02007267
7268 if (IS_HTX_STRM(s))
7269 return htx_check_response_for_cacheability(s, rtr);
7270
Willy Tarreau12b32f22017-12-21 16:08:09 +01007271 if (txn->status < 200) {
7272 /* do not try to cache interim responses! */
7273 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007274 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007275 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007276
7277 /* Iterate through the headers.
7278 * we start with the start line.
7279 */
7280 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007281 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007282
7283 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7284 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007285 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007286
7287 cur_hdr = &txn->hdr_idx.v[cur_idx];
7288 cur_ptr = cur_next;
7289 cur_end = cur_ptr + cur_hdr->len;
7290 cur_next = cur_end + cur_hdr->cr + 1;
7291
7292 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007293 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007294 */
7295
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007296 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7297 if (val) {
7298 if ((cur_end - (cur_ptr + val) >= 8) &&
7299 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7300 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7301 return;
7302 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007303 }
7304
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007305 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7306 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007307 continue;
7308
7309 /* OK, right now we know we have a cache-control header at cur_ptr */
7310
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007311 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007312
7313 if (p1 >= cur_end) /* no more info */
7314 continue;
7315
7316 /* p1 is at the beginning of the value */
7317 p2 = p1;
7318
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007319 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007320 p2++;
7321
7322 /* we have a complete value between p1 and p2 */
7323 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007324 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7325 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7326 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7327 continue;
7328 }
7329
Willy Tarreaua15645d2007-03-18 16:22:39 +01007330 /* we have something of the form no-cache="set-cookie" */
7331 if ((cur_end - p1 >= 21) &&
7332 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7333 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007334 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007335 continue;
7336 }
7337
7338 /* OK, so we know that either p2 points to the end of string or to a comma */
7339 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007340 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007341 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007342 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007343 return;
7344 }
7345
7346 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007347 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007348 continue;
7349 }
7350 }
7351}
7352
Willy Tarreau58f10d72006-12-04 02:26:12 +01007353
Willy Tarreaub2513902006-12-17 14:52:38 +01007354/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007355 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007356 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007357 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007358 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007359 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007360 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007361 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007362 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007363int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007364{
7365 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007366 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007367 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007368
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007369 if (!uri_auth)
7370 return 0;
7371
Cyril Bonté70be45d2010-10-12 00:14:35 +02007372 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007373 return 0;
7374
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007375 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007376 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007377 return 0;
7378
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007379 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007380 return 0;
7381
Willy Tarreaub2513902006-12-17 14:52:38 +01007382 return 1;
7383}
7384
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007385/* Append the description of what is present in error snapshot <es> into <out>.
7386 * The description must be small enough to always fit in a trash. The output
7387 * buffer may be the trash so the trash must not be used inside this function.
7388 */
7389void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7390{
Christopher Fauleted26fb82018-11-29 22:53:30 +01007391 chunk_appendf(out,
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007392 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7393 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7394 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7395 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7396 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7397 es->ctx.http.m_flags, es->ctx.http.m_clen,
7398 es->ctx.http.m_blen, es->ctx.http.b_flags);
7399}
7400
Willy Tarreau4076a152009-04-02 15:18:36 +02007401/*
7402 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007403 * By default it tries to report the error position as msg->err_pos. However if
7404 * this one is not set, it will then report msg->next, which is the last known
7405 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007406 * displays buffers as a contiguous area starting at buf->p. The direction is
7407 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007408 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007409void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007410 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007411 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007412{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007413 union error_snapshot_ctx ctx;
7414 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007415
Willy Tarreauef3ca732018-09-07 15:47:35 +02007416 /* http-specific part now */
7417 ctx.http.sid = s->uniq_id;
7418 ctx.http.state = state;
7419 ctx.http.b_flags = msg->chn->flags;
7420 ctx.http.s_flags = s->flags;
7421 ctx.http.t_flags = s->txn->flags;
7422 ctx.http.m_flags = msg->flags;
7423 ctx.http.m_clen = msg->chunk_len;
7424 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007425
Willy Tarreauef3ca732018-09-07 15:47:35 +02007426 ofs = msg->chn->total - ci_data(msg->chn);
7427 if (ofs < 0)
7428 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007429
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007430 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007431 other_end, s->target,
7432 strm_sess(s), &msg->chn->buf,
7433 ofs, co_data(msg->chn),
7434 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7435 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007436}
Willy Tarreaub2513902006-12-17 14:52:38 +01007437
Willy Tarreau294c4732011-12-16 21:35:50 +01007438/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7439 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7440 * performed over the whole headers. Otherwise it must contain a valid header
7441 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7442 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7443 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7444 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007445 * -1. The value fetch stops at commas, so this function is suited for use with
7446 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007447 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007448 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007449unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007450 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007451 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007452{
Willy Tarreau294c4732011-12-16 21:35:50 +01007453 struct hdr_ctx local_ctx;
7454 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007455 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007456 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007457 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007458
Willy Tarreau294c4732011-12-16 21:35:50 +01007459 if (!ctx) {
7460 local_ctx.idx = 0;
7461 ctx = &local_ctx;
7462 }
7463
Willy Tarreaubce70882009-09-07 11:51:47 +02007464 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007465 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007466 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007467 occ--;
7468 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007469 *vptr = ctx->line + ctx->val;
7470 *vlen = ctx->vlen;
7471 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007472 }
7473 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007474 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007475 }
7476
7477 /* negative occurrence, we scan all the list then walk back */
7478 if (-occ > MAX_HDR_HISTORY)
7479 return 0;
7480
Willy Tarreau294c4732011-12-16 21:35:50 +01007481 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007482 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007483 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7484 len_hist[hist_ptr] = ctx->vlen;
7485 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007486 hist_ptr = 0;
7487 found++;
7488 }
7489 if (-occ > found)
7490 return 0;
7491 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007492 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7493 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7494 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007495 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007496 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007497 if (hist_ptr >= MAX_HDR_HISTORY)
7498 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007499 *vptr = ptr_hist[hist_ptr];
7500 *vlen = len_hist[hist_ptr];
7501 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007502}
7503
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007504/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7505 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7506 * performed over the whole headers. Otherwise it must contain a valid header
7507 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7508 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7509 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7510 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7511 * -1. This function differs from http_get_hdr() in that it only returns full
7512 * line header values and does not stop at commas.
7513 * The return value is 0 if nothing was found, or non-zero otherwise.
7514 */
7515unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7516 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007517 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007518{
7519 struct hdr_ctx local_ctx;
7520 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007521 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007522 unsigned int hist_ptr;
7523 int found;
7524
7525 if (!ctx) {
7526 local_ctx.idx = 0;
7527 ctx = &local_ctx;
7528 }
7529
7530 if (occ >= 0) {
7531 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007532 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007533 occ--;
7534 if (occ <= 0) {
7535 *vptr = ctx->line + ctx->val;
7536 *vlen = ctx->vlen;
7537 return 1;
7538 }
7539 }
7540 return 0;
7541 }
7542
7543 /* negative occurrence, we scan all the list then walk back */
7544 if (-occ > MAX_HDR_HISTORY)
7545 return 0;
7546
7547 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007548 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007549 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7550 len_hist[hist_ptr] = ctx->vlen;
7551 if (++hist_ptr >= MAX_HDR_HISTORY)
7552 hist_ptr = 0;
7553 found++;
7554 }
7555 if (-occ > found)
7556 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007557
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007558 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007559 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7560 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7561 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007562 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007563 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007564 if (hist_ptr >= MAX_HDR_HISTORY)
7565 hist_ptr -= MAX_HDR_HISTORY;
7566 *vptr = ptr_hist[hist_ptr];
7567 *vlen = len_hist[hist_ptr];
7568 return 1;
7569}
7570
Willy Tarreaubaaee002006-06-26 02:48:02 +02007571/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007572 * Print a debug line with a header. Always stop at the first CR or LF char,
7573 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7574 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007575 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007576void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007577{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007578 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007579 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007580
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007581 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007582 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007583 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007584 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 +02007585
7586 for (max = 0; start + max < end; max++)
7587 if (start[max] == '\r' || start[max] == '\n')
7588 break;
7589
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007590 UBOUND(max, trash.size - trash.data - 3);
7591 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7592 trash.area[trash.data++] = '\n';
7593 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007594}
7595
Willy Tarreaueee5b512015-04-03 23:46:31 +02007596
7597/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7598 * The hdr_idx is allocated as well. In case of allocation failure, everything
7599 * allocated is freed and NULL is returned. Otherwise the new transaction is
7600 * assigned to the stream and returned.
7601 */
7602struct http_txn *http_alloc_txn(struct stream *s)
7603{
7604 struct http_txn *txn = s->txn;
7605
7606 if (txn)
7607 return txn;
7608
Willy Tarreaubafbe012017-11-24 17:34:44 +01007609 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007610 if (!txn)
7611 return txn;
7612
7613 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007614 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007615 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007616 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007617 return NULL;
7618 }
7619
7620 s->txn = txn;
7621 return txn;
7622}
7623
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007624void http_txn_reset_req(struct http_txn *txn)
7625{
7626 txn->req.flags = 0;
7627 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7628 txn->req.next = 0;
7629 txn->req.chunk_len = 0LL;
7630 txn->req.body_len = 0LL;
7631 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7632}
7633
7634void http_txn_reset_res(struct http_txn *txn)
7635{
7636 txn->rsp.flags = 0;
7637 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7638 txn->rsp.next = 0;
7639 txn->rsp.chunk_len = 0LL;
7640 txn->rsp.body_len = 0LL;
7641 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7642}
7643
Willy Tarreau0937bc42009-12-22 15:03:09 +01007644/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007645 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007646 * the required fields are properly allocated and that we only need to (re)init
7647 * them. This should be used before processing any new request.
7648 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007649void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007650{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007651 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007652 struct proxy *fe = strm_fe(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02007653 struct conn_stream *cs = objt_cs(s->si[0].end);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007654
Christopher Fauletf2824e62018-10-01 12:12:37 +02007655 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
7656 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
7657 : 0);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007658 txn->status = -1;
7659
Willy Tarreauf64d1412010-10-07 20:06:11 +02007660 txn->cookie_first_date = 0;
7661 txn->cookie_last_date = 0;
7662
Willy Tarreaueee5b512015-04-03 23:46:31 +02007663 txn->srv_cookie = NULL;
7664 txn->cli_cookie = NULL;
7665 txn->uri = NULL;
7666
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007667 http_txn_reset_req(txn);
7668 http_txn_reset_res(txn);
7669
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007670 txn->req.chn = &s->req;
7671 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007672
7673 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007674
7675 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7676 if (fe->options2 & PR_O2_REQBUG_OK)
7677 txn->req.err_pos = -1; /* let buggy requests pass */
7678
Willy Tarreau0937bc42009-12-22 15:03:09 +01007679 if (txn->hdr_idx.v)
7680 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007681
7682 vars_init(&s->vars_txn, SCOPE_TXN);
7683 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007684}
7685
7686/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007687void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007688{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007689 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007690 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007691
7692 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007693 pool_free(pool_head_requri, txn->uri);
7694 pool_free(pool_head_capture, txn->cli_cookie);
7695 pool_free(pool_head_capture, txn->srv_cookie);
7696 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007697
William Lallemanda73203e2012-03-12 12:48:57 +01007698 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007699 txn->uri = NULL;
7700 txn->srv_cookie = NULL;
7701 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007702
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007703 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007704 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007705 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007706 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007707 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007708 }
7709
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007710 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007711 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007712 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007713 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007714 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007715 }
7716
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007717 if (!LIST_ISEMPTY(&s->vars_txn.head))
7718 vars_prune(&s->vars_txn, s->sess, s);
7719 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7720 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007721}
7722
7723/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007724void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007725{
7726 http_end_txn(s);
7727 http_init_txn(s);
7728
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007729 /* reinitialise the current rule list pointer to NULL. We are sure that
7730 * any rulelist match the NULL pointer.
7731 */
7732 s->current_rule_list = NULL;
7733
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007734 s->be = strm_fe(s);
7735 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007736 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007737 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007738 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007739 /* re-init store persistence */
7740 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007741 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007742
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007743 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007744
Willy Tarreau739cfba2010-01-25 23:11:14 +01007745 /* We must trim any excess data from the response buffer, because we
7746 * may have blocked an invalid response from a server that we don't
Joseph Herlant5ba80252018-11-15 09:25:36 -08007747 * want to accidently forward once we disable the analysers, nor do
Willy Tarreau739cfba2010-01-25 23:11:14 +01007748 * we want those data to come along with next response. A typical
7749 * example of such data would be from a buggy server responding to
7750 * a HEAD with some data, or sending more than the advertised
7751 * content-length.
7752 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007753 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007754 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007755
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007756 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007757 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007758
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007759 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007760 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007761
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007762 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007763 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007764
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007765 s->req.rex = TICK_ETERNITY;
7766 s->req.wex = TICK_ETERNITY;
7767 s->req.analyse_exp = TICK_ETERNITY;
7768 s->res.rex = TICK_ETERNITY;
7769 s->res.wex = TICK_ETERNITY;
7770 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007771 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007772}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007773
Willy Tarreau79e57332018-10-02 16:01:16 +02007774/* This function executes one of the set-{method,path,query,uri} actions. It
7775 * takes the string from the variable 'replace' with length 'len', then modifies
7776 * the relevant part of the request line accordingly. Then it updates various
7777 * pointers to the next elements which were moved, and the total buffer length.
7778 * It finds the action to be performed in p[2], previously filled by function
7779 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7780 * error, though this can be revisited when this code is finally exploited.
7781 *
7782 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7783 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007784 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007785 * In query string case, the mark question '?' must be set at the start of the
7786 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007787 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007788int http_replace_req_line(int action, const char *replace, int len,
7789 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007790{
Willy Tarreau79e57332018-10-02 16:01:16 +02007791 struct http_txn *txn = s->txn;
7792 char *cur_ptr, *cur_end;
7793 int offset = 0;
7794 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007795
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007796 if (IS_HTX_STRM(s))
7797 return htx_req_replace_stline(action, replace, len, px, s);
7798
Willy Tarreau79e57332018-10-02 16:01:16 +02007799 switch (action) {
7800 case 0: // method
7801 cur_ptr = ci_head(&s->req);
7802 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007803
Willy Tarreau79e57332018-10-02 16:01:16 +02007804 /* adjust req line offsets and lengths */
7805 delta = len - offset - (cur_end - cur_ptr);
7806 txn->req.sl.rq.m_l += delta;
7807 txn->req.sl.rq.u += delta;
7808 txn->req.sl.rq.v += delta;
7809 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007810
Willy Tarreau79e57332018-10-02 16:01:16 +02007811 case 1: // path
7812 cur_ptr = http_txn_get_path(txn);
7813 if (!cur_ptr)
7814 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007815
Willy Tarreau79e57332018-10-02 16:01:16 +02007816 cur_end = cur_ptr;
7817 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7818 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007819
Willy Tarreau79e57332018-10-02 16:01:16 +02007820 /* adjust req line offsets and lengths */
7821 delta = len - offset - (cur_end - cur_ptr);
7822 txn->req.sl.rq.u_l += delta;
7823 txn->req.sl.rq.v += delta;
7824 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007825
Willy Tarreau79e57332018-10-02 16:01:16 +02007826 case 2: // query
7827 offset = 1;
7828 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7829 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7830 while (cur_ptr < cur_end && *cur_ptr != '?')
7831 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007832
Willy Tarreau79e57332018-10-02 16:01:16 +02007833 /* skip the question mark or indicate that we must insert it
7834 * (but only if the format string is not empty then).
7835 */
7836 if (cur_ptr < cur_end)
7837 cur_ptr++;
7838 else if (len > 1)
7839 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007840
Willy Tarreau79e57332018-10-02 16:01:16 +02007841 /* adjust req line offsets and lengths */
7842 delta = len - offset - (cur_end - cur_ptr);
7843 txn->req.sl.rq.u_l += delta;
7844 txn->req.sl.rq.v += delta;
7845 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007846
Willy Tarreau79e57332018-10-02 16:01:16 +02007847 case 3: // uri
7848 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7849 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007850
Willy Tarreau79e57332018-10-02 16:01:16 +02007851 /* adjust req line offsets and lengths */
7852 delta = len - offset - (cur_end - cur_ptr);
7853 txn->req.sl.rq.u_l += delta;
7854 txn->req.sl.rq.v += delta;
7855 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007856
Willy Tarreau79e57332018-10-02 16:01:16 +02007857 default:
7858 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007859 }
7860
Willy Tarreau79e57332018-10-02 16:01:16 +02007861 /* commit changes and adjust end of message */
7862 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7863 txn->req.sl.rq.l += delta;
7864 txn->hdr_idx.v[0].len += delta;
7865 http_msg_move_end(&txn->req, delta);
7866 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007867}
7868
Willy Tarreau79e57332018-10-02 16:01:16 +02007869/* This function replace the HTTP status code and the associated message. The
7870 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007871 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007872void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007873{
Willy Tarreau79e57332018-10-02 16:01:16 +02007874 struct http_txn *txn = s->txn;
7875 char *cur_ptr, *cur_end;
7876 int delta;
7877 char *res;
7878 int c_l;
7879 const char *msg = reason;
7880 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007881
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007882 if (IS_HTX_STRM(s))
7883 return htx_res_set_status(status, reason, s);
7884
Willy Tarreau79e57332018-10-02 16:01:16 +02007885 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007886
Willy Tarreau79e57332018-10-02 16:01:16 +02007887 res = ultoa_o(status, trash.area, trash.size);
7888 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007889
Willy Tarreau79e57332018-10-02 16:01:16 +02007890 trash.area[c_l] = ' ';
7891 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007892
Willy Tarreau79e57332018-10-02 16:01:16 +02007893 /* Do we have a custom reason format string? */
7894 if (msg == NULL)
7895 msg = http_get_reason(status);
7896 msg_len = strlen(msg);
7897 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7898 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007899
Willy Tarreau79e57332018-10-02 16:01:16 +02007900 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7901 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007902
Willy Tarreau79e57332018-10-02 16:01:16 +02007903 /* commit changes and adjust message */
7904 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7905 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007906
Willy Tarreau79e57332018-10-02 16:01:16 +02007907 /* adjust res line offsets and lengths */
7908 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7909 txn->rsp.sl.st.c_l = c_l;
7910 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007911
Willy Tarreau79e57332018-10-02 16:01:16 +02007912 delta = trash.data - (cur_end - cur_ptr);
7913 txn->rsp.sl.st.l += delta;
7914 txn->hdr_idx.v[0].len += delta;
7915 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007916}
7917
Willy Tarreau58f10d72006-12-04 02:26:12 +01007918/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007919 * Local variables:
7920 * c-indent-level: 8
7921 * c-basic-offset: 8
7922 * End:
7923 */