blob: 1c1b24b90027f532f2cb528c919b6a4cba426706 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010028#include <common/base64.h>
Frédéric Lécaillea41d5312018-01-29 12:05:07 +010029#include <common/cfgparse.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010043#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020044#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020045#include <types/global.h>
William Lallemand71bd11a2017-11-20 19:13:14 +010046#include <types/cache.h>
William Lallemand9ed62032016-11-21 17:49:11 +010047#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048
Willy Tarreau8797c062007-05-07 00:55:35 +020049#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020050#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020051#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010052#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020054#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010055#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010056#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020057#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010058#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020060#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020061#include <proto/frontend.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020062#include <proto/h1.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010064#include <proto/hdr_idx.h>
Patrick Hemmere3faf022018-08-22 10:02:00 -040065#include <proto/hlua.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010066#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020067#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010069#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020070#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020071#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010072#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020073#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010074#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020075#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020076#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020077#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020078
Cyril Bonté19979e12012-04-04 12:57:21 +020079/* status codes available for the stats admin page (strictly 4 chars length) */
80const char *stat_status_codes[STAT_STATUS_SIZE] = {
81 [STAT_STATUS_DENY] = "DENY",
82 [STAT_STATUS_DONE] = "DONE",
83 [STAT_STATUS_ERRP] = "ERRP",
84 [STAT_STATUS_EXCD] = "EXCD",
85 [STAT_STATUS_NONE] = "NONE",
86 [STAT_STATUS_PART] = "PART",
87 [STAT_STATUS_UNKN] = "UNKN",
88};
89
Willy Tarreau80587432006-12-24 17:47:20 +010090void init_proto_http()
91{
Willy Tarreau332f8bf2007-05-13 21:36:56 +020092 /* memory allocations */
Willy Tarreaubafbe012017-11-24 17:34:44 +010093 pool_head_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
94 pool_head_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +010095}
Willy Tarreaubaaee002006-06-26 02:48:02 +020096
Willy Tarreau53b6c742006-12-17 13:37:46 +010097/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +010098 * Adds a header and its CRLF at the tail of the message's buffer, just before
Willy Tarreau4d893d42018-07-12 15:43:32 +020099 * the last CRLF. <len> bytes are copied, not counting the CRLF.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100100 * The header is also automatically added to the index <hdr_idx>, and the end
101 * of headers is automatically adjusted. The number of bytes added is returned
102 * on success, otherwise <0 is returned indicating an error.
103 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100104int http_header_add_tail2(struct http_msg *msg,
105 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100106{
107 int bytes;
108
Willy Tarreau4d893d42018-07-12 15:43:32 +0200109 bytes = ci_insert_line2(msg->chn, msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100110 if (!bytes)
111 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100112 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100113 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
114}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200116/* Find the first or next occurrence of header <name> in message buffer <sol>
117 * using headers index <idx>, and return it in the <ctx> structure. This
118 * structure holds everything necessary to use the header and find next
119 * occurrence. If its <idx> member is 0, the header is searched from the
120 * beginning. Otherwise, the next occurrence is returned. The function returns
121 * 1 when it finds a value, and 0 when there is no more. It is very similar to
122 * http_find_header2() except that it is designed to work with full-line headers
123 * whose comma is not a delimiter but is part of the syntax. As a special case,
124 * if ctx->val is NULL when searching for a new values of a header, the current
125 * header is rescanned. This allows rescanning after a header deletion.
126 */
127int http_find_full_header2(const char *name, int len,
128 char *sol, struct hdr_idx *idx,
129 struct hdr_ctx *ctx)
130{
131 char *eol, *sov;
132 int cur_idx, old_idx;
133
134 cur_idx = ctx->idx;
135 if (cur_idx) {
136 /* We have previously returned a header, let's search another one */
137 sol = ctx->line;
138 eol = sol + idx->v[cur_idx].len;
139 goto next_hdr;
140 }
141
142 /* first request for this header */
143 sol += hdr_idx_first_pos(idx);
144 old_idx = 0;
145 cur_idx = hdr_idx_first_idx(idx);
146 while (cur_idx) {
147 eol = sol + idx->v[cur_idx].len;
148
149 if (len == 0) {
150 /* No argument was passed, we want any header.
151 * To achieve this, we simply build a fake request. */
152 while (sol + len < eol && sol[len] != ':')
153 len++;
154 name = sol;
155 }
156
157 if ((len < eol - sol) &&
158 (sol[len] == ':') &&
159 (strncasecmp(sol, name, len) == 0)) {
160 ctx->del = len;
161 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100162 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200163 sov++;
164
165 ctx->line = sol;
166 ctx->prev = old_idx;
167 ctx->idx = cur_idx;
168 ctx->val = sov - sol;
169 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100170 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200171 eol--;
172 ctx->tws++;
173 }
174 ctx->vlen = eol - sov;
175 return 1;
176 }
177 next_hdr:
178 sol = eol + idx->v[cur_idx].cr + 1;
179 old_idx = cur_idx;
180 cur_idx = idx->v[cur_idx].next;
181 }
182 return 0;
183}
184
Willy Tarreauc90dc232015-02-20 13:51:36 +0100185/* Find the first or next header field in message buffer <sol> using headers
186 * index <idx>, and return it in the <ctx> structure. This structure holds
187 * everything necessary to use the header and find next occurrence. If its
188 * <idx> member is 0, the first header is retrieved. Otherwise, the next
189 * occurrence is returned. The function returns 1 when it finds a value, and
190 * 0 when there is no more. It is equivalent to http_find_full_header2() with
191 * no header name.
192 */
193int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
194{
195 char *eol, *sov;
196 int cur_idx, old_idx;
197 int len;
198
199 cur_idx = ctx->idx;
200 if (cur_idx) {
201 /* We have previously returned a header, let's search another one */
202 sol = ctx->line;
203 eol = sol + idx->v[cur_idx].len;
204 goto next_hdr;
205 }
206
207 /* first request for this header */
208 sol += hdr_idx_first_pos(idx);
209 old_idx = 0;
210 cur_idx = hdr_idx_first_idx(idx);
211 while (cur_idx) {
212 eol = sol + idx->v[cur_idx].len;
213
214 len = 0;
215 while (1) {
216 if (len >= eol - sol)
217 goto next_hdr;
218 if (sol[len] == ':')
219 break;
220 len++;
221 }
222
223 ctx->del = len;
224 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100225 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100226 sov++;
227
228 ctx->line = sol;
229 ctx->prev = old_idx;
230 ctx->idx = cur_idx;
231 ctx->val = sov - sol;
232 ctx->tws = 0;
233
Willy Tarreau2235b262016-11-05 15:50:20 +0100234 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100235 eol--;
236 ctx->tws++;
237 }
238 ctx->vlen = eol - sov;
239 return 1;
240
241 next_hdr:
242 sol = eol + idx->v[cur_idx].cr + 1;
243 old_idx = cur_idx;
244 cur_idx = idx->v[cur_idx].next;
245 }
246 return 0;
247}
248
Willy Tarreau33a7e692007-06-10 19:45:56 +0200249/* Find the first or next occurrence of header <name> in message buffer <sol>
250 * using headers index <idx>, and return it in the <ctx> structure. This
251 * structure holds everything necessary to use the header and find next
252 * occurrence. If its <idx> member is 0, the header is searched from the
253 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100254 * 1 when it finds a value, and 0 when there is no more. It is designed to work
255 * with headers defined as comma-separated lists. As a special case, if ctx->val
256 * is NULL when searching for a new values of a header, the current header is
257 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200258 */
259int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100260 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200261 struct hdr_ctx *ctx)
262{
Willy Tarreau68085d82010-01-18 14:54:04 +0100263 char *eol, *sov;
264 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200265
Willy Tarreau68085d82010-01-18 14:54:04 +0100266 cur_idx = ctx->idx;
267 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200268 /* We have previously returned a value, let's search
269 * another one on the same line.
270 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200271 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200272 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100273 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200274 eol = sol + idx->v[cur_idx].len;
275
276 if (sov >= eol)
277 /* no more values in this header */
278 goto next_hdr;
279
Willy Tarreau68085d82010-01-18 14:54:04 +0100280 /* values remaining for this header, skip the comma but save it
281 * for later use (eg: for header deletion).
282 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200283 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100284 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200285 sov++;
286
287 goto return_hdr;
288 }
289
290 /* first request for this header */
291 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100292 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200293 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200294 while (cur_idx) {
295 eol = sol + idx->v[cur_idx].len;
296
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200297 if (len == 0) {
298 /* No argument was passed, we want any header.
299 * To achieve this, we simply build a fake request. */
300 while (sol + len < eol && sol[len] != ':')
301 len++;
302 name = sol;
303 }
304
Willy Tarreau33a7e692007-06-10 19:45:56 +0200305 if ((len < eol - sol) &&
306 (sol[len] == ':') &&
307 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100308 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200309 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100310 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200311 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100312
Willy Tarreau33a7e692007-06-10 19:45:56 +0200313 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100314 ctx->prev = old_idx;
315 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200316 ctx->idx = cur_idx;
317 ctx->val = sov - sol;
318
Willy Tarreauab813a42018-09-10 18:41:28 +0200319 eol = http_find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200320 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100321 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200322 eol--;
323 ctx->tws++;
324 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200325 ctx->vlen = eol - sov;
326 return 1;
327 }
328 next_hdr:
329 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100330 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200331 cur_idx = idx->v[cur_idx].next;
332 }
333 return 0;
334}
335
336int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100337 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200338 struct hdr_ctx *ctx)
339{
340 return http_find_header2(name, strlen(name), sol, idx, ctx);
341}
342
Willy Tarreau68085d82010-01-18 14:54:04 +0100343/* Remove one value of a header. This only works on a <ctx> returned by one of
344 * the http_find_header functions. The value is removed, as well as surrounding
345 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100346 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100347 * message <msg>. The new index is returned. If it is zero, it means there is
348 * no more header, so any processing may stop. The ctx is always left in a form
349 * that can be handled by http_find_header2() to find next occurrence.
350 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100351int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100352{
353 int cur_idx = ctx->idx;
354 char *sol = ctx->line;
355 struct hdr_idx_elem *hdr;
356 int delta, skip_comma;
357
358 if (!cur_idx)
359 return 0;
360
361 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200362 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100363 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200364 delta = b_rep_blk(&msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100365 http_msg_move_end(msg, delta);
366 idx->used--;
367 hdr->len = 0; /* unused entry */
368 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100369 if (idx->tail == ctx->idx)
370 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100371 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100372 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100373 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200374 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100375 return ctx->idx;
376 }
377
378 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200379 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
380 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100381 */
382
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200383 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200384 delta = b_rep_blk(&msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200385 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100386 NULL, 0);
387 hdr->len += delta;
388 http_msg_move_end(msg, delta);
389 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200390 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100391 return ctx->idx;
392}
393
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100394/* This function handles a server error at the stream interface level. The
395 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100396 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100397 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100398 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200399 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200400static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau83061a82018-07-13 11:56:34 +0200401 int err, int finst, const struct buffer *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200402{
Willy Tarreau2019f952017-03-14 11:07:31 +0100403 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100404 channel_auto_read(si_oc(si));
405 channel_abort(si_oc(si));
406 channel_auto_close(si_oc(si));
407 channel_erase(si_oc(si));
408 channel_auto_close(si_ic(si));
409 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200410 if (msg)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200411 co_inject(si_ic(si), msg->area, msg->data);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200412 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200413 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200414 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200415 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200416}
417
Willy Tarreau87b09662015-04-03 00:22:06 +0200418/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100419 * and message.
420 */
421
Willy Tarreau83061a82018-07-13 11:56:34 +0200422struct buffer *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100423{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200424 const int msgnum = http_get_status_idx(s->txn->status);
425
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200426 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200427 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200428 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200429 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100430 else
431 return &http_err_chunks[msgnum];
432}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200433
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100434void
Willy Tarreau83061a82018-07-13 11:56:34 +0200435http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100436{
Christopher Fauletd7c91962015-04-30 11:48:27 +0200437 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100438 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100439 stream_int_retnclose(&s->si[0], msg);
440}
441
Willy Tarreau21d2af32008-02-14 20:25:24 +0100442/* Parse the URI from the given transaction (which is assumed to be in request
443 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
444 * It is returned otherwise.
445 */
Willy Tarreau6b952c82018-09-10 17:45:34 +0200446char *http_txn_get_path(const struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100447{
Willy Tarreau6b952c82018-09-10 17:45:34 +0200448 struct ist ret;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100449
Willy Tarreau6b952c82018-09-10 17:45:34 +0200450 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 +0100451
Willy Tarreau6b952c82018-09-10 17:45:34 +0200452 return ret.ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +0100453}
454
Willy Tarreau71241ab2012-12-27 11:30:54 +0100455/* Returns a 302 for a redirectable request that reaches a server working in
456 * in redirect mode. This may only be called just after the stream interface
457 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
458 * follow normal proxy processing. NOTE: this function is designed to support
459 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100460 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200461void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100462{
463 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100464 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100465 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200466 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100467
468 /* 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 Tarreaubafbe012017-11-24 17:34:44 +0100582struct pool_head *pool_head_http_txn;
583struct pool_head *pool_head_requri;
584struct pool_head *pool_head_capture = NULL;
585struct pool_head *pool_head_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100586
Willy Tarreau117f59e2007-03-04 18:17:17 +0100587/*
588 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +0200589 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +0100590 */
Christopher Faulet10079f52018-10-03 15:17:28 +0200591void http_capture_headers(char *som, struct hdr_idx *idx,
592 char **cap, struct cap_hdr *cap_hdr)
Willy Tarreau117f59e2007-03-04 18:17:17 +0100593{
594 char *eol, *sol, *col, *sov;
595 int cur_idx;
596 struct cap_hdr *h;
597 int len;
598
599 sol = som + hdr_idx_first_pos(idx);
600 cur_idx = hdr_idx_first_idx(idx);
601
602 while (cur_idx) {
603 eol = sol + idx->v[cur_idx].len;
604
605 col = sol;
606 while (col < eol && *col != ':')
607 col++;
608
609 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100610 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +0100611 sov++;
612
613 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +0200614 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +0100615 (strncasecmp(sol, h->name, h->namelen) == 0)) {
616 if (cap[h->index] == NULL)
617 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +0100618 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100619
620 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100621 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +0100622 continue;
623 }
624
625 len = eol - sov;
626 if (len > h->len)
627 len = h->len;
628
629 memcpy(cap[h->index], sov, len);
630 cap[h->index][len]=0;
631 }
632 }
633 sol = eol + idx->v[cur_idx].cr + 1;
634 cur_idx = idx->v[cur_idx].next;
635 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100636}
637
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200638/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
639 * conversion succeeded, 0 in case of error. If the request was already 1.X,
640 * nothing is done and 1 is returned.
641 */
Willy Tarreau79e57332018-10-02 16:01:16 +0200642int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200643{
644 int delta;
645 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100646 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200647
648 if (msg->sl.rq.v_l != 0)
649 return 1;
650
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300651 /* RFC 1945 allows only GET for HTTP/0.9 requests */
652 if (txn->meth != HTTP_METH_GET)
653 return 0;
654
Willy Tarreauf37954d2018-06-15 18:31:02 +0200655 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200656
657 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300658 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
659 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200660 }
661 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200662 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +0100663 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200664 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200665 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200666 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +0200667 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200668 NULL, NULL);
669 if (unlikely(!cur_end))
670 return 0;
671
672 /* we have a full HTTP/1.0 request now and we know that
673 * we have either a CR or an LF at <ptr>.
674 */
675 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
676 return 1;
677}
678
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100679/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100680 * and "keep-alive" values. If we already know that some headers may safely
681 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100682 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
683 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +0100684 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100685 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
686 * found, and TX_CON_*_SET is adjusted depending on what is left so only
687 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100688 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +0100689 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100690void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +0100691{
Willy Tarreau5b154472009-12-21 20:11:07 +0100692 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100693 const char *hdr_val = "Connection";
694 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +0100695
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100696 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +0100697 return;
698
Willy Tarreau88d349d2010-01-25 12:15:43 +0100699 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
700 hdr_val = "Proxy-Connection";
701 hdr_len = 16;
702 }
703
Willy Tarreau5b154472009-12-21 20:11:07 +0100704 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100705 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200706 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100707 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
708 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100709 if (to_del & 2)
710 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100711 else
712 txn->flags |= TX_CON_KAL_SET;
713 }
714 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
715 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100716 if (to_del & 1)
717 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100718 else
719 txn->flags |= TX_CON_CLO_SET;
720 }
Willy Tarreau50fc7772012-11-11 22:19:57 +0100721 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
722 txn->flags |= TX_HDR_CONN_UPG;
723 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100724 }
725
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100726 txn->flags |= TX_HDR_CONN_PRS;
727 return;
728}
Willy Tarreau5b154472009-12-21 20:11:07 +0100729
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100730/* Apply desired changes on the Connection: header. Values may be removed and/or
731 * added depending on the <wanted> flags, which are exclusively composed of
732 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
733 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
734 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100735void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100736{
737 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100738 const char *hdr_val = "Connection";
739 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100740
741 ctx.idx = 0;
742
Willy Tarreau88d349d2010-01-25 12:15:43 +0100743
744 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
745 hdr_val = "Proxy-Connection";
746 hdr_len = 16;
747 }
748
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100749 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200750 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100751 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
752 if (wanted & TX_CON_KAL_SET)
753 txn->flags |= TX_CON_KAL_SET;
754 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100755 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +0100756 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100757 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
758 if (wanted & TX_CON_CLO_SET)
759 txn->flags |= TX_CON_CLO_SET;
760 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100761 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +0100762 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100763 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100764
765 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
766 return;
767
768 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
769 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100770 hdr_val = "Connection: close";
771 hdr_len = 17;
772 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
773 hdr_val = "Proxy-Connection: close";
774 hdr_len = 23;
775 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100776 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100777 }
778
779 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
780 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100781 hdr_val = "Connection: keep-alive";
782 hdr_len = 22;
783 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
784 hdr_val = "Proxy-Connection: keep-alive";
785 hdr_len = 28;
786 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100787 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100788 }
789 return;
Willy Tarreau5b154472009-12-21 20:11:07 +0100790}
791
Willy Tarreau87b09662015-04-03 00:22:06 +0200792void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200793{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200794 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200795 int tmp = TX_CON_WANT_KAL;
796
Christopher Faulet315b39c2018-09-21 16:26:19 +0200797 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
798 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
799 tmp = TX_CON_WANT_TUN;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200800
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200801 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Faulet315b39c2018-09-21 16:26:19 +0200802 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200803 tmp = TX_CON_WANT_SCL;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200804
Christopher Faulet315b39c2018-09-21 16:26:19 +0200805 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
806 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200807 tmp = TX_CON_WANT_CLO;
808
809 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
810 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
811
812 if (!(txn->flags & TX_HDR_CONN_PRS) &&
813 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
814 /* parse the Connection header and possibly clean it */
815 int to_del = 0;
816 if ((msg->flags & HTTP_MSGF_VER_11) ||
817 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200818 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200819 to_del |= 2; /* remove "keep-alive" */
820 if (!(msg->flags & HTTP_MSGF_VER_11))
821 to_del |= 1; /* remove "close" */
822 http_parse_connection_header(txn, msg, to_del);
823 }
824
825 /* check if client or config asks for explicit close in KAL/SCL */
826 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
827 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
828 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
829 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
830 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200831 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200832 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
833}
William Lallemand82fe75c2012-10-23 10:25:10 +0200834
Willy Tarreaud787e662009-07-07 10:14:51 +0200835/* This stream analyser waits for a complete HTTP request. It returns 1 if the
836 * processing can continue on next analysers, or zero if it either needs more
837 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100838 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200839 * when it has nothing left to do, and may remove any analyser when it wants to
840 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100841 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200842int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100843{
Willy Tarreau59234e92008-11-30 23:51:27 +0100844 /*
845 * We will parse the partial (or complete) lines.
846 * We will check the request syntax, and also join multi-line
847 * headers. An index of all the lines will be elaborated while
848 * parsing.
849 *
850 * For the parsing, we use a 28 states FSM.
851 *
852 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200853 * ci_head(req) = beginning of request
854 * ci_head(req) + msg->eoh = end of processed headers / start of current one
855 * ci_tail(req) = end of input data
856 * msg->eol = end of current header or line (LF or CRLF)
857 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200858 *
859 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200860 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200861 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
862 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100863 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100864
Willy Tarreau59234e92008-11-30 23:51:27 +0100865 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200866 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200867 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100868 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200869 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100870
Christopher Faulet45073512018-07-20 10:16:29 +0200871 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 +0100872 now_ms, __FUNCTION__,
873 s,
874 req,
875 req->rex, req->wex,
876 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200877 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100878 req->analysers);
879
Willy Tarreau52a0c602009-08-16 22:45:38 +0200880 /* we're speaking HTTP here, so let's speak HTTP to the client */
881 s->srv_error = http_return_srv_error;
882
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300883 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200884 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300885 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
886
Willy Tarreau83e3af02009-12-28 17:39:57 +0100887 /* There's a protected area at the end of the buffer for rewriting
888 * purposes. We don't want to start to parse the request if the
889 * protected area is affected, because we may have to move processed
890 * data later, which is much more complicated.
891 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200892 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200893 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100894 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200895 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100896 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100897 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200898 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200899 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100900 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100901 return 0;
902 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200903 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200904 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200905 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100906 }
907
Willy Tarreauf37954d2018-06-15 18:31:02 +0200908 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100909 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100910 }
911
Willy Tarreau59234e92008-11-30 23:51:27 +0100912 /* 1: we might have to print this header in debug mode */
913 if (unlikely((global.mode & MODE_DEBUG) &&
914 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200915 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100916 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100917
Willy Tarreauf37954d2018-06-15 18:31:02 +0200918 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200919 /* this is a bit complex : in case of error on the request line,
920 * we know that rq.l is still zero, so we display only the part
921 * up to the end of the line (truncated by debug_hdr).
922 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200923 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100924 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100925
Willy Tarreau59234e92008-11-30 23:51:27 +0100926 sol += hdr_idx_first_pos(&txn->hdr_idx);
927 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100928
Willy Tarreau59234e92008-11-30 23:51:27 +0100929 while (cur_idx) {
930 eol = sol + txn->hdr_idx.v[cur_idx].len;
931 debug_hdr("clihdr", s, sol, eol);
932 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
933 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100934 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100935 }
936
Willy Tarreau58f10d72006-12-04 02:26:12 +0100937
Willy Tarreau59234e92008-11-30 23:51:27 +0100938 /*
939 * Now we quickly check if we have found a full valid request.
940 * If not so, we check the FD and buffer states before leaving.
941 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100942 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100943 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200944 * on a keep-alive stream, if we encounter and error, close, t/o,
945 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100946 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200947 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200948 * Last, we may increase some tracked counters' http request errors on
949 * the cases that are deliberately the client's fault. For instance,
950 * a timeout or connection reset is not counted as an error. However
951 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100952 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100953
Willy Tarreau655dce92009-11-08 13:10:58 +0100954 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100955 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100956 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100957 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100958 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200959 stream_inc_http_req_ctr(s);
960 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200961 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100962 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100963 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100964
Willy Tarreau59234e92008-11-30 23:51:27 +0100965 /* 1: Since we are in header mode, if there's no space
966 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200967 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100968 * must terminate it now.
969 */
Willy Tarreau23752332018-06-15 14:54:53 +0200970 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100971 /* FIXME: check if URI is set and return Status
972 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100973 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200974 stream_inc_http_req_ctr(s);
975 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200976 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +0200977 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +0200978 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +0100979 goto return_bad_req;
980 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100981
Willy Tarreau59234e92008-11-30 23:51:27 +0100982 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200983 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200984 if (!(s->flags & SF_ERR_MASK))
985 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100986
Willy Tarreaufcffa692010-01-10 14:21:19 +0100987 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100988 goto failed_keep_alive;
989
Willy Tarreau0f228a02015-05-01 15:37:53 +0200990 if (sess->fe->options & PR_O_IGNORE_PRB)
991 goto failed_keep_alive;
992
Willy Tarreau59234e92008-11-30 23:51:27 +0100993 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +0200994 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200995 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +0200996 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200997 }
998
Willy Tarreaudc979f22012-12-04 10:39:01 +0100999 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001000 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001001 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001002 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001003 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001004 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001005 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001006 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001007 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001008 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001009
Willy Tarreaue7dff022015-04-03 01:14:29 +02001010 if (!(s->flags & SF_FINST_MASK))
1011 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001012 return 0;
1013 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001014
Willy Tarreau59234e92008-11-30 23:51:27 +01001015 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001016 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001017 if (!(s->flags & SF_ERR_MASK))
1018 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001019
Willy Tarreaufcffa692010-01-10 14:21:19 +01001020 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001021 goto failed_keep_alive;
1022
Willy Tarreau0f228a02015-05-01 15:37:53 +02001023 if (sess->fe->options & PR_O_IGNORE_PRB)
1024 goto failed_keep_alive;
1025
Willy Tarreau59234e92008-11-30 23:51:27 +01001026 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001027 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001028 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001029 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001030 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001031 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001032 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001033 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001034 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001035 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001036
Willy Tarreau87b09662015-04-03 00:22:06 +02001037 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001038 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001039 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001040 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001041 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001042
Willy Tarreaue7dff022015-04-03 01:14:29 +02001043 if (!(s->flags & SF_FINST_MASK))
1044 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001045 return 0;
1046 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001047
Willy Tarreau59234e92008-11-30 23:51:27 +01001048 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001049 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001050 if (!(s->flags & SF_ERR_MASK))
1051 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001052
Willy Tarreaufcffa692010-01-10 14:21:19 +01001053 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001054 goto failed_keep_alive;
1055
Willy Tarreau0f228a02015-05-01 15:37:53 +02001056 if (sess->fe->options & PR_O_IGNORE_PRB)
1057 goto failed_keep_alive;
1058
Willy Tarreau4076a152009-04-02 15:18:36 +02001059 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001060 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001061 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001062 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001063 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001064 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001065 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001066 stream_inc_http_err_ctr(s);
1067 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001068 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001069 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001070 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001071 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001072
Willy Tarreaue7dff022015-04-03 01:14:29 +02001073 if (!(s->flags & SF_FINST_MASK))
1074 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001075 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001076 }
1077
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001078 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001079 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001080 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001081#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001082 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001083 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001084 /* We need more data, we have to re-enable quick-ack in case we
1085 * previously disabled it, otherwise we might cause the client
1086 * to delay next data.
1087 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001088 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001089 }
1090#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001091
Willy Tarreaufcffa692010-01-10 14:21:19 +01001092 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1093 /* If the client starts to talk, let's fall back to
1094 * request timeout processing.
1095 */
1096 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001097 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001098 }
1099
Willy Tarreau59234e92008-11-30 23:51:27 +01001100 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001101 if (!tick_isset(req->analyse_exp)) {
1102 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1103 (txn->flags & TX_WAIT_NEXT_RQ) &&
1104 tick_isset(s->be->timeout.httpka))
1105 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1106 else
1107 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1108 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001109
Willy Tarreau59234e92008-11-30 23:51:27 +01001110 /* we're not ready yet */
1111 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001112
1113 failed_keep_alive:
1114 /* Here we process low-level errors for keep-alive requests. In
1115 * short, if the request is not the first one and it experiences
1116 * a timeout, read error or shutdown, we just silently close so
1117 * that the client can try again.
1118 */
1119 txn->status = 0;
1120 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001121 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001122 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001123 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001124 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001125 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001126 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001127 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001128
Willy Tarreaud787e662009-07-07 10:14:51 +02001129 /* OK now we have a complete HTTP request with indexed headers. Let's
1130 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001131 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001132 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001133 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001134 * byte after the last LF. msg->sov points to the first byte of data.
1135 * msg->eol cannot be trusted because it may have been left uninitialized
1136 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001137 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001138
Willy Tarreau87b09662015-04-03 00:22:06 +02001139 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001140 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001141
Willy Tarreaub16a5742010-01-10 14:46:16 +01001142 if (txn->flags & TX_WAIT_NEXT_RQ) {
1143 /* kill the pending keep-alive timeout */
1144 txn->flags &= ~TX_WAIT_NEXT_RQ;
1145 req->analyse_exp = TICK_ETERNITY;
1146 }
1147
1148
Willy Tarreaud787e662009-07-07 10:14:51 +02001149 /* Maybe we found in invalid header name while we were configured not
1150 * to block on that, so we have to capture it now.
1151 */
1152 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001153 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001154
Willy Tarreau59234e92008-11-30 23:51:27 +01001155 /*
1156 * 1: identify the method
1157 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001158 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001159
1160 /* we can make use of server redirect on GET and HEAD */
1161 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001162 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001163 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001164 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001165 /* PRI is reserved for the HTTP/2 preface */
1166 msg->err_pos = 0;
1167 goto return_bad_req;
1168 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001169
Willy Tarreau59234e92008-11-30 23:51:27 +01001170 /*
1171 * 2: check if the URI matches the monitor_uri.
1172 * We have to do this for every request which gets in, because
1173 * the monitor-uri is defined by the frontend.
1174 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001175 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1176 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001177 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001178 sess->fe->monitor_uri,
1179 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001180 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001181 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001182 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001183 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001184
Willy Tarreaue7dff022015-04-03 01:14:29 +02001185 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001186 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001187
Willy Tarreau59234e92008-11-30 23:51:27 +01001188 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001189 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001190 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001191
Willy Tarreau59234e92008-11-30 23:51:27 +01001192 ret = acl_pass(ret);
1193 if (cond->pol == ACL_COND_UNLESS)
1194 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001195
Willy Tarreau59234e92008-11-30 23:51:27 +01001196 if (ret) {
1197 /* we fail this request, let's return 503 service unavail */
1198 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001199 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001200 if (!(s->flags & SF_ERR_MASK))
1201 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001202 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001203 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001204 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001205
Willy Tarreau59234e92008-11-30 23:51:27 +01001206 /* nothing to fail, let's reply normaly */
1207 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001208 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001209 if (!(s->flags & SF_ERR_MASK))
1210 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001211 goto return_prx_cond;
1212 }
1213
1214 /*
1215 * 3: Maybe we have to copy the original REQURI for the logs ?
1216 * Note: we cannot log anymore if the request has been
1217 * classified as invalid.
1218 */
1219 if (unlikely(s->logs.logwait & LW_REQ)) {
1220 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001221 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001222 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001223
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001224 if (urilen >= global.tune.requri_len )
1225 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001226 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001227 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001228
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001229 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001230 s->do_log(s);
1231 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001232 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001233 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001234 }
Willy Tarreau06619262006-12-17 08:37:22 +01001235
Willy Tarreau91852eb2015-05-01 13:26:00 +02001236 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1237 * exactly one digit "." one digit. This check may be disabled using
1238 * option accept-invalid-http-request.
1239 */
1240 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1241 if (msg->sl.rq.v_l != 8) {
1242 msg->err_pos = msg->sl.rq.v;
1243 goto return_bad_req;
1244 }
1245
Willy Tarreauf37954d2018-06-15 18:31:02 +02001246 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1247 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1248 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1249 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001250 msg->err_pos = msg->sl.rq.v + 4;
1251 goto return_bad_req;
1252 }
1253 }
Willy Tarreau13317662015-05-01 13:47:08 +02001254 else {
1255 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1256 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1257 goto return_bad_req;
1258 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001259
Willy Tarreau5b154472009-12-21 20:11:07 +01001260 /* ... and check if the request is HTTP/1.1 or above */
1261 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001262 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
1263 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
1264 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001265 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001266
1267 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001268 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 +01001269
Willy Tarreau88d349d2010-01-25 12:15:43 +01001270 /* if the frontend has "option http-use-proxy-header", we'll check if
1271 * we have what looks like a proxied connection instead of a connection,
1272 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1273 * Note that this is *not* RFC-compliant, however browsers and proxies
1274 * happen to do that despite being non-standard :-(
1275 * We consider that a request not beginning with either '/' or '*' is
1276 * a proxied connection, which covers both "scheme://location" and
1277 * CONNECT ip:port.
1278 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001279 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001280 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01001281 txn->flags |= TX_USE_PX_CONN;
1282
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001283 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001284 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001285
Willy Tarreau59234e92008-11-30 23:51:27 +01001286 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001287 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02001288 http_capture_headers(ci_head(req), &txn->hdr_idx,
1289 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001290
Willy Tarreau557f1992015-05-01 10:05:17 +02001291 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1292 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001293 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001294 * The length of a message body is determined by one of the following
1295 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001296 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001297 * 1. Any response to a HEAD request and any response with a 1xx
1298 * (Informational), 204 (No Content), or 304 (Not Modified) status
1299 * code is always terminated by the first empty line after the
1300 * header fields, regardless of the header fields present in the
1301 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001302 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001303 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1304 * the connection will become a tunnel immediately after the empty
1305 * line that concludes the header fields. A client MUST ignore any
1306 * Content-Length or Transfer-Encoding header fields received in
1307 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001308 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001309 * 3. If a Transfer-Encoding header field is present and the chunked
1310 * transfer coding (Section 4.1) is the final encoding, the message
1311 * body length is determined by reading and decoding the chunked
1312 * data until the transfer coding indicates the data is complete.
1313 *
1314 * If a Transfer-Encoding header field is present in a response and
1315 * the chunked transfer coding is not the final encoding, the
1316 * message body length is determined by reading the connection until
1317 * it is closed by the server. If a Transfer-Encoding header field
1318 * is present in a request and the chunked transfer coding is not
1319 * the final encoding, the message body length cannot be determined
1320 * reliably; the server MUST respond with the 400 (Bad Request)
1321 * status code and then close the connection.
1322 *
1323 * If a message is received with both a Transfer-Encoding and a
1324 * Content-Length header field, the Transfer-Encoding overrides the
1325 * Content-Length. Such a message might indicate an attempt to
1326 * perform request smuggling (Section 9.5) or response splitting
1327 * (Section 9.4) and ought to be handled as an error. A sender MUST
1328 * remove the received Content-Length field prior to forwarding such
1329 * a message downstream.
1330 *
1331 * 4. If a message is received without Transfer-Encoding and with
1332 * either multiple Content-Length header fields having differing
1333 * field-values or a single Content-Length header field having an
1334 * invalid value, then the message framing is invalid and the
1335 * recipient MUST treat it as an unrecoverable error. If this is a
1336 * request message, the server MUST respond with a 400 (Bad Request)
1337 * status code and then close the connection. If this is a response
1338 * message received by a proxy, the proxy MUST close the connection
1339 * to the server, discard the received response, and send a 502 (Bad
1340 * Gateway) response to the client. If this is a response message
1341 * received by a user agent, the user agent MUST close the
1342 * connection to the server and discard the received response.
1343 *
1344 * 5. If a valid Content-Length header field is present without
1345 * Transfer-Encoding, its decimal value defines the expected message
1346 * body length in octets. If the sender closes the connection or
1347 * the recipient times out before the indicated number of octets are
1348 * received, the recipient MUST consider the message to be
1349 * incomplete and close the connection.
1350 *
1351 * 6. If this is a request message and none of the above are true, then
1352 * the message body length is zero (no message body is present).
1353 *
1354 * 7. Otherwise, this is a response message without a declared message
1355 * body length, so the message body length is determined by the
1356 * number of octets received prior to the server closing the
1357 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001358 */
1359
Willy Tarreau32b47f42009-10-18 20:55:02 +02001360 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001361 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001362 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001363 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001364 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001365 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001366 /* chunked not last, return badreq */
1367 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001368 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001369 }
1370
Willy Tarreau1c913912015-04-30 10:57:51 +02001371 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001372 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001373 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001374 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001375 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1376 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001377 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001378 signed long long cl;
1379
Willy Tarreauad14f752011-09-02 20:33:27 +02001380 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001381 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001382 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001383 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001384
Willy Tarreauad14f752011-09-02 20:33:27 +02001385 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001386 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001387 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001388 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001389
Willy Tarreauad14f752011-09-02 20:33:27 +02001390 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001391 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001392 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001393 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001394
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001395 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001396 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001397 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001398 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001399
Christopher Fauletbe821b92017-03-30 11:21:53 +02001400 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001401 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001402 }
1403
Willy Tarreau34dfc602015-05-01 10:09:49 +02001404 /* even bodyless requests have a known length */
1405 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001406
Willy Tarreau179085c2014-04-28 16:48:56 +02001407 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1408 * only change if both the request and the config reference something else.
1409 * Option httpclose by itself sets tunnel mode where headers are mangled.
1410 * However, if another mode is set, it will affect it (eg: server-close/
1411 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1412 * if FE and BE have the same settings (common). The method consists in
1413 * checking if options changed between the two calls (implying that either
1414 * one is non-null, or one of them is non-null and we are there for the first
1415 * time.
1416 */
1417 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001418 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001419 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001420
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001421 /* we may have to wait for the request's body */
1422 if ((s->be->options & PR_O_WREQ_BODY) &&
1423 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1424 req->analysers |= AN_REQ_HTTP_BODY;
1425
Willy Tarreau83ece462017-12-21 15:13:09 +01001426 /*
1427 * RFC7234#4:
1428 * A cache MUST write through requests with methods
1429 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1430 * the origin server; i.e., a cache is not allowed
1431 * to generate a reply to such a request before
1432 * having forwarded the request and having received
1433 * a corresponding response.
1434 *
1435 * RFC7231#4.2.1:
1436 * Of the request methods defined by this
1437 * specification, the GET, HEAD, OPTIONS, and TRACE
1438 * methods are defined to be safe.
1439 */
1440 if (likely(txn->meth == HTTP_METH_GET ||
1441 txn->meth == HTTP_METH_HEAD ||
1442 txn->meth == HTTP_METH_OPTIONS ||
1443 txn->meth == HTTP_METH_TRACE))
1444 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1445
Willy Tarreaud787e662009-07-07 10:14:51 +02001446 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001447 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001448 req->analyse_exp = TICK_ETERNITY;
1449 return 1;
1450
1451 return_bad_req:
1452 /* We centralize bad requests processing here */
1453 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1454 /* we detected a parsing error. We want to archive this request
1455 * in the dedicated proxy area for later troubleshooting.
1456 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001457 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001458 }
1459
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001460 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001461 txn->req.msg_state = HTTP_MSG_ERROR;
1462 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001463 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001464
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001465 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001466 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001467 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001468
1469 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001470 if (!(s->flags & SF_ERR_MASK))
1471 s->flags |= SF_ERR_PRXCOND;
1472 if (!(s->flags & SF_FINST_MASK))
1473 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001474
Christopher Faulet0184ea72017-01-05 14:06:34 +01001475 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001476 req->analyse_exp = TICK_ETERNITY;
1477 return 0;
1478}
1479
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001480
Willy Tarreau347a35d2013-11-22 17:51:09 +01001481/* This function prepares an applet to handle the stats. It can deal with the
1482 * "100-continue" expectation, check that admin rules are met for POST requests,
1483 * and program a response message if something was unexpected. It cannot fail
1484 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001485 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001486 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001487 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001488 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001489int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001490{
1491 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001492 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001493 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001494 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001495 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001496 struct uri_auth *uri_auth = s->be->uri_auth;
1497 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001498 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001499
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001500 appctx = si_appctx(si);
1501 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1502 appctx->st1 = appctx->st2 = 0;
1503 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1504 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001505 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001506 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001507
Willy Tarreauf37954d2018-06-15 18:31:02 +02001508 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001509 lookup = uri + uri_auth->uri_len;
1510
1511 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1512 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001513 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001514 break;
1515 }
1516 }
1517
1518 if (uri_auth->refresh) {
1519 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1520 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001521 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001522 break;
1523 }
1524 }
1525 }
1526
1527 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1528 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001529 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001530 break;
1531 }
1532 }
1533
Willy Tarreau1e62df92016-01-11 18:57:53 +01001534 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1535 if (memcmp(h, ";typed", 6) == 0) {
1536 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1537 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1538 break;
1539 }
1540 }
1541
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001542 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1543 if (memcmp(h, ";st=", 4) == 0) {
1544 int i;
1545 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001546 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001547 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1548 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001549 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001550 break;
1551 }
1552 }
1553 break;
1554 }
1555 }
1556
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001557 appctx->ctx.stats.scope_str = 0;
1558 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001559 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1560 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1561 int itx = 0;
1562 const char *h2;
1563 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1564 const char *err;
1565
1566 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1567 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001568 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001569 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1570 itx++;
1571 h++;
1572 }
1573
1574 if (itx > STAT_SCOPE_TXT_MAXLEN)
1575 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001576 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001577
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001578 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001579 memcpy(scope_txt, h2, itx);
1580 scope_txt[itx] = '\0';
1581 err = invalid_char(scope_txt);
1582 if (err) {
1583 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001584 appctx->ctx.stats.scope_str = 0;
1585 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001586 }
1587 break;
1588 }
1589 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001590
1591 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001592 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001593 int ret = 1;
1594
1595 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001596 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 +01001597 ret = acl_pass(ret);
1598 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1599 ret = !ret;
1600 }
1601
1602 if (ret) {
1603 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001604 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001605 break;
1606 }
1607 }
1608
1609 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001610 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001611 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001612 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001613 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1614 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001615 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001616 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001617 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001618 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1619 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001620 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001621 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001622 else {
1623 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001624 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001625 }
1626
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001627 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001628 return 1;
1629}
1630
Lukas Tribus67db8df2013-06-23 17:37:13 +02001631/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
1632 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
1633 */
Vincent Bernat6e615892016-05-18 16:17:44 +02001634void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001635{
1636#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02001637 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001638 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1639#endif
1640#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02001641 if (from->ss_family == AF_INET6) {
1642 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02001643 /* v4-mapped addresses need IP_TOS */
1644 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1645 else
1646 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
1647 }
1648#endif
1649}
1650
Willy Tarreau87b09662015-04-03 00:22:06 +02001651int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001652 const char* name, unsigned int name_len,
1653 const char *str, struct my_regex *re,
1654 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001655{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001656 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001657 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02001658 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001659 int (*http_find_hdr_func)(const char *name, int len, char *sol,
1660 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02001661 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001662
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001663 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06001664
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001665 /* Choose the header browsing function. */
1666 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001667 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001668 http_find_hdr_func = http_find_header2;
1669 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001670 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001671 http_find_hdr_func = http_find_full_header2;
1672 break;
1673 default: /* impossible */
1674 return -1;
1675 }
1676
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001677 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
1678 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02001679 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001680 char *val = ctx.line + ctx.val;
1681 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06001682
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001683 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
1684 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06001685
Willy Tarreau6e27be12018-08-22 04:46:47 +02001686 len = exp_replace(output->area, output->size, val, str, pmatch);
1687 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06001688 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06001689
Willy Tarreau6e27be12018-08-22 04:46:47 +02001690 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06001691
1692 hdr->len += delta;
1693 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001694
1695 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001696 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06001697 }
1698
1699 return 0;
1700}
1701
Willy Tarreau87b09662015-04-03 00:22:06 +02001702static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001703 const char* name, unsigned int name_len,
1704 struct list *fmt, struct my_regex *re,
1705 int action)
1706{
Willy Tarreau83061a82018-07-13 11:56:34 +02001707 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001708 int ret = -1;
1709
1710 replace = alloc_trash_chunk();
1711 if (!replace)
1712 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001713
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001714 replace->data = build_logline(s, replace->area, replace->size, fmt);
1715 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001716 goto leave;
1717
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001718 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1719 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001720
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001721 leave:
1722 free_trash_chunk(replace);
1723 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001724}
1725
Willy Tarreau87b09662015-04-03 00:22:06 +02001726/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001727 * transaction <txn>. Returns the verdict of the first rule that prevents
1728 * further processing of the request (auth, deny, ...), and defaults to
1729 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1730 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001731 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1732 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1733 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001734 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001735enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001736http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001737{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001738 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001739 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001740 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001741 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001742 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001743 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02001744 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001745 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001746
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001747 /* If "the current_rule_list" match the executed rule list, we are in
1748 * resume condition. If a resume is needed it is always in the action
1749 * and never in the ACL or converters. In this case, we initialise the
1750 * current rule, and go to the action execution point.
1751 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001752 if (s->current_rule) {
1753 rule = s->current_rule;
1754 s->current_rule = NULL;
1755 if (s->current_rule_list == rules)
1756 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001757 }
1758 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001759
Willy Tarreauff011f22011-01-06 17:51:27 +01001760 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001761
Willy Tarreau96257ec2012-12-27 10:46:37 +01001762 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001763 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001764 int ret;
1765
Willy Tarreau192252e2015-04-04 01:47:55 +02001766 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001767 ret = acl_pass(ret);
1768
Willy Tarreauff011f22011-01-06 17:51:27 +01001769 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001770 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001771
1772 if (!ret) /* condition not matched */
1773 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001774 }
1775
Willy Tarreauacc98002015-09-27 23:34:39 +02001776 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001777resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001778 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001779 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02001780 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001781
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001782 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001783 if (deny_status)
1784 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02001785 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001786
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001787 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001788 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001789 if (deny_status)
1790 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02001791 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001792
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001793 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02001794 /* Auth might be performed on regular http-req rules as well as on stats */
1795 auth_realm = rule->arg.auth.realm;
1796 if (!auth_realm) {
1797 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1798 auth_realm = STATS_DEFAULT_REALM;
1799 else
1800 auth_realm = px->id;
1801 }
1802 /* send 401/407 depending on whether we use a proxy or not. We still
1803 * count one error, because normal browsing won't significantly
1804 * increase the counter but brute force attempts will.
1805 */
1806 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1807 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001808 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001809 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02001810 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001811
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001812 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02001813 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
1814 return HTTP_RULE_RES_BADREQ;
1815 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001816
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001817 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001818 s->task->nice = rule->arg.nice;
1819 break;
1820
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001821 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001822 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001823 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001824 break;
1825
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001826 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02001827#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001828 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001829 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02001830#endif
1831 break;
1832
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001833 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001834 s->logs.level = rule->arg.loglevel;
1835 break;
1836
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001837 case ACT_HTTP_REPLACE_HDR:
1838 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001839 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1840 rule->arg.hdr_add.name_len,
1841 &rule->arg.hdr_add.fmt,
1842 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06001843 return HTTP_RULE_RES_BADREQ;
1844 break;
1845
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001846 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001847 ctx.idx = 0;
1848 /* remove all occurrences of the header */
1849 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001850 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001851 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001852 }
Willy Tarreau85603282015-01-21 20:39:27 +01001853 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001854
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001855 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001856 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001857 /* The scope of the trash buffer must be limited to this function. The
1858 * build_logline() function can execute a lot of other function which
1859 * can use the trash buffer. So for limiting the scope of this global
1860 * buffer, we build first the header value using build_logline, and
1861 * after we store the header name.
1862 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001863 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001864
1865 replace = alloc_trash_chunk();
1866 if (!replace)
1867 return HTTP_RULE_RES_BADREQ;
1868
Thierry Fournier4b788f72016-06-01 13:35:36 +02001869 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001870 len += build_logline(s, replace->area + len,
1871 replace->size - len,
1872 &rule->arg.hdr_add.fmt);
1873 memcpy(replace->area, rule->arg.hdr_add.name,
1874 rule->arg.hdr_add.name_len);
1875 replace->area[rule->arg.hdr_add.name_len] = ':';
1876 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1877 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001878
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001879 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001880 /* remove all occurrences of the header */
1881 ctx.idx = 0;
1882 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001883 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001884 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1885 }
1886 }
1887
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001888 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001889 static unsigned char rate_limit = 0;
1890
1891 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001892 replace->area[rule->arg.hdr_add.name_len] = 0;
1893 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,
1894 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001895 }
1896
1897 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1898 if (sess->fe != s->be)
1899 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1900 if (sess->listener->counters)
1901 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1902 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001903
1904 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001905 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001906 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001907
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001908 case ACT_HTTP_DEL_ACL:
1909 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001910 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001911 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001912
1913 /* collect reference */
1914 ref = pat_ref_lookup(rule->arg.map.ref);
1915 if (!ref)
1916 continue;
1917
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001918 /* allocate key */
1919 key = alloc_trash_chunk();
1920 if (!key)
1921 return HTTP_RULE_RES_BADREQ;
1922
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001923 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001924 key->data = build_logline(s, key->area, key->size,
1925 &rule->arg.map.key);
1926 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001927
1928 /* perform update */
1929 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001930 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001931 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001932 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001933
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001934 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001935 break;
1936 }
1937
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001938 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001939 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001940 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001941
1942 /* collect reference */
1943 ref = pat_ref_lookup(rule->arg.map.ref);
1944 if (!ref)
1945 continue;
1946
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001947 /* allocate key */
1948 key = alloc_trash_chunk();
1949 if (!key)
1950 return HTTP_RULE_RES_BADREQ;
1951
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001952 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001953 key->data = build_logline(s, key->area, key->size,
1954 &rule->arg.map.key);
1955 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001956
1957 /* perform update */
1958 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001959 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001960 if (pat_ref_find_elt(ref, key->area) == NULL)
1961 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001962 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001963
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001964 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001965 break;
1966 }
1967
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001968 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001969 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001970 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001971
1972 /* collect reference */
1973 ref = pat_ref_lookup(rule->arg.map.ref);
1974 if (!ref)
1975 continue;
1976
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001977 /* allocate key */
1978 key = alloc_trash_chunk();
1979 if (!key)
1980 return HTTP_RULE_RES_BADREQ;
1981
1982 /* allocate value */
1983 value = alloc_trash_chunk();
1984 if (!value) {
1985 free_trash_chunk(key);
1986 return HTTP_RULE_RES_BADREQ;
1987 }
1988
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001989 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001990 key->data = build_logline(s, key->area, key->size,
1991 &rule->arg.map.key);
1992 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001993
1994 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001995 value->data = build_logline(s, value->area,
1996 value->size,
1997 &rule->arg.map.value);
1998 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001999
2000 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002001 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002002 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002003 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002004 else
2005 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002006 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002007
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002008 free_trash_chunk(key);
2009 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002010 break;
2011 }
William Lallemand73025dd2014-04-24 14:38:37 +02002012
Thierry FOURNIER42148732015-09-02 17:17:33 +02002013 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002014 if ((s->req.flags & CF_READ_ERROR) ||
2015 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2016 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2017 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002018 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002019
Willy Tarreauacc98002015-09-27 23:34:39 +02002020 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002021 case ACT_RET_ERR:
2022 case ACT_RET_CONT:
2023 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002024 case ACT_RET_STOP:
2025 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002026 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002027 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002028 return HTTP_RULE_RES_YIELD;
2029 }
William Lallemand73025dd2014-04-24 14:38:37 +02002030 break;
2031
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002032 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002033 /* Note: only the first valid tracking parameter of each
2034 * applies.
2035 */
2036
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002037 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002038 struct stktable *t;
2039 struct stksess *ts;
2040 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002041 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002042
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002043 t = rule->arg.trk_ctr.table.t;
2044 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 +02002045
2046 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002047 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002048
2049 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002050 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2051 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2052 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002053 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002054
Emeric Brun819fc6f2017-06-13 19:37:32 +02002055 if (ptr1)
2056 stktable_data_cast(ptr1, http_req_cnt)++;
2057
2058 if (ptr2)
2059 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2060 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2061
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002062 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002063
2064 /* If data was modified, we need to touch to re-schedule sync */
2065 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002066 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002067
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002068 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002069 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002070 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002071 }
2072 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002073 break;
2074
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002075 /* other flags exists, but normaly, they never be matched. */
2076 default:
2077 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002078 }
2079 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002080
2081 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002082 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002083}
2084
Willy Tarreau71241ab2012-12-27 11:30:54 +01002085
Willy Tarreau51d861a2015-05-22 17:30:48 +02002086/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2087 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2088 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2089 * is returned, the process can continue the evaluation of next rule list. If
2090 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2091 * is returned, it means the operation could not be processed and a server error
2092 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2093 * deny rule. If *YIELD is returned, the caller must call again the function
2094 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002095 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002096enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002097http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002098{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002099 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002100 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002101 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002102 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002103 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002104 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002105
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002106 /* If "the current_rule_list" match the executed rule list, we are in
2107 * resume condition. If a resume is needed it is always in the action
2108 * and never in the ACL or converters. In this case, we initialise the
2109 * current rule, and go to the action execution point.
2110 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002111 if (s->current_rule) {
2112 rule = s->current_rule;
2113 s->current_rule = NULL;
2114 if (s->current_rule_list == rules)
2115 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002116 }
2117 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002118
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002119 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002120
2121 /* check optional condition */
2122 if (rule->cond) {
2123 int ret;
2124
Willy Tarreau192252e2015-04-04 01:47:55 +02002125 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002126 ret = acl_pass(ret);
2127
2128 if (rule->cond->pol == ACL_COND_UNLESS)
2129 ret = !ret;
2130
2131 if (!ret) /* condition not matched */
2132 continue;
2133 }
2134
Willy Tarreauacc98002015-09-27 23:34:39 +02002135 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002136resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002137 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002138 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002139 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002140
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002141 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002142 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002143 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002144
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002145 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002146 s->task->nice = rule->arg.nice;
2147 break;
2148
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002149 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002150 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002151 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002152 break;
2153
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002154 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002155#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002156 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002157 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002158#endif
2159 break;
2160
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002161 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002162 s->logs.level = rule->arg.loglevel;
2163 break;
2164
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002165 case ACT_HTTP_REPLACE_HDR:
2166 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002167 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2168 rule->arg.hdr_add.name_len,
2169 &rule->arg.hdr_add.fmt,
2170 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002171 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002172 break;
2173
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002174 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002175 ctx.idx = 0;
2176 /* remove all occurrences of the header */
2177 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002178 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002179 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2180 }
Willy Tarreau85603282015-01-21 20:39:27 +01002181 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002182
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002183 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002184 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002185 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002186
2187 replace = alloc_trash_chunk();
2188 if (!replace)
2189 return HTTP_RULE_RES_BADREQ;
2190
2191 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002192 memcpy(replace->area, rule->arg.hdr_add.name,
2193 rule->arg.hdr_add.name_len);
2194 replace->data = rule->arg.hdr_add.name_len;
2195 replace->area[replace->data++] = ':';
2196 replace->area[replace->data++] = ' ';
2197 replace->data += build_logline(s,
2198 replace->area + replace->data,
2199 replace->size - replace->data,
2200 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002201
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002202 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002203 /* remove all occurrences of the header */
2204 ctx.idx = 0;
2205 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002206 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002207 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2208 }
2209 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002210
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002211 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002212 static unsigned char rate_limit = 0;
2213
2214 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002215 replace->area[rule->arg.hdr_add.name_len] = 0;
2216 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,
2217 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002218 }
2219
2220 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2221 if (sess->fe != s->be)
2222 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2223 if (sess->listener->counters)
2224 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2225 if (objt_server(s->target))
2226 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2227 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002228
2229 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002230 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002231 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002232
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002233 case ACT_HTTP_DEL_ACL:
2234 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002235 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002236 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002237
2238 /* collect reference */
2239 ref = pat_ref_lookup(rule->arg.map.ref);
2240 if (!ref)
2241 continue;
2242
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002243 /* allocate key */
2244 key = alloc_trash_chunk();
2245 if (!key)
2246 return HTTP_RULE_RES_BADREQ;
2247
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002248 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002249 key->data = build_logline(s, key->area, key->size,
2250 &rule->arg.map.key);
2251 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002252
2253 /* perform update */
2254 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002255 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002256 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002257 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002258
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002259 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002260 break;
2261 }
2262
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002263 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002264 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002265 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002266
2267 /* collect reference */
2268 ref = pat_ref_lookup(rule->arg.map.ref);
2269 if (!ref)
2270 continue;
2271
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002272 /* allocate key */
2273 key = alloc_trash_chunk();
2274 if (!key)
2275 return HTTP_RULE_RES_BADREQ;
2276
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002277 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002278 key->data = build_logline(s, key->area, key->size,
2279 &rule->arg.map.key);
2280 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002281
2282 /* perform update */
2283 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002284 if (pat_ref_find_elt(ref, key->area) == NULL)
2285 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002286
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002287 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002288 break;
2289 }
2290
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002291 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002292 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002293 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002294
2295 /* collect reference */
2296 ref = pat_ref_lookup(rule->arg.map.ref);
2297 if (!ref)
2298 continue;
2299
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002300 /* allocate key */
2301 key = alloc_trash_chunk();
2302 if (!key)
2303 return HTTP_RULE_RES_BADREQ;
2304
2305 /* allocate value */
2306 value = alloc_trash_chunk();
2307 if (!value) {
2308 free_trash_chunk(key);
2309 return HTTP_RULE_RES_BADREQ;
2310 }
2311
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002312 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002313 key->data = build_logline(s, key->area, key->size,
2314 &rule->arg.map.key);
2315 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002316
2317 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002318 value->data = build_logline(s, value->area,
2319 value->size,
2320 &rule->arg.map.value);
2321 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002322
2323 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002324 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002325 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002326 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002327 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002328 else
2329 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002330 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002331 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002332 free_trash_chunk(key);
2333 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002334 break;
2335 }
William Lallemand73025dd2014-04-24 14:38:37 +02002336
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002337 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02002338 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2339 return HTTP_RULE_RES_BADREQ;
2340 return HTTP_RULE_RES_DONE;
2341
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002342 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2343 /* Note: only the first valid tracking parameter of each
2344 * applies.
2345 */
2346
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002347 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002348 struct stktable *t;
2349 struct stksess *ts;
2350 struct stktable_key *key;
2351 void *ptr;
2352
2353 t = rule->arg.trk_ctr.table.t;
2354 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2355
2356 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002357 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002358
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002359 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002360
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002361 /* let's count a new HTTP request as it's the first time we do it */
2362 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2363 if (ptr)
2364 stktable_data_cast(ptr, http_req_cnt)++;
2365
2366 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2367 if (ptr)
2368 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2369 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2370
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002371 /* When the client triggers a 4xx from the server, it's most often due
2372 * to a missing object or permission. These events should be tracked
2373 * because if they happen often, it may indicate a brute force or a
2374 * vulnerability scan. Normally this is done when receiving the response
2375 * but here we're tracking after this ought to have been done so we have
2376 * to do it on purpose.
2377 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002378 if ((unsigned)(txn->status - 400) < 100) {
2379 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2380 if (ptr)
2381 stktable_data_cast(ptr, http_err_cnt)++;
2382
2383 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2384 if (ptr)
2385 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2386 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2387 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002388
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002389 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002390
Emeric Brun0fed0b02017-11-29 16:15:07 +01002391 /* If data was modified, we need to touch to re-schedule sync */
2392 stktable_touch_local(t, ts, 0);
2393
Emeric Brun819fc6f2017-06-13 19:37:32 +02002394 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2395 if (sess->fe != s->be)
2396 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2397
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002398 }
2399 }
2400 break;
2401
Thierry FOURNIER42148732015-09-02 17:17:33 +02002402 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002403 if ((s->req.flags & CF_READ_ERROR) ||
2404 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2405 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2406 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002407 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002408
Willy Tarreauacc98002015-09-27 23:34:39 +02002409 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002410 case ACT_RET_ERR:
2411 case ACT_RET_CONT:
2412 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002413 case ACT_RET_STOP:
2414 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002415 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002416 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002417 return HTTP_RULE_RES_YIELD;
2418 }
William Lallemand73025dd2014-04-24 14:38:37 +02002419 break;
2420
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002421 /* other flags exists, but normaly, they never be matched. */
2422 default:
2423 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002424 }
2425 }
2426
2427 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002428 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002429}
2430
2431
Willy Tarreau71241ab2012-12-27 11:30:54 +01002432/* Perform an HTTP redirect based on the information in <rule>. The function
2433 * returns non-zero on success, or zero in case of a, irrecoverable error such
2434 * as too large a request to build a valid response.
2435 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002436int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002437{
Willy Tarreaub329a312015-05-22 16:27:37 +02002438 struct http_msg *req = &txn->req;
2439 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002440 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002441 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002442 int ret = 0;
2443
2444 chunk = alloc_trash_chunk();
2445 if (!chunk)
2446 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002447
2448 /* build redirect message */
2449 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002450 case 308:
2451 msg_fmt = HTTP_308;
2452 break;
2453 case 307:
2454 msg_fmt = HTTP_307;
2455 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002456 case 303:
2457 msg_fmt = HTTP_303;
2458 break;
2459 case 301:
2460 msg_fmt = HTTP_301;
2461 break;
2462 case 302:
2463 default:
2464 msg_fmt = HTTP_302;
2465 break;
2466 }
2467
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002468 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2469 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002470
2471 switch(rule->type) {
2472 case REDIRECT_TYPE_SCHEME: {
2473 const char *path;
2474 const char *host;
2475 struct hdr_ctx ctx;
2476 int pathlen;
2477 int hostlen;
2478
2479 host = "";
2480 hostlen = 0;
2481 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002482 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002483 host = ctx.line + ctx.val;
2484 hostlen = ctx.vlen;
2485 }
2486
Willy Tarreau6b952c82018-09-10 17:45:34 +02002487 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002488 /* build message using path */
2489 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002490 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002491 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2492 int qs = 0;
2493 while (qs < pathlen) {
2494 if (path[qs] == '?') {
2495 pathlen = qs;
2496 break;
2497 }
2498 qs++;
2499 }
2500 }
2501 } else {
2502 path = "/";
2503 pathlen = 1;
2504 }
2505
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002506 if (rule->rdr_str) { /* this is an old "redirect" rule */
2507 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002508 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002509 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002510
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002511 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002512 memcpy(chunk->area + chunk->data, rule->rdr_str,
2513 rule->rdr_len);
2514 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002515 }
2516 else {
2517 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002518 chunk->data += build_logline(s,
2519 chunk->area + chunk->data,
2520 chunk->size - chunk->data,
2521 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002522
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002523 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002524 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002525 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002526 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002527 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002528 memcpy(chunk->area + chunk->data, "://", 3);
2529 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002530
2531 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002532 memcpy(chunk->area + chunk->data, host, hostlen);
2533 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002534
2535 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002536 memcpy(chunk->area + chunk->data, path, pathlen);
2537 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002538
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002539 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002540 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002541 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002542 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002543 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002544 chunk->area[chunk->data] = '/';
2545 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002546 }
2547
2548 break;
2549 }
2550 case REDIRECT_TYPE_PREFIX: {
2551 const char *path;
2552 int pathlen;
2553
Willy Tarreau6b952c82018-09-10 17:45:34 +02002554 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002555 /* build message using path */
2556 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002557 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002558 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2559 int qs = 0;
2560 while (qs < pathlen) {
2561 if (path[qs] == '?') {
2562 pathlen = qs;
2563 break;
2564 }
2565 qs++;
2566 }
2567 }
2568 } else {
2569 path = "/";
2570 pathlen = 1;
2571 }
2572
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002573 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002574 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002575 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002576
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002577 /* add prefix. Note that if prefix == "/", we don't want to
2578 * add anything, otherwise it makes it hard for the user to
2579 * configure a self-redirection.
2580 */
2581 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002582 memcpy(chunk->area + chunk->data,
2583 rule->rdr_str, rule->rdr_len);
2584 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002585 }
2586 }
2587 else {
2588 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002589 chunk->data += build_logline(s,
2590 chunk->area + chunk->data,
2591 chunk->size - chunk->data,
2592 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002593
2594 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002595 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002596 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002597 }
2598
2599 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002600 memcpy(chunk->area + chunk->data, path, pathlen);
2601 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002602
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002603 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002604 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002605 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002606 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002607 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002608 chunk->area[chunk->data] = '/';
2609 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002610 }
2611
2612 break;
2613 }
2614 case REDIRECT_TYPE_LOCATION:
2615 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002616 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002617 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002618 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002619
2620 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002621 memcpy(chunk->area + chunk->data, rule->rdr_str,
2622 rule->rdr_len);
2623 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002624 }
2625 else {
2626 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002627 chunk->data += build_logline(s,
2628 chunk->area + chunk->data,
2629 chunk->size - chunk->data,
2630 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002631
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002632 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002633 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002634 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002635 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002636 break;
2637 }
2638
2639 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002640 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2641 chunk->data += 14;
2642 memcpy(chunk->area + chunk->data, rule->cookie_str,
2643 rule->cookie_len);
2644 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002645 }
2646
Willy Tarreau19b14122017-02-28 09:48:11 +01002647 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002648 txn->status = rule->code;
2649 /* let's log the request time */
2650 s->logs.tv_request = now;
2651
Christopher Fauletbe821b92017-03-30 11:21:53 +02002652 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002653 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2654 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2655 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002656 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002657 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002658 memcpy(chunk->area + chunk->data,
2659 "\r\nProxy-Connection: keep-alive", 30);
2660 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002661 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002662 memcpy(chunk->area + chunk->data,
2663 "\r\nConnection: keep-alive", 24);
2664 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002665 }
2666 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002667 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2668 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002669 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002670 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002671 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002672 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002673 req->next -= req->sov;
2674 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002675 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002676 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002677 req->msg_state = HTTP_MSG_CLOSED;
2678 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002679 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002680 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002681 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002682 /* let the server side turn to SI_ST_CLO */
2683 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002684 } else {
2685 /* keep-alive not possible */
2686 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002687 memcpy(chunk->area + chunk->data,
2688 "\r\nProxy-Connection: close\r\n\r\n", 29);
2689 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002690 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002691 memcpy(chunk->area + chunk->data,
2692 "\r\nConnection: close\r\n\r\n", 23);
2693 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002694 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002695 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002696 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002697 }
2698
Willy Tarreaue7dff022015-04-03 01:14:29 +02002699 if (!(s->flags & SF_ERR_MASK))
2700 s->flags |= SF_ERR_LOCAL;
2701 if (!(s->flags & SF_FINST_MASK))
2702 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002703
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002704 ret = 1;
2705 leave:
2706 free_trash_chunk(chunk);
2707 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002708}
2709
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002710/* This stream analyser runs all HTTP request processing which is common to
2711 * frontends and backends, which means blocking ACLs, filters, connection-close,
2712 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002713 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002714 * either needs more data or wants to immediately abort the request (eg: deny,
2715 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002716 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002717int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002718{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002719 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002720 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002721 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002722 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002723 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002724 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002725 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002726 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002727
Willy Tarreau655dce92009-11-08 13:10:58 +01002728 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002729 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002730 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002731 }
2732
Christopher Faulet45073512018-07-20 10:16:29 +02002733 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 +02002734 now_ms, __FUNCTION__,
2735 s,
2736 req,
2737 req->rex, req->wex,
2738 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002739 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002740 req->analysers);
2741
Willy Tarreau65410832014-04-28 21:25:43 +02002742 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002743 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002744
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002745 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002746 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002747 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002748
Willy Tarreau0b748332014-04-29 00:13:29 +02002749 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002750 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2751 goto return_prx_yield;
2752
Willy Tarreau0b748332014-04-29 00:13:29 +02002753 case HTTP_RULE_RES_CONT:
2754 case HTTP_RULE_RES_STOP: /* nothing to do */
2755 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002756
Willy Tarreau0b748332014-04-29 00:13:29 +02002757 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2758 if (txn->flags & TX_CLTARPIT)
2759 goto tarpit;
2760 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002761
Willy Tarreau0b748332014-04-29 00:13:29 +02002762 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2763 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002764
Willy Tarreau0b748332014-04-29 00:13:29 +02002765 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002766 goto done;
2767
Willy Tarreau0b748332014-04-29 00:13:29 +02002768 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2769 goto return_bad_req;
2770 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002771 }
2772
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002773 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2774 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002775 struct hdr_ctx ctx;
2776
2777 ctx.idx = 0;
2778 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002779 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002780 if (unlikely(http_header_add_tail2(&txn->req,
2781 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002782 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002783 goto return_bad_req;
2784 }
2785 }
2786
2787 }
2788
Willy Tarreau52542592014-04-28 18:33:26 +02002789 /* OK at this stage, we know that the request was accepted according to
2790 * the http-request rules, we can check for the stats. Note that the
2791 * URI is detected *before* the req* rules in order not to be affected
2792 * by a possible reqrep, while they are processed *after* so that a
2793 * reqdeny can still block them. This clearly needs to change in 1.6!
2794 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002795 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002796 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01002797 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002798 txn->status = 500;
2799 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002800 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002801
Willy Tarreaue7dff022015-04-03 01:14:29 +02002802 if (!(s->flags & SF_ERR_MASK))
2803 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002804 goto return_prx_cond;
2805 }
2806
2807 /* parse the whole stats request and extract the relevant information */
2808 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002809 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002810 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002811
Willy Tarreau0b748332014-04-29 00:13:29 +02002812 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2813 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002814
Willy Tarreau0b748332014-04-29 00:13:29 +02002815 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2816 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002817 }
2818
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002819 /* evaluate the req* rules except reqadd */
2820 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002821 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002822 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002823
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002824 if (txn->flags & TX_CLDENY)
2825 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002826
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002827 if (txn->flags & TX_CLTARPIT) {
2828 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002829 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002830 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002831 }
Willy Tarreau06619262006-12-17 08:37:22 +01002832
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002833 /* add request headers from the rule sets in the same order */
2834 list_for_each_entry(wl, &px->req_add, list) {
2835 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002836 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002837 ret = acl_pass(ret);
2838 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2839 ret = !ret;
2840 if (!ret)
2841 continue;
2842 }
2843
Christopher Faulet10079f52018-10-03 15:17:28 +02002844 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002845 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002846 }
2847
Willy Tarreau52542592014-04-28 18:33:26 +02002848
2849 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002850 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2851 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002852 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002853 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002854 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002855
Willy Tarreaue7dff022015-04-03 01:14:29 +02002856 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2857 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2858 if (!(s->flags & SF_FINST_MASK))
2859 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002860
Willy Tarreau70730dd2014-04-24 18:06:27 +02002861 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002862 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2863 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002864 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002865 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002866 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002867
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002868 /* check whether we have some ACLs set to redirect this request */
2869 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002870 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002871 int ret;
2872
Willy Tarreau192252e2015-04-04 01:47:55 +02002873 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002874 ret = acl_pass(ret);
2875 if (rule->cond->pol == ACL_COND_UNLESS)
2876 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002877 if (!ret)
2878 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01002879 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002880 if (!http_apply_redirect_rule(rule, s, txn))
2881 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002882 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002883 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002884
Willy Tarreau2be39392010-01-03 17:24:51 +01002885 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2886 * If this happens, then the data will not come immediately, so we must
2887 * send all what we have without waiting. Note that due to the small gain
2888 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002889 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01002890 * itself once used.
2891 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002892 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01002893
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002894 done: /* done with this analyser, continue with next ones that the calling
2895 * points will have set, if any.
2896 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002897 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002898 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
2899 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002900 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002901
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002902 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02002903 /* Allow cookie logging
2904 */
2905 if (s->be->cookie_name || sess->fe->capture_name)
2906 manage_client_side_cookies(s, req);
2907
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002908 /* When a connection is tarpitted, we use the tarpit timeout,
2909 * which may be the same as the connect timeout if unspecified.
2910 * If unset, then set it to zero because we really want it to
2911 * eventually expire. We build the tarpit as an analyser.
2912 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002913 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002914
2915 /* wipe the request out so that we can drop the connection early
2916 * if the client closes first.
2917 */
2918 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002919
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002920 txn->status = http_err_codes[deny_status];
2921
Christopher Faulet0184ea72017-01-05 14:06:34 +01002922 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002923 req->analysers |= AN_REQ_HTTP_TARPIT;
2924 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2925 if (!req->analyse_exp)
2926 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02002927 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002928 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002929 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002930 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002931 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002932 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002933 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002934
2935 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002936
2937 /* Allow cookie logging
2938 */
2939 if (s->be->cookie_name || sess->fe->capture_name)
2940 manage_client_side_cookies(s, req);
2941
Willy Tarreau0b748332014-04-29 00:13:29 +02002942 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002943 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002944 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002945 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02002946 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002947 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002948 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002949 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002950 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002951 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002952 goto return_prx_cond;
2953
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002954 return_bad_req:
2955 /* We centralize bad requests processing here */
2956 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2957 /* we detected a parsing error. We want to archive this request
2958 * in the dedicated proxy area for later troubleshooting.
2959 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02002960 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002961 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002962
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002963 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002964 txn->req.msg_state = HTTP_MSG_ERROR;
2965 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002966 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002967
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002968 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002969 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002970 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002971
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002972 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002973 if (!(s->flags & SF_ERR_MASK))
2974 s->flags |= SF_ERR_PRXCOND;
2975 if (!(s->flags & SF_FINST_MASK))
2976 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002977
Christopher Faulet0184ea72017-01-05 14:06:34 +01002978 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002979 req->analyse_exp = TICK_ETERNITY;
2980 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002981
2982 return_prx_yield:
2983 channel_dont_connect(req);
2984 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002985}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002986
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002987/* This function performs all the processing enabled for the current request.
2988 * It returns 1 if the processing can continue on next analysers, or zero if it
2989 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002990 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002991 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002992int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002993{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002994 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002995 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002996 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02002997 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002998
Willy Tarreau655dce92009-11-08 13:10:58 +01002999 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003000 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003001 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003002 return 0;
3003 }
3004
Christopher Faulet45073512018-07-20 10:16:29 +02003005 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 +02003006 now_ms, __FUNCTION__,
3007 s,
3008 req,
3009 req->rex, req->wex,
3010 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003011 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003012 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003013
Willy Tarreau59234e92008-11-30 23:51:27 +01003014 /*
3015 * Right now, we know that we have processed the entire headers
3016 * and that unwanted requests have been filtered out. We can do
3017 * whatever we want with the remaining request. Also, now we
3018 * may have separate values for ->fe, ->be.
3019 */
Willy Tarreau06619262006-12-17 08:37:22 +01003020
Willy Tarreau59234e92008-11-30 23:51:27 +01003021 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003022 * If HTTP PROXY is set we simply get remote server address parsing
3023 * incoming request. Note that this requires that a connection is
3024 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003025 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003026 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003027 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003028 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003029
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003030 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003031 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003032 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003033 txn->req.msg_state = HTTP_MSG_ERROR;
3034 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003035 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003036 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003037
Willy Tarreaue7dff022015-04-03 01:14:29 +02003038 if (!(s->flags & SF_ERR_MASK))
3039 s->flags |= SF_ERR_RESOURCE;
3040 if (!(s->flags & SF_FINST_MASK))
3041 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003042
3043 return 0;
3044 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003045
Willy Tarreau6b952c82018-09-10 17:45:34 +02003046 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003047 if (url2sa(ci_head(req) + msg->sl.rq.u,
3048 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003049 &conn->addr.to, NULL) == -1)
3050 goto return_bad_req;
3051
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003052 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003053 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3054 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003055 * u_l characters by a single "/".
3056 */
3057 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003058 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003059 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3060 int delta;
3061
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003062 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003063 http_msg_move_end(&txn->req, delta);
3064 cur_end += delta;
3065 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3066 goto return_bad_req;
3067 }
3068 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003069 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003070 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3071 int delta;
3072
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003073 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003074 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003075 http_msg_move_end(&txn->req, delta);
3076 cur_end += delta;
3077 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3078 goto return_bad_req;
3079 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003080 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003081
Willy Tarreau59234e92008-11-30 23:51:27 +01003082 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003083 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003084 * Note that doing so might move headers in the request, but
3085 * the fields will stay coherent and the URI will not move.
3086 * This should only be performed in the backend.
3087 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003088 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003089 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003090
William Lallemanda73203e2012-03-12 12:48:57 +01003091 /* add unique-id if "header-unique-id" is specified */
3092
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003093 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003094 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003095 goto return_bad_req;
3096 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003097 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003098 }
William Lallemanda73203e2012-03-12 12:48:57 +01003099
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003100 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003101 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003102 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003103 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003104 goto return_bad_req;
3105 }
3106
Cyril Bontéb21570a2009-11-29 20:04:48 +01003107 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003108 * 9: add X-Forwarded-For if either the frontend or the backend
3109 * asks for it.
3110 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003111 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003112 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003113 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3114 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3115 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003116 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003117 /* The header is set to be added only if none is present
3118 * and we found it, so don't do anything.
3119 */
3120 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003121 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003122 /* Add an X-Forwarded-For header unless the source IP is
3123 * in the 'except' network range.
3124 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003125 if ((!sess->fe->except_mask.s_addr ||
3126 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3127 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003128 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003129 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003130 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003131 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003132 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003133 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003134
3135 /* Note: we rely on the backend to get the header name to be used for
3136 * x-forwarded-for, because the header is really meant for the backends.
3137 * However, if the backend did not specify any option, we have to rely
3138 * on the frontend's header name.
3139 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003140 if (s->be->fwdfor_hdr_len) {
3141 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003142 memcpy(trash.area,
3143 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003144 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003145 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003146 memcpy(trash.area,
3147 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003148 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003149 len += snprintf(trash.area + len,
3150 trash.size - len,
3151 ": %d.%d.%d.%d", pn[0], pn[1],
3152 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003153
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003154 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003155 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003156 }
3157 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003158 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003159 /* FIXME: for the sake of completeness, we should also support
3160 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003161 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003162 int len;
3163 char pn[INET6_ADDRSTRLEN];
3164 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003165 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003166 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003167
Willy Tarreau59234e92008-11-30 23:51:27 +01003168 /* Note: we rely on the backend to get the header name to be used for
3169 * x-forwarded-for, because the header is really meant for the backends.
3170 * However, if the backend did not specify any option, we have to rely
3171 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003172 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003173 if (s->be->fwdfor_hdr_len) {
3174 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003175 memcpy(trash.area, s->be->fwdfor_hdr_name,
3176 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003177 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003178 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003179 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3180 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003181 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003182 len += snprintf(trash.area + len, trash.size - len,
3183 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003184
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003185 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003186 goto return_bad_req;
3187 }
3188 }
3189
3190 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003191 * 10: add X-Original-To if either the frontend or the backend
3192 * asks for it.
3193 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003194 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003195
3196 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003197 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003198 /* Add an X-Original-To header unless the destination IP is
3199 * in the 'except' network range.
3200 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003201 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003202
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003203 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003204 ((!sess->fe->except_mask_to.s_addr ||
3205 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3206 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003207 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003208 (((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 +02003209 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003210 int len;
3211 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003212 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003213
3214 /* Note: we rely on the backend to get the header name to be used for
3215 * x-original-to, because the header is really meant for the backends.
3216 * However, if the backend did not specify any option, we have to rely
3217 * on the frontend's header name.
3218 */
3219 if (s->be->orgto_hdr_len) {
3220 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003221 memcpy(trash.area,
3222 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003223 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003224 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003225 memcpy(trash.area,
3226 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003227 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003228 len += snprintf(trash.area + len,
3229 trash.size - len,
3230 ": %d.%d.%d.%d", pn[0], pn[1],
3231 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003232
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003233 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003234 goto return_bad_req;
3235 }
3236 }
3237 }
3238
Willy Tarreau50fc7772012-11-11 22:19:57 +01003239 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3240 * If an "Upgrade" token is found, the header is left untouched in order not to have
3241 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3242 * "Upgrade" is present in the Connection header.
3243 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003244 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003245 unsigned int want_flags = 0;
3246
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003247 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003248 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003249 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003250 want_flags |= TX_CON_CLO_SET;
3251 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003252 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003253 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003254 want_flags |= TX_CON_KAL_SET;
3255 }
3256
3257 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003258 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003259 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003260
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003261
Willy Tarreau522d6c02009-12-06 18:49:18 +01003262 /* If we have no server assigned yet and we're balancing on url_param
3263 * with a POST request, we may be interested in checking the body for
3264 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003265 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003266 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003267 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003268 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003269 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003270 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003271 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003272
Christopher Fauletbe821b92017-03-30 11:21:53 +02003273 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3274 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003275#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003276 /* We expect some data from the client. Unless we know for sure
3277 * we already have a full request, we have to re-enable quick-ack
3278 * in case we previously disabled it, otherwise we might cause
3279 * the client to delay further data.
3280 */
3281 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3282 cli_conn && conn_ctrl_ready(cli_conn) &&
3283 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003284 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003285 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003286#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003287
Willy Tarreau59234e92008-11-30 23:51:27 +01003288 /*************************************************************
3289 * OK, that's finished for the headers. We have done what we *
3290 * could. Let's switch to the DATA state. *
3291 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003292 req->analyse_exp = TICK_ETERNITY;
3293 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003294
Willy Tarreau59234e92008-11-30 23:51:27 +01003295 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003296 /* OK let's go on with the BODY now */
3297 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003298
Willy Tarreau59234e92008-11-30 23:51:27 +01003299 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003300 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003301 /* we detected a parsing error. We want to archive this request
3302 * in the dedicated proxy area for later troubleshooting.
3303 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003304 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003305 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003306
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003307 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003308 txn->req.msg_state = HTTP_MSG_ERROR;
3309 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003310 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003311 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003312
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003313 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003314 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003315 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003316
Willy Tarreaue7dff022015-04-03 01:14:29 +02003317 if (!(s->flags & SF_ERR_MASK))
3318 s->flags |= SF_ERR_PRXCOND;
3319 if (!(s->flags & SF_FINST_MASK))
3320 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003321 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003322}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003323
Willy Tarreau60b85b02008-11-30 23:28:40 +01003324/* This function is an analyser which processes the HTTP tarpit. It always
3325 * returns zero, at the beginning because it prevents any other processing
3326 * from occurring, and at the end because it terminates the request.
3327 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003328int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003329{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003330 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003331
3332 /* This connection is being tarpitted. The CLIENT side has
3333 * already set the connect expiration date to the right
3334 * timeout. We just have to check that the client is still
3335 * there and that the timeout has not expired.
3336 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003337 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003338 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003339 !tick_is_expired(req->analyse_exp, now_ms))
3340 return 0;
3341
3342 /* We will set the queue timer to the time spent, just for
3343 * logging purposes. We fake a 500 server error, so that the
3344 * attacker will not suspect his connection has been tarpitted.
3345 * It will not cause trouble to the logs because we can exclude
3346 * the tarpitted connections by filtering on the 'PT' status flags.
3347 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003348 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3349
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003350 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003351 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003352
Christopher Faulet0184ea72017-01-05 14:06:34 +01003353 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003354 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003355
Willy Tarreaue7dff022015-04-03 01:14:29 +02003356 if (!(s->flags & SF_ERR_MASK))
3357 s->flags |= SF_ERR_PRXCOND;
3358 if (!(s->flags & SF_FINST_MASK))
3359 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003360 return 0;
3361}
3362
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003363/* This function is an analyser which waits for the HTTP request body. It waits
3364 * for either the buffer to be full, or the full advertised contents to have
3365 * reached the buffer. It must only be called after the standard HTTP request
3366 * processing has occurred, because it expects the request to be parsed and will
3367 * look for the Expect header. It may send a 100-Continue interim response. It
3368 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3369 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3370 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003371 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003372int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003373{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003374 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003375 struct http_txn *txn = s->txn;
3376 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003377
3378 /* We have to parse the HTTP request body to find any required data.
3379 * "balance url_param check_post" should have been the only way to get
3380 * into this. We were brought here after HTTP header analysis, so all
3381 * related structures are ready.
3382 */
3383
Willy Tarreau890988f2014-04-10 11:59:33 +02003384 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3385 /* This is the first call */
3386 if (msg->msg_state < HTTP_MSG_BODY)
3387 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003388
Willy Tarreau890988f2014-04-10 11:59:33 +02003389 if (msg->msg_state < HTTP_MSG_100_SENT) {
3390 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3391 * send an HTTP/1.1 100 Continue intermediate response.
3392 */
3393 if (msg->flags & HTTP_MSGF_VER_11) {
3394 struct hdr_ctx ctx;
3395 ctx.idx = 0;
3396 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003397 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003398 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003399 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003400 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003401 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003402 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003403 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003404 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003405
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003406 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003407 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003408 * must save the body in msg->next because it survives buffer
3409 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003410 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003411 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003412
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003413 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003414 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3415 else
3416 msg->msg_state = HTTP_MSG_DATA;
3417 }
3418
Willy Tarreau890988f2014-04-10 11:59:33 +02003419 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3420 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003421 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003422 goto missing_data;
3423
3424 /* OK we have everything we need now */
3425 goto http_end;
3426 }
3427
3428 /* OK here we're parsing a chunked-encoded message */
3429
Willy Tarreau522d6c02009-12-06 18:49:18 +01003430 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003431 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003432 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003433 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003434 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003435 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003436 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003437
Willy Tarreau115acb92009-12-26 13:56:06 +01003438 if (!ret)
3439 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003440 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003441 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003442 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003443 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003444 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003445 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003446 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003447
3448 msg->chunk_len = chunk;
3449 msg->body_len += chunk;
3450
3451 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003452 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003453 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003454 }
3455
Willy Tarreaud98cf932009-12-27 22:54:55 +01003456 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003457 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3458 * for at least a whole chunk or the whole content length bytes after
3459 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003460 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003461 if (msg->msg_state == HTTP_MSG_TRAILERS)
3462 goto http_end;
3463
Willy Tarreaue115b492015-05-01 23:05:14 +02003464 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003465 goto http_end;
3466
3467 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003468 /* we get here if we need to wait for more data. If the buffer is full,
3469 * we have the maximum we can expect.
3470 */
Willy Tarreau23752332018-06-15 14:54:53 +02003471 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003472 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003473
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003474 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003475 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003476 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003477
Willy Tarreaue7dff022015-04-03 01:14:29 +02003478 if (!(s->flags & SF_ERR_MASK))
3479 s->flags |= SF_ERR_CLITO;
3480 if (!(s->flags & SF_FINST_MASK))
3481 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003482 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003483 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003484
3485 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003486 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003487 /* Not enough data. We'll re-use the http-request
3488 * timeout here. Ideally, we should set the timeout
3489 * relative to the accept() date. We just set the
3490 * request timeout once at the beginning of the
3491 * request.
3492 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003493 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003494 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003495 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003496 return 0;
3497 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003498
3499 http_end:
3500 /* The situation will not evolve, so let's give up on the analysis. */
3501 s->logs.tv_request = now; /* update the request timer to reflect full request */
3502 req->analysers &= ~an_bit;
3503 req->analyse_exp = TICK_ETERNITY;
3504 return 1;
3505
3506 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003507 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003508 txn->req.msg_state = HTTP_MSG_ERROR;
3509 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003510 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003511
Willy Tarreaue7dff022015-04-03 01:14:29 +02003512 if (!(s->flags & SF_ERR_MASK))
3513 s->flags |= SF_ERR_PRXCOND;
3514 if (!(s->flags & SF_FINST_MASK))
3515 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003516
Willy Tarreau522d6c02009-12-06 18:49:18 +01003517 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003518 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003519 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003520 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003521 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003522 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003523}
3524
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003525/* send a server's name with an outgoing request over an established connection.
3526 * Note: this function is designed to be called once the request has been scheduled
3527 * for being forwarded. This is the reason why it rewinds the buffer before
3528 * proceeding.
3529 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003530int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003531
3532 struct hdr_ctx ctx;
3533
Mark Lamourinec2247f02012-01-04 13:02:01 -05003534 char *hdr_name = be->server_id_hdr_name;
3535 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003536 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003537 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003538 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003539
William Lallemandd9e90662012-01-30 17:27:17 +01003540 ctx.idx = 0;
3541
Willy Tarreau211cdec2014-04-17 20:18:08 +02003542 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003543 if (old_o) {
3544 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003545 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003546 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003547 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003548 }
3549
Willy Tarreauf37954d2018-06-15 18:31:02 +02003550 old_i = ci_data(chn);
3551 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 -05003552 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003553 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003554 }
3555
3556 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003557 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003558 memcpy(hdr_val, hdr_name, hdr_name_len);
3559 hdr_val += hdr_name_len;
3560 *hdr_val++ = ':';
3561 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003562 hdr_val += strlcpy2(hdr_val, srv_name,
3563 trash.area + trash.size - hdr_val);
3564 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3565 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003566
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003567 if (old_o) {
3568 /* If this was a forwarded request, we must readjust the amount of
3569 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003570 * variations. Note that the current state is >= HTTP_MSG_BODY,
3571 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003572 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003573 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003574 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003575 txn->req.next -= old_o;
3576 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003577 }
3578
Mark Lamourinec2247f02012-01-04 13:02:01 -05003579 return 0;
3580}
3581
Willy Tarreau610ecce2010-01-04 21:15:02 +01003582/* Terminate current transaction and prepare a new one. This is very tricky
3583 * right now but it works.
3584 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003585void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003586{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003587 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003588 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003589 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003590 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003591 struct connection *srv_conn;
3592 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003593 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003594
Willy Tarreau610ecce2010-01-04 21:15:02 +01003595 /* FIXME: We need a more portable way of releasing a backend's and a
3596 * server's connections. We need a safer way to reinitialize buffer
3597 * flags. We also need a more accurate method for computing per-request
3598 * data.
3599 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003600 /*
3601 * XXX cognet: This is probably wrong, this is killing a whole
3602 * connection, in the new world order, we probably want to just kill
3603 * the stream, this is to be revisited the day we handle multiple
3604 * streams in one server connection.
3605 */
3606 cs = objt_cs(s->si[1].end);
3607 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003608
Willy Tarreau4213a112013-12-15 10:25:42 +01003609 /* unless we're doing keep-alive, we want to quickly close the connection
3610 * to the server.
3611 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003612 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003613 !si_conn_ready(&s->si[1])) {
3614 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3615 si_shutr(&s->si[1]);
3616 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003617 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003618
Willy Tarreaue7dff022015-04-03 01:14:29 +02003619 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003620 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003621 if (unlikely(s->srv_conn))
3622 sess_change_server(s, NULL);
3623 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003624
3625 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003626 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003627
Willy Tarreaueee5b512015-04-03 23:46:31 +02003628 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003629 int n;
3630
Willy Tarreaueee5b512015-04-03 23:46:31 +02003631 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003632 if (n < 1 || n > 5)
3633 n = 0;
3634
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003635 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003636 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003637 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003638 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003639 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003640 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3641 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003642 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003643 }
3644
3645 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003646 s->logs.bytes_in -= ci_data(&s->req);
3647 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003648
Willy Tarreau66425e32018-07-25 06:55:12 +02003649 /* we may need to know the position in the queue */
3650 pendconn_free(s);
3651
Willy Tarreau610ecce2010-01-04 21:15:02 +01003652 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003653 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003654 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003655 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003656 s->do_log(s);
3657 }
3658
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003659 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003660 stream_stop_content_counters(s);
3661 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003662
Willy Tarreau610ecce2010-01-04 21:15:02 +01003663 s->logs.accept_date = date; /* user-visible date for logging */
3664 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003665 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3666 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003667 tv_zero(&s->logs.tv_request);
3668 s->logs.t_queue = -1;
3669 s->logs.t_connect = -1;
3670 s->logs.t_data = -1;
3671 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003672 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3673 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003674
Willy Tarreauf37954d2018-06-15 18:31:02 +02003675 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3676 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003677
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003678 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003679 if (s->flags & SF_CURR_SESS) {
3680 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02003681 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003682 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003683 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003684 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003685 }
3686
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003687 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003688
Willy Tarreau4213a112013-12-15 10:25:42 +01003689 /* only release our endpoint if we don't intend to reuse the
3690 * connection.
3691 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003692 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003693 !si_conn_ready(&s->si[1])) {
3694 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02003695 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003696 }
3697
Willy Tarreau350f4872014-11-28 14:42:25 +01003698 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3699 s->si[1].err_type = SI_ET_NONE;
3700 s->si[1].conn_retries = 0; /* used for logging too */
3701 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003702 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 +01003703 s->req.flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WAKE_CONNECT|CF_WROTE_DATA);
Christopher Fauletc5a9d5b2017-11-09 09:36:43 +01003704 s->res.flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA|CF_WRITE_EVENT);
Willy Tarreaue7dff022015-04-03 01:14:29 +02003705 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3706 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3707 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003708
Patrick Hemmere3faf022018-08-22 10:02:00 -04003709 hlua_ctx_destroy(s->hlua);
3710 s->hlua = NULL;
3711
Willy Tarreaueee5b512015-04-03 23:46:31 +02003712 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003713 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003714 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003715
3716 if (prev_status == 401 || prev_status == 407) {
3717 /* In HTTP keep-alive mode, if we receive a 401, we still have
3718 * a chance of being able to send the visitor again to the same
3719 * server over the same connection. This is required by some
3720 * broken protocols such as NTLM, and anyway whenever there is
3721 * an opportunity for sending the challenge to the proper place,
3722 * it's better to do it (at least it helps with debugging).
3723 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003724 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02003725 if (srv_conn)
3726 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01003727 }
3728
Willy Tarreau53f96852016-02-02 18:50:47 +01003729 /* Never ever allow to reuse a connection from a non-reuse backend */
3730 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3731 srv_conn->flags |= CO_FL_PRIVATE;
3732
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003733 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003734 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003735
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003736 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003737 s->req.flags |= CF_NEVER_WAIT;
3738 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003739 }
3740
Willy Tarreau714ea782015-11-25 20:11:11 +01003741 /* we're removing the analysers, we MUST re-enable events detection.
3742 * We don't enable close on the response channel since it's either
3743 * already closed, or in keep-alive with an idle connection handler.
3744 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003745 channel_auto_read(&s->req);
3746 channel_auto_close(&s->req);
3747 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003748
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003749 /* we're in keep-alive with an idle connection, monitor it if not already done */
3750 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003751 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02003752 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003753 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01003754 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003755 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02003756 else if (prev_flags & TX_NOT_FIRST)
3757 /* note: we check the request, not the connection, but
3758 * this is valid for strategies SAFE and AGGR, and in
3759 * case of ALWS, we don't care anyway.
3760 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003761 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02003762 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003763 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003764 }
Christopher Faulete6006242017-03-10 11:52:44 +01003765 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3766 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003767}
3768
3769
3770/* This function updates the request state machine according to the response
3771 * state machine and buffer flags. It returns 1 if it changes anything (flag
3772 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3773 * it is only used to find when a request/response couple is complete. Both
3774 * this function and its equivalent should loop until both return zero. It
3775 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3776 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003777int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003778{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003779 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003780 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003781 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003782 unsigned int old_state = txn->req.msg_state;
3783
Christopher Faulet4be98032017-07-18 10:48:24 +02003784 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003785 return 0;
3786
3787 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003788 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003789 * We can shut the read side unless we want to abort_on_close,
3790 * or we have a POST request. The issue with POST requests is
3791 * that some browsers still send a CRLF after the request, and
3792 * this CRLF must be read so that it does not remain in the kernel
3793 * buffers, otherwise a close could cause an RST on some systems
3794 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003795 * Note that if we're using keep-alive on the client side, we'd
3796 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003797 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003798 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003799 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003800 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3801 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003802 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3803 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003804 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003805 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003806
Willy Tarreau40f151a2012-12-20 12:10:09 +01003807 /* if the server closes the connection, we want to immediately react
3808 * and close the socket to save packets and syscalls.
3809 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003810 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003811
Willy Tarreau7f876a12015-11-18 11:59:55 +01003812 /* In any case we've finished parsing the request so we must
3813 * disable Nagle when sending data because 1) we're not going
3814 * to shut this side, and 2) the server is waiting for us to
3815 * send pending data.
3816 */
3817 chn->flags |= CF_NEVER_WAIT;
3818
Willy Tarreau610ecce2010-01-04 21:15:02 +01003819 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3820 goto wait_other_side;
3821
3822 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3823 /* The server has not finished to respond, so we
3824 * don't want to move in order not to upset it.
3825 */
3826 goto wait_other_side;
3827 }
3828
Willy Tarreau610ecce2010-01-04 21:15:02 +01003829 /* When we get here, it means that both the request and the
3830 * response have finished receiving. Depending on the connection
3831 * mode, we'll have to wait for the last bytes to leave in either
3832 * direction, and sometimes for a close to be effective.
3833 */
3834
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003835 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3836 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003837 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3838 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003839 }
3840 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3841 /* Option forceclose is set, or either side wants to close,
3842 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003843 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003844 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003845 *
3846 * However, there is an exception if the response
3847 * length is undefined. In this case, we need to wait
3848 * the close from the server. The response will be
3849 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003850 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003851 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3852 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3853 goto check_channel_flags;
3854
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003855 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3856 channel_shutr_now(chn);
3857 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003858 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003859 }
3860 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003861 /* The last possible modes are keep-alive and tunnel. Tunnel mode
3862 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003863 */
Willy Tarreau4213a112013-12-15 10:25:42 +01003864 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
3865 channel_auto_read(chn);
3866 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003867 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003868 }
3869
Christopher Faulet4be98032017-07-18 10:48:24 +02003870 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003871 }
3872
3873 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3874 http_msg_closing:
3875 /* nothing else to forward, just waiting for the output buffer
3876 * to be empty and for the shutw_now to take effect.
3877 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003878 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003879 txn->req.msg_state = HTTP_MSG_CLOSED;
3880 goto http_msg_closed;
3881 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003882 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003883 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003884 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003885 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003886 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003887 }
3888
3889 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3890 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01003891 /* if we don't know whether the server will close, we need to hard close */
3892 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
3893 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3894
Willy Tarreau3988d932013-12-27 23:03:08 +01003895 /* see above in MSG_DONE why we only do this in these states */
3896 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3897 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003898 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3899 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01003900 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003901 goto wait_other_side;
3902 }
3903
Christopher Faulet4be98032017-07-18 10:48:24 +02003904 check_channel_flags:
3905 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
3906 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
3907 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02003908 txn->req.msg_state = HTTP_MSG_CLOSING;
3909 goto http_msg_closing;
3910 }
3911
3912
Willy Tarreau610ecce2010-01-04 21:15:02 +01003913 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003914 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003915}
3916
3917
3918/* This function updates the response state machine according to the request
3919 * state machine and buffer flags. It returns 1 if it changes anything (flag
3920 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3921 * it is only used to find when a request/response couple is complete. Both
3922 * this function and its equivalent should loop until both return zero. It
3923 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3924 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003925int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003926{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003927 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003928 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003929 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003930 unsigned int old_state = txn->rsp.msg_state;
3931
Christopher Faulet4be98032017-07-18 10:48:24 +02003932 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003933 return 0;
3934
3935 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3936 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003937 * still monitor the server connection for a possible close
3938 * while the request is being uploaded, so we don't disable
3939 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003940 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003941 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003942
3943 if (txn->req.msg_state == HTTP_MSG_ERROR)
3944 goto wait_other_side;
3945
3946 if (txn->req.msg_state < HTTP_MSG_DONE) {
3947 /* The client seems to still be sending data, probably
3948 * because we got an error response during an upload.
3949 * We have the choice of either breaking the connection
3950 * or letting it pass through. Let's do the later.
3951 */
3952 goto wait_other_side;
3953 }
3954
Willy Tarreau610ecce2010-01-04 21:15:02 +01003955 /* When we get here, it means that both the request and the
3956 * response have finished receiving. Depending on the connection
3957 * mode, we'll have to wait for the last bytes to leave in either
3958 * direction, and sometimes for a close to be effective.
3959 */
3960
3961 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3962 /* Server-close mode : shut read and wait for the request
3963 * side to close its output buffer. The caller will detect
3964 * when we're in DONE and the other is in CLOSED and will
3965 * catch that for the final cleanup.
3966 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003967 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
3968 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003969 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003970 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3971 /* Option forceclose is set, or either side wants to close,
3972 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003973 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003974 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003975 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01003976 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003977 channel_shutr_now(chn);
3978 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003979 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003980 }
3981 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003982 /* The last possible modes are keep-alive and tunnel. Tunnel will
3983 * need to forward remaining data. Keep-alive will need to monitor
3984 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003985 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003986 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02003987 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01003988 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
3989 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003990 }
3991
Christopher Faulet4be98032017-07-18 10:48:24 +02003992 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003993 }
3994
3995 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3996 http_msg_closing:
3997 /* nothing else to forward, just waiting for the output buffer
3998 * to be empty and for the shutw_now to take effect.
3999 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004000 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004001 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4002 goto http_msg_closed;
4003 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004004 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004005 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004006 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004007 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004008 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004009 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004010 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004011 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004012 }
4013
4014 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4015 http_msg_closed:
4016 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004017 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004018 channel_auto_close(chn);
4019 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004020 goto wait_other_side;
4021 }
4022
Christopher Faulet4be98032017-07-18 10:48:24 +02004023 check_channel_flags:
4024 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4025 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4026 /* if we've just closed an output, let's switch */
4027 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4028 goto http_msg_closing;
4029 }
4030
Willy Tarreau610ecce2010-01-04 21:15:02 +01004031 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004032 /* We force the response to leave immediately if we're waiting for the
4033 * other side, since there is no pending shutdown to push it out.
4034 */
4035 if (!channel_is_empty(chn))
4036 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004037 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004038}
4039
4040
Christopher Faulet894da4c2017-07-18 11:29:07 +02004041/* Resync the request and response state machines. */
4042void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004043{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004044 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004045#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004046 int old_req_state = txn->req.msg_state;
4047 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004048#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004049
Willy Tarreau610ecce2010-01-04 21:15:02 +01004050 http_sync_req_state(s);
4051 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004052 if (!http_sync_res_state(s))
4053 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004054 if (!http_sync_req_state(s))
4055 break;
4056 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004057
Christopher Faulet894da4c2017-07-18 11:29:07 +02004058 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4059 "req->analysers=0x%08x res->analysers=0x%08x\n",
4060 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004061 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4062 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004063 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004064
4065
Willy Tarreau610ecce2010-01-04 21:15:02 +01004066 /* OK, both state machines agree on a compatible state.
4067 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004068 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4069 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004070 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4071 * means we must abort the request.
4072 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4073 * corresponding channel.
4074 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4075 * on the response with server-close mode means we've completed one
4076 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004077 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004078 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4079 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004080 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004081 channel_auto_close(&s->req);
4082 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004083 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004084 channel_auto_close(&s->res);
4085 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004086 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004087 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4088 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004089 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004090 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004091 channel_auto_close(&s->res);
4092 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004093 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004094 channel_abort(&s->req);
4095 channel_auto_close(&s->req);
4096 channel_auto_read(&s->req);
4097 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004098 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004099 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4100 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4101 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4102 s->req.analysers &= AN_REQ_FLT_END;
4103 if (HAS_REQ_DATA_FILTERS(s))
4104 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4105 }
4106 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4107 s->res.analysers &= AN_RES_FLT_END;
4108 if (HAS_RSP_DATA_FILTERS(s))
4109 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4110 }
4111 channel_auto_close(&s->req);
4112 channel_auto_read(&s->req);
4113 channel_auto_close(&s->res);
4114 channel_auto_read(&s->res);
4115 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004116 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4117 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004118 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004119 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4120 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4121 /* server-close/keep-alive: terminate this transaction,
4122 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004123 * a fresh-new transaction, but only once we're sure there's
4124 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004125 * another request. They must not hold any pending output data
4126 * and the response buffer must realigned
4127 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004128 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004129 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004130 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004131 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004132 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004133 else {
4134 s->req.analysers = AN_REQ_FLT_END;
4135 s->res.analysers = AN_RES_FLT_END;
4136 txn->flags |= TX_WAIT_CLEANUP;
4137 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004138 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004139}
4140
Willy Tarreaud98cf932009-12-27 22:54:55 +01004141/* This function is an analyser which forwards request body (including chunk
4142 * sizes if any). It is called as soon as we must forward, even if we forward
4143 * zero byte. The only situation where it must not be called is when we're in
4144 * tunnel mode and we want to forward till the close. It's used both to forward
4145 * remaining data and to resync after end of body. It expects the msg_state to
4146 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004147 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004148 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004149 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004150 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004151int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004152{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004153 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004154 struct http_txn *txn = s->txn;
4155 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004156 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004157
Christopher Faulet45073512018-07-20 10:16:29 +02004158 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 +02004159 now_ms, __FUNCTION__,
4160 s,
4161 req,
4162 req->rex, req->wex,
4163 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004164 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004165 req->analysers);
4166
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004167 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4168 return 0;
4169
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004170 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004171 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004172 /* Output closed while we were sending data. We must abort and
4173 * wake the other side up.
4174 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004175 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004176 msg->msg_state = HTTP_MSG_ERROR;
4177 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004178 return 1;
4179 }
4180
Willy Tarreaud98cf932009-12-27 22:54:55 +01004181 /* Note that we don't have to send 100-continue back because we don't
4182 * need the data to complete our job, and it's up to the server to
4183 * decide whether to return 100, 417 or anything else in return of
4184 * an "Expect: 100-continue" header.
4185 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004186 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004187 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4188 ? HTTP_MSG_CHUNK_SIZE
4189 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004190
4191 /* TODO/filters: when http-buffer-request option is set or if a
4192 * rule on url_param exists, the first chunk size could be
4193 * already parsed. In that case, msg->next is after the chunk
4194 * size (including the CRLF after the size). So this case should
4195 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004196 }
4197
Willy Tarreau7ba23542014-04-17 21:50:00 +02004198 /* Some post-connect processing might want us to refrain from starting to
4199 * forward data. Currently, the only reason for this is "balance url_param"
4200 * whichs need to parse/process the request after we've enabled forwarding.
4201 */
4202 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004203 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004204 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004205 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004206 channel_dont_close(req); /* don't fail on early shutr */
4207 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004208 }
4209 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4210 }
4211
Willy Tarreau80a92c02014-03-12 10:41:13 +01004212 /* in most states, we should abort in case of early close */
4213 channel_auto_close(req);
4214
Willy Tarreauefdf0942014-04-24 20:08:57 +02004215 if (req->to_forward) {
4216 /* We can't process the buffer's contents yet */
4217 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004218 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004219 }
4220
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004221 if (msg->msg_state < HTTP_MSG_DONE) {
4222 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4223 ? http_msg_forward_chunked_body(s, msg)
4224 : http_msg_forward_body(s, msg));
4225 if (!ret)
4226 goto missing_data_or_waiting;
4227 if (ret < 0)
4228 goto return_bad_req;
4229 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004230
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004231 /* other states, DONE...TUNNEL */
4232 /* we don't want to forward closes on DONE except in tunnel mode. */
4233 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4234 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004235
Christopher Faulet894da4c2017-07-18 11:29:07 +02004236 http_resync_states(s);
4237 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004238 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4239 if (req->flags & CF_SHUTW) {
4240 /* request errors are most likely due to the
4241 * server aborting the transfer. */
4242 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004243 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004244 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004245 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004246 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004247 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004248 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004249 }
4250
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004251 /* If "option abortonclose" is set on the backend, we want to monitor
4252 * the client's connection and forward any shutdown notification to the
4253 * server, which will decide whether to close or to go on processing the
4254 * request. We only do that in tunnel mode, and not in other modes since
4255 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004256 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004257 channel_auto_read(req);
4258 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4259 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4260 s->si[1].flags |= SI_FL_NOLINGER;
4261 channel_auto_close(req);
4262 }
4263 else if (s->txn->meth == HTTP_METH_POST) {
4264 /* POST requests may require to read extra CRLF sent by broken
4265 * browsers and which could cause an RST to be sent upon close
4266 * on some systems (eg: Linux). */
4267 channel_auto_read(req);
4268 }
4269 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004270
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004271 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004272 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004273 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004274 if (!(s->flags & SF_ERR_MASK))
4275 s->flags |= SF_ERR_CLICL;
4276 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004277 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004278 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004279 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004280 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004281 }
4282
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004283 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4284 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004285 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004286 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004287
4288 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004289 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004290
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004291 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004292 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004293 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004294 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004295
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004296 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004297 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004298 * And when content-length is used, we never want to let the possible
4299 * shutdown be forwarded to the other side, as the state machine will
4300 * take care of it once the client responds. It's also important to
4301 * prevent TIME_WAITs from accumulating on the backend side, and for
4302 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004303 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004304 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004305 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004306
Willy Tarreau5c620922011-05-11 19:56:11 +02004307 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004308 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004309 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004310 * modes are already handled by the stream sock layer. We must not do
4311 * this in content-length mode because it could present the MSG_MORE
4312 * flag with the last block of forwarded data, which would cause an
4313 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004314 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004315 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004316 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004317
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004318 return 0;
4319
Willy Tarreaud98cf932009-12-27 22:54:55 +01004320 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004321 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004322 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004323 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004324
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004325 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004326 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004327 txn->req.msg_state = HTTP_MSG_ERROR;
4328 if (txn->status) {
4329 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004330 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004331 } else {
4332 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004333 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004334 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004335 req->analysers &= AN_REQ_FLT_END;
4336 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 +01004337
Willy Tarreaue7dff022015-04-03 01:14:29 +02004338 if (!(s->flags & SF_ERR_MASK))
4339 s->flags |= SF_ERR_PRXCOND;
4340 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004341 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004342 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004343 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004344 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004345 }
4346 return 0;
4347
4348 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004349 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004350 txn->req.msg_state = HTTP_MSG_ERROR;
4351 if (txn->status) {
4352 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004353 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004354 } else {
4355 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004356 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004357 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004358 req->analysers &= AN_REQ_FLT_END;
4359 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 +01004360
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004361 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4362 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004363 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004364 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004365
Willy Tarreaue7dff022015-04-03 01:14:29 +02004366 if (!(s->flags & SF_ERR_MASK))
4367 s->flags |= SF_ERR_SRVCL;
4368 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004369 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004370 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004371 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004372 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004373 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004374 return 0;
4375}
4376
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004377/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4378 * processing can continue on next analysers, or zero if it either needs more
4379 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004380 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004381 * when it has nothing left to do, and may remove any analyser when it wants to
4382 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004383 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004384int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004385{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004386 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004387 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004388 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004389 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004390 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004391 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004392 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004393
Christopher Faulet45073512018-07-20 10:16:29 +02004394 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 +02004395 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004396 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004397 rep,
4398 rep->rex, rep->wex,
4399 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004400 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004401 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004402
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004403 /*
4404 * Now parse the partial (or complete) lines.
4405 * We will check the response syntax, and also join multi-line
4406 * headers. An index of all the lines will be elaborated while
4407 * parsing.
4408 *
4409 * For the parsing, we use a 28 states FSM.
4410 *
4411 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004412 * ci_head(rep) = beginning of response
4413 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4414 * ci_tail(rep) = end of input data
4415 * msg->eol = end of current header or line (LF or CRLF)
4416 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004417 */
4418
Willy Tarreau628c40c2014-04-24 19:11:26 +02004419 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004420 /* There's a protected area at the end of the buffer for rewriting
4421 * purposes. We don't want to start to parse the request if the
4422 * protected area is affected, because we may have to move processed
4423 * data later, which is much more complicated.
4424 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004425 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004426 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004427 /* some data has still not left the buffer, wake us once that's done */
4428 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4429 goto abort_response;
4430 channel_dont_close(rep);
4431 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004432 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004433 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004434 }
4435
Willy Tarreau188e2302018-06-15 11:11:53 +02004436 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004437 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004438 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004439
Willy Tarreauf37954d2018-06-15 18:31:02 +02004440 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004441 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004442 }
4443
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004444 /* 1: we might have to print this header in debug mode */
4445 if (unlikely((global.mode & MODE_DEBUG) &&
4446 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004447 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004448 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004449
Willy Tarreauf37954d2018-06-15 18:31:02 +02004450 sol = ci_head(rep);
4451 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004452 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004453
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004454 sol += hdr_idx_first_pos(&txn->hdr_idx);
4455 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004456
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004457 while (cur_idx) {
4458 eol = sol + txn->hdr_idx.v[cur_idx].len;
4459 debug_hdr("srvhdr", s, sol, eol);
4460 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4461 cur_idx = txn->hdr_idx.v[cur_idx].next;
4462 }
4463 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004464
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004465 /*
4466 * Now we quickly check if we have found a full valid response.
4467 * If not so, we check the FD and buffer states before leaving.
4468 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004469 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004470 * responses are checked first.
4471 *
4472 * Depending on whether the client is still there or not, we
4473 * may send an error response back or not. Note that normally
4474 * we should only check for HTTP status there, and check I/O
4475 * errors somewhere else.
4476 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004477
Willy Tarreau655dce92009-11-08 13:10:58 +01004478 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004479 /* Invalid response */
4480 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4481 /* we detected a parsing error. We want to archive this response
4482 * in the dedicated proxy area for later troubleshooting.
4483 */
4484 hdr_response_bad:
4485 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004486 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004487
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004488 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004489 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004490 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004491 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004492 }
Willy Tarreau64648412010-03-05 10:41:54 +01004493 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004494 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004495 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004496 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004497 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004498 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004499 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004500
Willy Tarreaue7dff022015-04-03 01:14:29 +02004501 if (!(s->flags & SF_ERR_MASK))
4502 s->flags |= SF_ERR_PRXCOND;
4503 if (!(s->flags & SF_FINST_MASK))
4504 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004505
4506 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004507 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004508
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004509 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004510 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004511 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004512 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004513 goto hdr_response_bad;
4514 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004515
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004516 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004517 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004518 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004519 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004520 else if (txn->flags & TX_NOT_FIRST)
4521 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004522
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004523 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004524 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004525 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004526 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004527 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004528
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004529 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004530 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004531 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004532
4533 /* Check to see if the server refused the early data.
4534 * If so, just send a 425
4535 */
4536 if (objt_cs(s->si[1].end)) {
4537 struct connection *conn = objt_cs(s->si[1].end)->conn;
4538
4539 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4540 txn->status = 425;
4541 }
4542
Willy Tarreau350f4872014-11-28 14:42:25 +01004543 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004544 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004545 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004546
Willy Tarreaue7dff022015-04-03 01:14:29 +02004547 if (!(s->flags & SF_ERR_MASK))
4548 s->flags |= SF_ERR_SRVCL;
4549 if (!(s->flags & SF_FINST_MASK))
4550 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004551 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004552 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004553
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004554 /* read timeout : return a 504 to the client. */
4555 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004556 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004557 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004558
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004559 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004560 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004561 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004562 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004563 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004564
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004565 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004566 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004567 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004568 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004569 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004570 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004571
Willy Tarreaue7dff022015-04-03 01:14:29 +02004572 if (!(s->flags & SF_ERR_MASK))
4573 s->flags |= SF_ERR_SRVTO;
4574 if (!(s->flags & SF_FINST_MASK))
4575 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004576 return 0;
4577 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004578
Willy Tarreauf003d372012-11-26 13:35:37 +01004579 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004580 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004581 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4582 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004583 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004584 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004585
Christopher Faulet0184ea72017-01-05 14:06:34 +01004586 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004587 channel_auto_close(rep);
4588
4589 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004590 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004591 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004592
Willy Tarreaue7dff022015-04-03 01:14:29 +02004593 if (!(s->flags & SF_ERR_MASK))
4594 s->flags |= SF_ERR_CLICL;
4595 if (!(s->flags & SF_FINST_MASK))
4596 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004597
Willy Tarreau87b09662015-04-03 00:22:06 +02004598 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004599 return 0;
4600 }
4601
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004602 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004603 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004604 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004605 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004606 else if (txn->flags & TX_NOT_FIRST)
4607 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004608
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004609 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004610 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004611 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004612 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004613 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004614
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004615 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004616 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004617 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004618 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004619 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004620 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004621
Willy Tarreaue7dff022015-04-03 01:14:29 +02004622 if (!(s->flags & SF_ERR_MASK))
4623 s->flags |= SF_ERR_SRVCL;
4624 if (!(s->flags & SF_FINST_MASK))
4625 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004626 return 0;
4627 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004628
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004629 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004630 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004631 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004632 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004633 else if (txn->flags & TX_NOT_FIRST)
4634 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004635
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004636 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004637 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004638 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004639
Willy Tarreaue7dff022015-04-03 01:14:29 +02004640 if (!(s->flags & SF_ERR_MASK))
4641 s->flags |= SF_ERR_CLICL;
4642 if (!(s->flags & SF_FINST_MASK))
4643 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004644
Willy Tarreau87b09662015-04-03 00:22:06 +02004645 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004646 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004647 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004648
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004649 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004650 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004651 return 0;
4652 }
4653
4654 /* More interesting part now : we know that we have a complete
4655 * response which at least looks like HTTP. We have an indicator
4656 * of each header's length, so we can parse them quickly.
4657 */
4658
4659 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004660 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004661
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004662 /*
4663 * 1: get the status code
4664 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004665 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004666 if (n < 1 || n > 5)
4667 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004668 /* when the client triggers a 4xx from the server, it's most often due
4669 * to a missing object or permission. These events should be tracked
4670 * because if they happen often, it may indicate a brute force or a
4671 * vulnerability scan.
4672 */
4673 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004674 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004675
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004676 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004677 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004678
Willy Tarreau91852eb2015-05-01 13:26:00 +02004679 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4680 * exactly one digit "." one digit. This check may be disabled using
4681 * option accept-invalid-http-response.
4682 */
4683 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4684 if (msg->sl.st.v_l != 8) {
4685 msg->err_pos = 0;
4686 goto hdr_response_bad;
4687 }
4688
Willy Tarreauf37954d2018-06-15 18:31:02 +02004689 if (ci_head(rep)[4] != '/' ||
4690 !isdigit((unsigned char)ci_head(rep)[5]) ||
4691 ci_head(rep)[6] != '.' ||
4692 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004693 msg->err_pos = 4;
4694 goto hdr_response_bad;
4695 }
4696 }
4697
Willy Tarreau5b154472009-12-21 20:11:07 +01004698 /* check if the response is HTTP/1.1 or above */
4699 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004700 ((ci_head(rep)[5] > '1') ||
4701 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004702 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004703
4704 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004705 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 +01004706
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004707 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004708 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004709
Willy Tarreauf37954d2018-06-15 18:31:02 +02004710 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004711
Willy Tarreau39650402010-03-15 19:44:39 +01004712 /* Adjust server's health based on status code. Note: status codes 501
4713 * and 505 are triggered on demand by client request, so we must not
4714 * count them as server failures.
4715 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004716 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004717 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004718 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004719 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004720 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004721 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004722
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004723 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004724 * We may be facing a 100-continue response, or any other informational
4725 * 1xx response which is non-final, in which case this is not the right
4726 * response, and we're waiting for the next one. Let's allow this response
4727 * to go to the client and wait for the next one. There's an exception for
4728 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004729 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004730 if (txn->status < 200 &&
4731 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004732 hdr_idx_init(&txn->hdr_idx);
4733 msg->next -= channel_forward(rep, msg->next);
4734 msg->msg_state = HTTP_MSG_RPBEFORE;
4735 txn->status = 0;
4736 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004737 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004738 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004739 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004740
Willy Tarreaua14ad722017-07-07 11:36:32 +02004741 /*
4742 * 2: check for cacheability.
4743 */
4744
4745 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004746 case 200:
4747 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004748 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004749 case 206:
4750 case 300:
4751 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004752 case 404:
4753 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004754 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004755 case 414:
4756 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004757 break;
4758 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004759 /* RFC7231#6.1:
4760 * Responses with status codes that are defined as
4761 * cacheable by default (e.g., 200, 203, 204, 206,
4762 * 300, 301, 404, 405, 410, 414, and 501 in this
4763 * specification) can be reused by a cache with
4764 * heuristic expiration unless otherwise indicated
4765 * by the method definition or explicit cache
4766 * controls [RFC7234]; all other status codes are
4767 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004768 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004769 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004770 break;
4771 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004772
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004773 /*
4774 * 3: we may need to capture headers
4775 */
4776 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004777 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004778 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4779 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004780
Willy Tarreau557f1992015-05-01 10:05:17 +02004781 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4782 * by RFC7230#3.3.3 :
4783 *
4784 * The length of a message body is determined by one of the following
4785 * (in order of precedence):
4786 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004787 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4788 * the connection will become a tunnel immediately after the empty
4789 * line that concludes the header fields. A client MUST ignore
4790 * any Content-Length or Transfer-Encoding header fields received
4791 * in such a message. Any 101 response (Switching Protocols) is
4792 * managed in the same manner.
4793 *
4794 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004795 * (Informational), 204 (No Content), or 304 (Not Modified) status
4796 * code is always terminated by the first empty line after the
4797 * header fields, regardless of the header fields present in the
4798 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004799 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004800 * 3. If a Transfer-Encoding header field is present and the chunked
4801 * transfer coding (Section 4.1) is the final encoding, the message
4802 * body length is determined by reading and decoding the chunked
4803 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004804 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004805 * If a Transfer-Encoding header field is present in a response and
4806 * the chunked transfer coding is not the final encoding, the
4807 * message body length is determined by reading the connection until
4808 * it is closed by the server. If a Transfer-Encoding header field
4809 * is present in a request and the chunked transfer coding is not
4810 * the final encoding, the message body length cannot be determined
4811 * reliably; the server MUST respond with the 400 (Bad Request)
4812 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004813 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004814 * If a message is received with both a Transfer-Encoding and a
4815 * Content-Length header field, the Transfer-Encoding overrides the
4816 * Content-Length. Such a message might indicate an attempt to
4817 * perform request smuggling (Section 9.5) or response splitting
4818 * (Section 9.4) and ought to be handled as an error. A sender MUST
4819 * remove the received Content-Length field prior to forwarding such
4820 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004821 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004822 * 4. If a message is received without Transfer-Encoding and with
4823 * either multiple Content-Length header fields having differing
4824 * field-values or a single Content-Length header field having an
4825 * invalid value, then the message framing is invalid and the
4826 * recipient MUST treat it as an unrecoverable error. If this is a
4827 * request message, the server MUST respond with a 400 (Bad Request)
4828 * status code and then close the connection. If this is a response
4829 * message received by a proxy, the proxy MUST close the connection
4830 * to the server, discard the received response, and send a 502 (Bad
4831 * Gateway) response to the client. If this is a response message
4832 * received by a user agent, the user agent MUST close the
4833 * connection to the server and discard the received response.
4834 *
4835 * 5. If a valid Content-Length header field is present without
4836 * Transfer-Encoding, its decimal value defines the expected message
4837 * body length in octets. If the sender closes the connection or
4838 * the recipient times out before the indicated number of octets are
4839 * received, the recipient MUST consider the message to be
4840 * incomplete and close the connection.
4841 *
4842 * 6. If this is a request message and none of the above are true, then
4843 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004844 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004845 * 7. Otherwise, this is a response message without a declared message
4846 * body length, so the message body length is determined by the
4847 * number of octets received prior to the server closing the
4848 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004849 */
4850
4851 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004852 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004853 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004854 * FIXME: should we parse anyway and return an error on chunked encoding ?
4855 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004856 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4857 txn->status == 101)) {
4858 /* Either we've established an explicit tunnel, or we're
4859 * switching the protocol. In both cases, we're very unlikely
4860 * to understand the next protocols. We have to switch to tunnel
4861 * mode, so that we transfer the request and responses then let
4862 * this protocol pass unmodified. When we later implement specific
4863 * parsers for such protocols, we'll want to check the Upgrade
4864 * header which contains information about that protocol for
4865 * responses with status 101 (eg: see RFC2817 about TLS).
4866 */
4867 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4868 msg->flags |= HTTP_MSGF_XFER_LEN;
4869 goto end;
4870 }
4871
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004872 if (txn->meth == HTTP_METH_HEAD ||
4873 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004874 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004875 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004876 goto skip_content_length;
4877 }
4878
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004879 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004880 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004881 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004882 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004883 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4884 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004885 /* bad transfer-encoding (chunked followed by something else) */
4886 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004887 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004888 break;
4889 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004890 }
4891
Willy Tarreau1c913912015-04-30 10:57:51 +02004892 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004893 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02004894 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004895 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02004896 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4897 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02004898 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004899 signed long long cl;
4900
Willy Tarreauad14f752011-09-02 20:33:27 +02004901 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004902 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004903 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004904 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004905
Willy Tarreauad14f752011-09-02 20:33:27 +02004906 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004907 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004908 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004909 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004910
Willy Tarreauad14f752011-09-02 20:33:27 +02004911 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004912 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004913 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004914 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004915
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004916 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004917 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004918 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004919 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004920
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004921 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004922 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004923 }
4924
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004925 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01004926 /* Now we have to check if we need to modify the Connection header.
4927 * This is more difficult on the response than it is on the request,
4928 * because we can have two different HTTP versions and we don't know
4929 * how the client will interprete a response. For instance, let's say
4930 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4931 * HTTP/1.1 response without any header. Maybe it will bound itself to
4932 * HTTP/1.0 because it only knows about it, and will consider the lack
4933 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4934 * the lack of header as a keep-alive. Thus we will use two flags
4935 * indicating how a request MAY be understood by the client. In case
4936 * of multiple possibilities, we'll fix the header to be explicit. If
4937 * ambiguous cases such as both close and keepalive are seen, then we
4938 * will fall back to explicit close. Note that we won't take risks with
4939 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004940 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004941 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004942 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02004943 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01004944 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004945
Willy Tarreau60466522010-01-18 19:08:45 +01004946 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02004947 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01004948 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004949
Willy Tarreau60466522010-01-18 19:08:45 +01004950 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02004951 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01004952 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004953 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004954 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004955 }
Willy Tarreau60466522010-01-18 19:08:45 +01004956 else { /* SCL / KAL */
4957 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004958 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004959 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004960 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004961
Willy Tarreau60466522010-01-18 19:08:45 +01004962 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004963 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004964
Willy Tarreau60466522010-01-18 19:08:45 +01004965 /* Some keep-alive responses are converted to Server-close if
4966 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004967 */
Willy Tarreau60466522010-01-18 19:08:45 +01004968 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4969 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004970 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004971 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004972 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004973 }
4974
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004975 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02004976 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02004977 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02004978
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004979 /* end of job, return OK */
4980 rep->analysers &= ~an_bit;
4981 rep->analyse_exp = TICK_ETERNITY;
4982 channel_auto_close(rep);
4983 return 1;
4984
4985 abort_keep_alive:
4986 /* A keep-alive request to the server failed on a network error.
4987 * The client is required to retry. We need to close without returning
4988 * any other information so that the client retries.
4989 */
4990 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004991 rep->analysers &= AN_RES_FLT_END;
4992 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004993 channel_auto_close(rep);
4994 s->logs.logwait = 0;
4995 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004996 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01004997 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004998 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004999 return 0;
5000}
5001
5002/* This function performs all the processing enabled for the current response.
5003 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005004 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005005 * other functions. It works like process_request (see indications above).
5006 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005007int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005008{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005009 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005010 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005011 struct http_msg *msg = &txn->rsp;
5012 struct proxy *cur_proxy;
5013 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005014 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005015
Christopher Faulet45073512018-07-20 10:16:29 +02005016 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 +02005017 now_ms, __FUNCTION__,
5018 s,
5019 rep,
5020 rep->rex, rep->wex,
5021 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005022 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005023 rep->analysers);
5024
5025 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5026 return 0;
5027
Willy Tarreau70730dd2014-04-24 18:06:27 +02005028 /* The stats applet needs to adjust the Connection header but we don't
5029 * apply any filter there.
5030 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005031 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5032 rep->analysers &= ~an_bit;
5033 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005034 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005035 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005036
Willy Tarreau58975672014-04-24 21:13:57 +02005037 /*
5038 * We will have to evaluate the filters.
5039 * As opposed to version 1.2, now they will be evaluated in the
5040 * filters order and not in the header order. This means that
5041 * each filter has to be validated among all headers.
5042 *
5043 * Filters are tried with ->be first, then with ->fe if it is
5044 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005045 *
5046 * Maybe we are in resume condiion. In this case I choose the
5047 * "struct proxy" which contains the rule list matching the resume
5048 * pointer. If none of theses "struct proxy" match, I initialise
5049 * the process with the first one.
5050 *
5051 * In fact, I check only correspondance betwwen the current list
5052 * pointer and the ->fe rule list. If it doesn't match, I initialize
5053 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005054 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005055 if (s->current_rule_list == &sess->fe->http_res_rules)
5056 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005057 else
5058 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005059 while (1) {
5060 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005061
Willy Tarreau58975672014-04-24 21:13:57 +02005062 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005063 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005064 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005065
Willy Tarreau51d861a2015-05-22 17:30:48 +02005066 if (ret == HTTP_RULE_RES_BADREQ)
5067 goto return_srv_prx_502;
5068
5069 if (ret == HTTP_RULE_RES_DONE) {
5070 rep->analysers &= ~an_bit;
5071 rep->analyse_exp = TICK_ETERNITY;
5072 return 1;
5073 }
5074 }
5075
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005076 /* we need to be called again. */
5077 if (ret == HTTP_RULE_RES_YIELD) {
5078 channel_dont_close(rep);
5079 return 0;
5080 }
5081
Willy Tarreau58975672014-04-24 21:13:57 +02005082 /* try headers filters */
5083 if (rule_set->rsp_exp != NULL) {
5084 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5085 return_bad_resp:
5086 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005087 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005088 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005089 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005090 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005091 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005092 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005093 txn->status = 502;
5094 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005095 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005096 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005097 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005098 if (!(s->flags & SF_ERR_MASK))
5099 s->flags |= SF_ERR_PRXCOND;
5100 if (!(s->flags & SF_FINST_MASK))
5101 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005102 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005103 }
Willy Tarreau58975672014-04-24 21:13:57 +02005104 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005105
Willy Tarreau58975672014-04-24 21:13:57 +02005106 /* has the response been denied ? */
5107 if (txn->flags & TX_SVDENY) {
5108 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005109 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005110
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005111 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5112 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005113 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005114 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005115
Willy Tarreau58975672014-04-24 21:13:57 +02005116 goto return_srv_prx_502;
5117 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005118
Willy Tarreau58975672014-04-24 21:13:57 +02005119 /* add response headers from the rule sets in the same order */
5120 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005121 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005122 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005123 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005124 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005125 ret = acl_pass(ret);
5126 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5127 ret = !ret;
5128 if (!ret)
5129 continue;
5130 }
Christopher Faulet10079f52018-10-03 15:17:28 +02005131 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005132 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005133 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005134
Willy Tarreau58975672014-04-24 21:13:57 +02005135 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005136 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005137 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005138 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005139 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005140
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005141 /* After this point, this anayzer can't return yield, so we can
5142 * remove the bit corresponding to this analyzer from the list.
5143 *
5144 * Note that the intermediate returns and goto found previously
5145 * reset the analyzers.
5146 */
5147 rep->analysers &= ~an_bit;
5148 rep->analyse_exp = TICK_ETERNITY;
5149
Willy Tarreau58975672014-04-24 21:13:57 +02005150 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005151 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005152 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005153
Willy Tarreau58975672014-04-24 21:13:57 +02005154 /*
5155 * Now check for a server cookie.
5156 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005157 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005158 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005159
Willy Tarreau58975672014-04-24 21:13:57 +02005160 /*
5161 * Check for cache-control or pragma headers if required.
5162 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005163 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005164 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005165
Willy Tarreau58975672014-04-24 21:13:57 +02005166 /*
5167 * Add server cookie in the response if needed
5168 */
5169 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5170 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005171 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005172 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5173 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5174 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5175 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5176 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005177 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005178 /* the server is known, it's not the one the client requested, or the
5179 * cookie's last seen date needs to be refreshed. We have to
5180 * insert a set-cookie here, except if we want to insert only on POST
5181 * requests and this one isn't. Note that servers which don't have cookies
5182 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005183 */
Willy Tarreau58975672014-04-24 21:13:57 +02005184 if (!objt_server(s->target)->cookie) {
5185 chunk_printf(&trash,
5186 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5187 s->be->cookie_name);
5188 }
5189 else {
5190 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005191
Willy Tarreau58975672014-04-24 21:13:57 +02005192 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5193 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005194 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5195 s30tob64((date.tv_sec+3) >> 2,
5196 trash.area + trash.data);
5197 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005198
Willy Tarreau58975672014-04-24 21:13:57 +02005199 if (s->be->cookie_maxlife) {
5200 /* emit first_date, which is either the original one or
5201 * the current date.
5202 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005203 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005204 s30tob64(txn->cookie_first_date ?
5205 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005206 (date.tv_sec+3) >> 2,
5207 trash.area + trash.data);
5208 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005209 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005210 }
Willy Tarreau58975672014-04-24 21:13:57 +02005211 chunk_appendf(&trash, "; path=/");
5212 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005213
Willy Tarreau58975672014-04-24 21:13:57 +02005214 if (s->be->cookie_domain)
5215 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005216
Willy Tarreau58975672014-04-24 21:13:57 +02005217 if (s->be->ck_opts & PR_CK_HTTPONLY)
5218 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005219
Willy Tarreau58975672014-04-24 21:13:57 +02005220 if (s->be->ck_opts & PR_CK_SECURE)
5221 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005222
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005223 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005224 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005225
Willy Tarreau58975672014-04-24 21:13:57 +02005226 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005227 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005228 /* the server did not change, only the date was updated */
5229 txn->flags |= TX_SCK_UPDATED;
5230 else
5231 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005232
Willy Tarreau58975672014-04-24 21:13:57 +02005233 /* Here, we will tell an eventual cache on the client side that we don't
5234 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5235 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5236 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005237 */
Willy Tarreau58975672014-04-24 21:13:57 +02005238 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005239
Willy Tarreau58975672014-04-24 21:13:57 +02005240 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005241
Willy Tarreau58975672014-04-24 21:13:57 +02005242 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5243 "Cache-control: private", 22) < 0))
5244 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005245 }
Willy Tarreau58975672014-04-24 21:13:57 +02005246 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005247
Willy Tarreau58975672014-04-24 21:13:57 +02005248 /*
5249 * Check if result will be cacheable with a cookie.
5250 * We'll block the response if security checks have caught
5251 * nasty things such as a cacheable cookie.
5252 */
5253 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5254 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5255 (s->be->options & PR_O_CHK_CACHE)) {
5256 /* we're in presence of a cacheable response containing
5257 * a set-cookie header. We'll block it as requested by
5258 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005259 */
Willy Tarreau58975672014-04-24 21:13:57 +02005260 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005261 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005262
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005263 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5264 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005265 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005266 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005267
Christopher Faulet767a84b2017-11-24 16:50:31 +01005268 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5269 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005270 send_log(s->be, LOG_ALERT,
5271 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5272 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5273 goto return_srv_prx_502;
5274 }
Willy Tarreau03945942009-12-22 16:50:27 +01005275
Willy Tarreau70730dd2014-04-24 18:06:27 +02005276 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005277 /*
5278 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5279 * If an "Upgrade" token is found, the header is left untouched in order
5280 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005281 * if anything but "Upgrade" is present in the Connection header. We don't
5282 * want to touch any 101 response either since it's switching to another
5283 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005284 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005285 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005286 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005287 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005288
Willy Tarreau58975672014-04-24 21:13:57 +02005289 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5290 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5291 /* we want a keep-alive response here. Keep-alive header
5292 * required if either side is not 1.1.
5293 */
5294 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5295 want_flags |= TX_CON_KAL_SET;
5296 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005297 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005298 /* we want a close response here. Close header required if
5299 * the server is 1.1, regardless of the client.
5300 */
5301 if (msg->flags & HTTP_MSGF_VER_11)
5302 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005303 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005304
Willy Tarreau58975672014-04-24 21:13:57 +02005305 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5306 http_change_connection_header(txn, msg, want_flags);
5307 }
5308
5309 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005310 /* Always enter in the body analyzer */
5311 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5312 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005313
Willy Tarreau58975672014-04-24 21:13:57 +02005314 /* if the user wants to log as soon as possible, without counting
5315 * bytes from the server, then this is the right moment. We have
5316 * to temporarily assign bytes_out to log what we currently have.
5317 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005318 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005319 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5320 s->logs.bytes_out = txn->rsp.eoh;
5321 s->do_log(s);
5322 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005323 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005324 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005325}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005326
Willy Tarreaud98cf932009-12-27 22:54:55 +01005327/* This function is an analyser which forwards response body (including chunk
5328 * sizes if any). It is called as soon as we must forward, even if we forward
5329 * zero byte. The only situation where it must not be called is when we're in
5330 * tunnel mode and we want to forward till the close. It's used both to forward
5331 * remaining data and to resync after end of body. It expects the msg_state to
5332 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005333 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005334 *
5335 * It is capable of compressing response data both in content-length mode and
5336 * in chunked mode. The state machines follows different flows depending on
5337 * whether content-length and chunked modes are used, since there are no
5338 * trailers in content-length :
5339 *
5340 * chk-mode cl-mode
5341 * ,----- BODY -----.
5342 * / \
5343 * V size > 0 V chk-mode
5344 * .--> SIZE -------------> DATA -------------> CRLF
5345 * | | size == 0 | last byte |
5346 * | v final crlf v inspected |
5347 * | TRAILERS -----------> DONE |
5348 * | |
5349 * `----------------------------------------------'
5350 *
5351 * Compression only happens in the DATA state, and must be flushed in final
5352 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5353 * is performed at once on final states for all bytes parsed, or when leaving
5354 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005355 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005356int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005357{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005358 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005359 struct http_txn *txn = s->txn;
5360 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005361 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005362
Christopher Faulet45073512018-07-20 10:16:29 +02005363 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 +02005364 now_ms, __FUNCTION__,
5365 s,
5366 res,
5367 res->rex, res->wex,
5368 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005369 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005370 res->analysers);
5371
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005372 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5373 return 0;
5374
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005375 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005376 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005377 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005378 /* Output closed while we were sending data. We must abort and
5379 * wake the other side up.
5380 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005381 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005382 msg->msg_state = HTTP_MSG_ERROR;
5383 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005384 return 1;
5385 }
5386
Willy Tarreau4fe41902010-06-07 22:27:41 +02005387 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005388 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005389
Christopher Fauletd7c91962015-04-30 11:48:27 +02005390 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005391 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5392 ? HTTP_MSG_CHUNK_SIZE
5393 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005394 }
5395
Willy Tarreauefdf0942014-04-24 20:08:57 +02005396 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005397 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005398 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005399 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005400 }
5401
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005402 if (msg->msg_state < HTTP_MSG_DONE) {
5403 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5404 ? http_msg_forward_chunked_body(s, msg)
5405 : http_msg_forward_body(s, msg));
5406 if (!ret)
5407 goto missing_data_or_waiting;
5408 if (ret < 0)
5409 goto return_bad_res;
5410 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005411
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005412 /* other states, DONE...TUNNEL */
5413 /* for keep-alive we don't want to forward closes on DONE */
5414 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5415 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5416 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005417
Christopher Faulet894da4c2017-07-18 11:29:07 +02005418 http_resync_states(s);
5419 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005420 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5421 if (res->flags & CF_SHUTW) {
5422 /* response errors are most likely due to the
5423 * client aborting the transfer. */
5424 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005425 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005426 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005427 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005428 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005429 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005430 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005431 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005432 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005433
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005434 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005435 if (res->flags & CF_SHUTW)
5436 goto aborted_xfer;
5437
5438 /* stop waiting for data if the input is closed before the end. If the
5439 * client side was already closed, it means that the client has aborted,
5440 * so we don't want to count this as a server abort. Otherwise it's a
5441 * server abort.
5442 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005443 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005444 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005445 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005446 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005447 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005448 if (!(s->flags & SF_ERR_MASK))
5449 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005450 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005451 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005452 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005453 goto return_bad_res_stats_ok;
5454 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005455 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005456
Willy Tarreau40dba092010-03-04 18:14:51 +01005457 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005458 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005459 goto return_bad_res;
5460
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005461 /* When TE: chunked is used, we need to get there again to parse
5462 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005463 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5464 * or if there are filters registered on the stream, we don't want to
5465 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005466 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005467 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005468 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005469 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5470 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005471 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005472
Willy Tarreau5c620922011-05-11 19:56:11 +02005473 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005474 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005475 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005476 * modes are already handled by the stream sock layer. We must not do
5477 * this in content-length mode because it could present the MSG_MORE
5478 * flag with the last block of forwarded data, which would cause an
5479 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005480 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005481 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005482 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005483
Willy Tarreau87b09662015-04-03 00:22:06 +02005484 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005485 return 0;
5486
Willy Tarreau40dba092010-03-04 18:14:51 +01005487 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005488 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005489 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005490 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005491
5492 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005493 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005494 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005495 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005496 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005497 res->analysers &= AN_RES_FLT_END;
5498 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 +01005499 if (objt_server(s->target))
5500 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005501
Willy Tarreaue7dff022015-04-03 01:14:29 +02005502 if (!(s->flags & SF_ERR_MASK))
5503 s->flags |= SF_ERR_PRXCOND;
5504 if (!(s->flags & SF_FINST_MASK))
5505 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005506 return 0;
5507
5508 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005509 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005510 txn->rsp.msg_state = HTTP_MSG_ERROR;
5511 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005512 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005513 res->analysers &= AN_RES_FLT_END;
5514 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 +01005515
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005516 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5517 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005518 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005519 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005520
Willy Tarreaue7dff022015-04-03 01:14:29 +02005521 if (!(s->flags & SF_ERR_MASK))
5522 s->flags |= SF_ERR_CLICL;
5523 if (!(s->flags & SF_FINST_MASK))
5524 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005525 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005526}
5527
5528
Christopher Faulet10079f52018-10-03 15:17:28 +02005529int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005530{
5531 struct channel *chn = msg->chn;
5532 int ret;
5533
5534 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5535
5536 if (msg->msg_state == HTTP_MSG_ENDING)
5537 goto ending;
5538
5539 /* Neither content-length, nor transfer-encoding was found, so we must
5540 * read the body until the server connection is closed. In that case, we
5541 * eat data as they come. Of course, this happens for response only. */
5542 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005543 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005544 msg->chunk_len += len;
5545 msg->body_len += len;
5546 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005547 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005548 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005549 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005550 msg->next += ret;
5551 msg->chunk_len -= ret;
5552 if (msg->chunk_len) {
5553 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005554 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005555 chn->flags |= CF_WAKE_WRITE;
5556 goto missing_data_or_waiting;
5557 }
5558
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005559 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5560 * always set for a request. */
5561 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5562 /* The server still sending data that should be filtered */
5563 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5564 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005565 msg->msg_state = HTTP_MSG_TUNNEL;
5566 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005567 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005568
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005569 msg->msg_state = HTTP_MSG_ENDING;
5570
5571 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005572 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005573 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005574 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5575 /* default_ret */ msg->next,
5576 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005577 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005578 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005579 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5580 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005581 if (msg->next)
5582 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005583
Christopher Fauletda02e172015-12-04 09:25:05 +01005584 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5585 /* default_ret */ 1,
5586 /* on_error */ goto error,
5587 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005588 if (msg->msg_state == HTTP_MSG_ENDING)
5589 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005590 return 1;
5591
5592 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005593 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005594 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5595 /* default_ret */ msg->next,
5596 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005597 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005598 msg->next -= ret;
5599 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5600 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005601 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005602 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005603 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005604 return 0;
5605 error:
5606 return -1;
5607}
5608
Christopher Faulet10079f52018-10-03 15:17:28 +02005609int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005610{
5611 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005612 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005613 int ret;
5614
5615 /* Here we have the guarantee to be in one of the following state:
5616 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5617 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5618
Christopher Fauletca874b82018-09-20 11:31:01 +02005619 if (msg->msg_state == HTTP_MSG_ENDING)
5620 goto ending;
5621
5622 /* Don't parse chunks if there is no input data */
5623 if (!ci_data(chn))
5624 goto waiting;
5625
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005626 switch_states:
5627 switch (msg->msg_state) {
5628 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005629 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005630 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005631 /* on_error */ goto error);
5632 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005633 msg->chunk_len -= ret;
5634 if (msg->chunk_len) {
5635 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005636 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005637 chn->flags |= CF_WAKE_WRITE;
5638 goto missing_data_or_waiting;
5639 }
5640
5641 /* nothing left to forward for this chunk*/
5642 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5643 /* fall through for HTTP_MSG_CHUNK_CRLF */
5644
5645 case HTTP_MSG_CHUNK_CRLF:
5646 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005647 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005648 if (ret == 0)
5649 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005650 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005651 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005652 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005653 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005654 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005655 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005656 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005657 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5658 /* fall through for HTTP_MSG_CHUNK_SIZE */
5659
5660 case HTTP_MSG_CHUNK_SIZE:
5661 /* read the chunk size and assign it to ->chunk_len,
5662 * then set ->next to point to the body and switch to
5663 * DATA or TRAILERS state.
5664 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005665 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005666 if (ret == 0)
5667 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005668 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005669 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005670 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005671 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005672 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005673 }
5674
5675 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005676 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005677 msg->chunk_len = chunk;
5678 msg->body_len += chunk;
5679
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005680 if (msg->chunk_len) {
5681 msg->msg_state = HTTP_MSG_DATA;
5682 goto switch_states;
5683 }
5684 msg->msg_state = HTTP_MSG_TRAILERS;
5685 /* fall through for HTTP_MSG_TRAILERS */
5686
5687 case HTTP_MSG_TRAILERS:
5688 ret = http_forward_trailers(msg);
5689 if (ret < 0)
5690 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005691 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5692 /* default_ret */ 1,
5693 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005694 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005695 if (!ret)
5696 goto missing_data_or_waiting;
5697 break;
5698
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005699 default:
5700 /* This should no happen in this function */
5701 goto error;
5702 }
5703
5704 msg->msg_state = HTTP_MSG_ENDING;
5705 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005706 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005707 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005708 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005709 /* default_ret */ msg->next,
5710 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005711 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005712 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005713 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5714 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005715 if (msg->next)
5716 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005717
Christopher Fauletda02e172015-12-04 09:25:05 +01005718 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005719 /* default_ret */ 1,
5720 /* on_error */ goto error,
5721 /* on_wait */ goto waiting);
5722 msg->msg_state = HTTP_MSG_DONE;
5723 return 1;
5724
5725 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005726 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005727 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005728 /* default_ret */ msg->next,
5729 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005730 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005731 msg->next -= ret;
5732 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5733 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005734 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005735 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005736 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005737 return 0;
5738
5739 chunk_parsing_error:
5740 if (msg->err_pos >= 0) {
5741 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005742 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005743 msg->msg_state, strm_fe(s));
5744 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005745 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005746 msg, msg->msg_state, s->be);
5747 }
5748 error:
5749 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005750}
5751
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005752
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005753/* Iterate the same filter through all request headers.
5754 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005755 * Since it can manage the switch to another backend, it updates the per-proxy
5756 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005757 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005758int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005759{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005760 char *cur_ptr, *cur_end, *cur_next;
5761 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005762 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005763 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005764 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005765
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005766 last_hdr = 0;
5767
Willy Tarreauf37954d2018-06-15 18:31:02 +02005768 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005769 old_idx = 0;
5770
5771 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005772 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005773 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005774 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005775 (exp->action == ACT_ALLOW ||
5776 exp->action == ACT_DENY ||
5777 exp->action == ACT_TARPIT))
5778 return 0;
5779
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005780 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005781 if (!cur_idx)
5782 break;
5783
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005784 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005785 cur_ptr = cur_next;
5786 cur_end = cur_ptr + cur_hdr->len;
5787 cur_next = cur_end + cur_hdr->cr + 1;
5788
5789 /* Now we have one header between cur_ptr and cur_end,
5790 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005791 */
5792
Willy Tarreau15a53a42015-01-21 13:39:42 +01005793 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005794 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005795 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005796 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005797 last_hdr = 1;
5798 break;
5799
5800 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005801 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005802 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005803 break;
5804
5805 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005806 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005807 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005808 break;
5809
5810 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005811 len = exp_replace(trash.area,
5812 trash.size, cur_ptr,
5813 exp->replace, pmatch);
5814 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005815 return -1;
5816
Willy Tarreau6e27be12018-08-22 04:46:47 +02005817 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5818
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005819 /* FIXME: if the user adds a newline in the replacement, the
5820 * index will not be recalculated for now, and the new line
5821 * will not be counted as a new header.
5822 */
5823
5824 cur_end += delta;
5825 cur_next += delta;
5826 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005827 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005828 break;
5829
5830 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005831 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005832 cur_next += delta;
5833
Willy Tarreaufa355d42009-11-29 18:12:29 +01005834 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005835 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5836 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005837 cur_hdr->len = 0;
5838 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005839 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005840 break;
5841
5842 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005843 }
5844
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005845 /* keep the link from this header to next one in case of later
5846 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005847 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005848 old_idx = cur_idx;
5849 }
5850 return 0;
5851}
5852
5853
5854/* Apply the filter to the request line.
5855 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5856 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005857 * Since it can manage the switch to another backend, it updates the per-proxy
5858 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005859 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005860int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005861{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005862 char *cur_ptr, *cur_end;
5863 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005864 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005865 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005866
Willy Tarreau3d300592007-03-18 18:34:41 +01005867 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005868 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005869 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005870 (exp->action == ACT_ALLOW ||
5871 exp->action == ACT_DENY ||
5872 exp->action == ACT_TARPIT))
5873 return 0;
5874 else if (exp->action == ACT_REMOVE)
5875 return 0;
5876
5877 done = 0;
5878
Willy Tarreauf37954d2018-06-15 18:31:02 +02005879 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005880 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005881
5882 /* Now we have the request line between cur_ptr and cur_end */
5883
Willy Tarreau15a53a42015-01-21 13:39:42 +01005884 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005885 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005886 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005887 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005888 done = 1;
5889 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005890
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005891 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005892 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005893 done = 1;
5894 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005895
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005896 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005897 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005898 done = 1;
5899 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005900
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005901 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005902 len = exp_replace(trash.area, trash.size,
5903 cur_ptr, exp->replace, pmatch);
5904 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005905 return -1;
5906
Willy Tarreau6e27be12018-08-22 04:46:47 +02005907 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5908
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005909 /* FIXME: if the user adds a newline in the replacement, the
5910 * index will not be recalculated for now, and the new line
5911 * will not be counted as a new header.
5912 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005913
Willy Tarreaufa355d42009-11-29 18:12:29 +01005914 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005915 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005916 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005917 HTTP_MSG_RQMETH,
5918 cur_ptr, cur_end + 1,
5919 NULL, NULL);
5920 if (unlikely(!cur_end))
5921 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005922
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005923 /* we have a full request and we know that we have either a CR
5924 * or an LF at <ptr>.
5925 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005926 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5927 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005928 /* there is no point trying this regex on headers */
5929 return 1;
5930 }
5931 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005932 return done;
5933}
Willy Tarreau97de6242006-12-27 17:18:38 +01005934
Willy Tarreau58f10d72006-12-04 02:26:12 +01005935
Willy Tarreau58f10d72006-12-04 02:26:12 +01005936
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005937/*
Willy Tarreau87b09662015-04-03 00:22:06 +02005938 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005939 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005940 * unparsable request. Since it can manage the switch to another backend, it
5941 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005942 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005943int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005944{
Willy Tarreau192252e2015-04-04 01:47:55 +02005945 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005946 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005947 struct hdr_exp *exp;
5948
5949 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005950 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005951
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005952 /*
5953 * The interleaving of transformations and verdicts
5954 * makes it difficult to decide to continue or stop
5955 * the evaluation.
5956 */
5957
Willy Tarreau6c123b12010-01-28 20:22:06 +01005958 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5959 break;
5960
Willy Tarreau3d300592007-03-18 18:34:41 +01005961 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005962 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005963 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005964 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005965
5966 /* if this filter had a condition, evaluate it now and skip to
5967 * next filter if the condition does not match.
5968 */
5969 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005970 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01005971 ret = acl_pass(ret);
5972 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5973 ret = !ret;
5974
5975 if (!ret)
5976 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005977 }
5978
5979 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005980 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005981 if (unlikely(ret < 0))
5982 return -1;
5983
5984 if (likely(ret == 0)) {
5985 /* The filter did not match the request, it can be
5986 * iterated through all headers.
5987 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01005988 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
5989 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005990 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005991 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005992 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005993}
5994
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005995
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005996/* Delete a value in a header between delimiters <from> and <next> in buffer
5997 * <buf>. The number of characters displaced is returned, and the pointer to
5998 * the first delimiter is updated if required. The function tries as much as
5999 * possible to respect the following principles :
6000 * - replace <from> delimiter by the <next> one unless <from> points to a
6001 * colon, in which case <next> is simply removed
6002 * - set exactly one space character after the new first delimiter, unless
6003 * there are not enough characters in the block being moved to do so.
6004 * - remove unneeded spaces before the previous delimiter and after the new
6005 * one.
6006 *
6007 * It is the caller's responsibility to ensure that :
6008 * - <from> points to a valid delimiter or the colon ;
6009 * - <next> points to a valid delimiter or the final CR/LF ;
6010 * - there are non-space chars before <from> ;
6011 * - there is a CR/LF at or after <next>.
6012 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006013int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006014{
6015 char *prev = *from;
6016
6017 if (*prev == ':') {
6018 /* We're removing the first value, preserve the colon and add a
6019 * space if possible.
6020 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006021 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006022 next++;
6023 prev++;
6024 if (prev < next)
6025 *prev++ = ' ';
6026
Willy Tarreau2235b262016-11-05 15:50:20 +01006027 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006028 next++;
6029 } else {
6030 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006031 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006032 prev--;
6033 *from = prev;
6034
6035 /* copy the delimiter and if possible a space if we're
6036 * not at the end of the line.
6037 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006038 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006039 *prev++ = *next++;
6040 if (prev + 1 < next)
6041 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006042 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006043 next++;
6044 }
6045 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006046 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006047}
6048
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006049/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006050 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006051 * desirable to call it only when needed. This code is quite complex because
6052 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6053 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006054 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006055void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006056{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006057 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006058 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006059 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006060 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006061 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6062 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006063
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006064 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006065 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006066 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006067
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006068 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006069 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006070 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006071
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006072 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006073 hdr_beg = hdr_next;
6074 hdr_end = hdr_beg + cur_hdr->len;
6075 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006076
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006077 /* We have one full header between hdr_beg and hdr_end, and the
6078 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006079 * "Cookie:" headers.
6080 */
6081
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006082 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006083 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006084 old_idx = cur_idx;
6085 continue;
6086 }
6087
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006088 del_from = NULL; /* nothing to be deleted */
6089 preserve_hdr = 0; /* assume we may kill the whole header */
6090
Willy Tarreau58f10d72006-12-04 02:26:12 +01006091 /* Now look for cookies. Conforming to RFC2109, we have to support
6092 * attributes whose name begin with a '$', and associate them with
6093 * the right cookie, if we want to delete this cookie.
6094 * So there are 3 cases for each cookie read :
6095 * 1) it's a special attribute, beginning with a '$' : ignore it.
6096 * 2) it's a server id cookie that we *MAY* want to delete : save
6097 * some pointers on it (last semi-colon, beginning of cookie...)
6098 * 3) it's an application cookie : we *MAY* have to delete a previous
6099 * "special" cookie.
6100 * At the end of loop, if a "special" cookie remains, we may have to
6101 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006102 * *MUST* delete it.
6103 *
6104 * Note: RFC2965 is unclear about the processing of spaces around
6105 * the equal sign in the ATTR=VALUE form. A careful inspection of
6106 * the RFC explicitly allows spaces before it, and not within the
6107 * tokens (attrs or values). An inspection of RFC2109 allows that
6108 * too but section 10.1.3 lets one think that spaces may be allowed
6109 * after the equal sign too, resulting in some (rare) buggy
6110 * implementations trying to do that. So let's do what servers do.
6111 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6112 * allowed quoted strings in values, with any possible character
6113 * after a backslash, including control chars and delimitors, which
6114 * causes parsing to become ambiguous. Browsers also allow spaces
6115 * within values even without quotes.
6116 *
6117 * We have to keep multiple pointers in order to support cookie
6118 * removal at the beginning, middle or end of header without
6119 * corrupting the header. All of these headers are valid :
6120 *
6121 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6122 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6123 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6124 * | | | | | | | | |
6125 * | | | | | | | | hdr_end <--+
6126 * | | | | | | | +--> next
6127 * | | | | | | +----> val_end
6128 * | | | | | +-----------> val_beg
6129 * | | | | +--------------> equal
6130 * | | | +----------------> att_end
6131 * | | +---------------------> att_beg
6132 * | +--------------------------> prev
6133 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006134 */
6135
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006136 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6137 /* Iterate through all cookies on this line */
6138
6139 /* find att_beg */
6140 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006141 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006142 att_beg++;
6143
6144 /* find att_end : this is the first character after the last non
6145 * space before the equal. It may be equal to hdr_end.
6146 */
6147 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006148
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006149 while (equal < hdr_end) {
6150 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006151 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006152 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006153 continue;
6154 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006155 }
6156
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006157 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6158 * is between <att_beg> and <equal>, both may be identical.
6159 */
6160
6161 /* look for end of cookie if there is an equal sign */
6162 if (equal < hdr_end && *equal == '=') {
6163 /* look for the beginning of the value */
6164 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006165 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006166 val_beg++;
6167
6168 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006169 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006170
6171 /* make val_end point to the first white space or delimitor after the value */
6172 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006173 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006174 val_end--;
6175 } else {
6176 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006177 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006178
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006179 /* We have nothing to do with attributes beginning with '$'. However,
6180 * they will automatically be removed if a header before them is removed,
6181 * since they're supposed to be linked together.
6182 */
6183 if (*att_beg == '$')
6184 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006185
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006186 /* Ignore cookies with no equal sign */
6187 if (equal == next) {
6188 /* This is not our cookie, so we must preserve it. But if we already
6189 * scheduled another cookie for removal, we cannot remove the
6190 * complete header, but we can remove the previous block itself.
6191 */
6192 preserve_hdr = 1;
6193 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006194 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006195 val_end += delta;
6196 next += delta;
6197 hdr_end += delta;
6198 hdr_next += delta;
6199 cur_hdr->len += delta;
6200 http_msg_move_end(&txn->req, delta);
6201 prev = del_from;
6202 del_from = NULL;
6203 }
6204 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006205 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006206
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006207 /* if there are spaces around the equal sign, we need to
6208 * strip them otherwise we'll get trouble for cookie captures,
6209 * or even for rewrites. Since this happens extremely rarely,
6210 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006211 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006212 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6213 int stripped_before = 0;
6214 int stripped_after = 0;
6215
6216 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006217 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006218 equal += stripped_before;
6219 val_beg += stripped_before;
6220 }
6221
6222 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006223 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006224 val_beg += stripped_after;
6225 stripped_before += stripped_after;
6226 }
6227
6228 val_end += stripped_before;
6229 next += stripped_before;
6230 hdr_end += stripped_before;
6231 hdr_next += stripped_before;
6232 cur_hdr->len += stripped_before;
6233 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006234 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006235 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006236
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006237 /* First, let's see if we want to capture this cookie. We check
6238 * that we don't already have a client side cookie, because we
6239 * can only capture one. Also as an optimisation, we ignore
6240 * cookies shorter than the declared name.
6241 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006242 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6243 (val_end - att_beg >= sess->fe->capture_namelen) &&
6244 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006245 int log_len = val_end - att_beg;
6246
Willy Tarreaubafbe012017-11-24 17:34:44 +01006247 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006248 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006249 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006250 if (log_len > sess->fe->capture_len)
6251 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006252 memcpy(txn->cli_cookie, att_beg, log_len);
6253 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006254 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006255 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006256
Willy Tarreaubca99692010-10-06 19:25:55 +02006257 /* Persistence cookies in passive, rewrite or insert mode have the
6258 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006259 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006260 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006261 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006262 * For cookies in prefix mode, the form is :
6263 *
6264 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006265 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006266 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6267 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6268 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006269 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006270
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006271 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6272 * have the server ID between val_beg and delim, and the original cookie between
6273 * delim+1 and val_end. Otherwise, delim==val_end :
6274 *
6275 * Cookie: NAME=SRV; # in all but prefix modes
6276 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6277 * | || || | |+-> next
6278 * | || || | +--> val_end
6279 * | || || +---------> delim
6280 * | || |+------------> val_beg
6281 * | || +-------------> att_end = equal
6282 * | |+-----------------> att_beg
6283 * | +------------------> prev
6284 * +-------------------------> hdr_beg
6285 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006286
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006287 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006288 for (delim = val_beg; delim < val_end; delim++)
6289 if (*delim == COOKIE_DELIM)
6290 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006291 } else {
6292 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006293 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006294 /* Now check if the cookie contains a date field, which would
6295 * appear after a vertical bar ('|') just after the server name
6296 * and before the delimiter.
6297 */
6298 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6299 if (vbar1) {
6300 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006301 * right is the last seen date. It is a base64 encoded
6302 * 30-bit value representing the UNIX date since the
6303 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006304 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006305 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006306 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006307 if (val_end - vbar1 >= 5) {
6308 val = b64tos30(vbar1);
6309 if (val > 0)
6310 txn->cookie_last_date = val << 2;
6311 }
6312 /* look for a second vertical bar */
6313 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6314 if (vbar1 && (val_end - vbar1 > 5)) {
6315 val = b64tos30(vbar1 + 1);
6316 if (val > 0)
6317 txn->cookie_first_date = val << 2;
6318 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006319 }
6320 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006321
Willy Tarreauf64d1412010-10-07 20:06:11 +02006322 /* if the cookie has an expiration date and the proxy wants to check
6323 * it, then we do that now. We first check if the cookie is too old,
6324 * then only if it has expired. We detect strict overflow because the
6325 * time resolution here is not great (4 seconds). Cookies with dates
6326 * in the future are ignored if their offset is beyond one day. This
6327 * allows an admin to fix timezone issues without expiring everyone
6328 * and at the same time avoids keeping unwanted side effects for too
6329 * long.
6330 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006331 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6332 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006333 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006334 txn->flags &= ~TX_CK_MASK;
6335 txn->flags |= TX_CK_OLD;
6336 delim = val_beg; // let's pretend we have not found the cookie
6337 txn->cookie_first_date = 0;
6338 txn->cookie_last_date = 0;
6339 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006340 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6341 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006342 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006343 txn->flags &= ~TX_CK_MASK;
6344 txn->flags |= TX_CK_EXPIRED;
6345 delim = val_beg; // let's pretend we have not found the cookie
6346 txn->cookie_first_date = 0;
6347 txn->cookie_last_date = 0;
6348 }
6349
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006350 /* Here, we'll look for the first running server which supports the cookie.
6351 * This allows to share a same cookie between several servers, for example
6352 * to dedicate backup servers to specific servers only.
6353 * However, to prevent clients from sticking to cookie-less backup server
6354 * when they have incidentely learned an empty cookie, we simply ignore
6355 * empty cookies and mark them as invalid.
6356 * The same behaviour is applied when persistence must be ignored.
6357 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006358 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006359 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006360
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006361 while (srv) {
6362 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6363 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006364 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006365 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006366 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006367 /* we found the server and we can use it */
6368 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006369 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006370 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006371 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006372 break;
6373 } else {
6374 /* we found a server, but it's down,
6375 * mark it as such and go on in case
6376 * another one is available.
6377 */
6378 txn->flags &= ~TX_CK_MASK;
6379 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006380 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006381 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006382 srv = srv->next;
6383 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006384
Willy Tarreauf64d1412010-10-07 20:06:11 +02006385 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006386 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006387 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006388 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006389 txn->flags |= TX_CK_UNUSED;
6390 else
6391 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006392 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006393
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006394 /* depending on the cookie mode, we may have to either :
6395 * - delete the complete cookie if we're in insert+indirect mode, so that
6396 * the server never sees it ;
6397 * - remove the server id from the cookie value, and tag the cookie as an
6398 * application cookie so that it does not get accidentely removed later,
6399 * if we're in cookie prefix mode
6400 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006401 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006402 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006403
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006404 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006405 val_end += delta;
6406 next += delta;
6407 hdr_end += delta;
6408 hdr_next += delta;
6409 cur_hdr->len += delta;
6410 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006411
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006412 del_from = NULL;
6413 preserve_hdr = 1; /* we want to keep this cookie */
6414 }
6415 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006416 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006417 del_from = prev;
6418 }
6419 } else {
6420 /* This is not our cookie, so we must preserve it. But if we already
6421 * scheduled another cookie for removal, we cannot remove the
6422 * complete header, but we can remove the previous block itself.
6423 */
6424 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006425
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006426 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006427 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006428 if (att_beg >= del_from)
6429 att_beg += delta;
6430 if (att_end >= del_from)
6431 att_end += delta;
6432 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006433 val_end += delta;
6434 next += delta;
6435 hdr_end += delta;
6436 hdr_next += delta;
6437 cur_hdr->len += delta;
6438 http_msg_move_end(&txn->req, delta);
6439 prev = del_from;
6440 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006441 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006442 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006443
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006444 /* continue with next cookie on this header line */
6445 att_beg = next;
6446 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006447
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006448 /* There are no more cookies on this line.
6449 * We may still have one (or several) marked for deletion at the
6450 * end of the line. We must do this now in two ways :
6451 * - if some cookies must be preserved, we only delete from the
6452 * mark to the end of line ;
6453 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006454 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006455 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006456 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006457 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006458 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006459 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006460 cur_hdr->len += delta;
6461 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006462 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006463
6464 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006465 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6466 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006467 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006468 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006469 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006470 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006471 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006472 }
6473
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006474 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006475 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006476 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006477}
6478
6479
Willy Tarreaua15645d2007-03-18 16:22:39 +01006480/* Iterate the same filter through all response headers contained in <rtr>.
6481 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6482 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006483int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006484{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006485 char *cur_ptr, *cur_end, *cur_next;
6486 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006487 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006488 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006489 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006490
6491 last_hdr = 0;
6492
Willy Tarreauf37954d2018-06-15 18:31:02 +02006493 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006494 old_idx = 0;
6495
6496 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006497 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006498 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006499 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006500 (exp->action == ACT_ALLOW ||
6501 exp->action == ACT_DENY))
6502 return 0;
6503
6504 cur_idx = txn->hdr_idx.v[old_idx].next;
6505 if (!cur_idx)
6506 break;
6507
6508 cur_hdr = &txn->hdr_idx.v[cur_idx];
6509 cur_ptr = cur_next;
6510 cur_end = cur_ptr + cur_hdr->len;
6511 cur_next = cur_end + cur_hdr->cr + 1;
6512
6513 /* Now we have one header between cur_ptr and cur_end,
6514 * and the next header starts at cur_next.
6515 */
6516
Willy Tarreau15a53a42015-01-21 13:39:42 +01006517 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006518 switch (exp->action) {
6519 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006520 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006521 last_hdr = 1;
6522 break;
6523
6524 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006525 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006526 last_hdr = 1;
6527 break;
6528
6529 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006530 len = exp_replace(trash.area,
6531 trash.size, cur_ptr,
6532 exp->replace, pmatch);
6533 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006534 return -1;
6535
Willy Tarreau6e27be12018-08-22 04:46:47 +02006536 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6537
Willy Tarreaua15645d2007-03-18 16:22:39 +01006538 /* FIXME: if the user adds a newline in the replacement, the
6539 * index will not be recalculated for now, and the new line
6540 * will not be counted as a new header.
6541 */
6542
6543 cur_end += delta;
6544 cur_next += delta;
6545 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006546 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006547 break;
6548
6549 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006550 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006551 cur_next += delta;
6552
Willy Tarreaufa355d42009-11-29 18:12:29 +01006553 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006554 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6555 txn->hdr_idx.used--;
6556 cur_hdr->len = 0;
6557 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006558 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006559 break;
6560
6561 }
6562 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006563
6564 /* keep the link from this header to next one in case of later
6565 * removal of next header.
6566 */
6567 old_idx = cur_idx;
6568 }
6569 return 0;
6570}
6571
6572
6573/* Apply the filter to the status line in the response buffer <rtr>.
6574 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6575 * or -1 if a replacement resulted in an invalid status line.
6576 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006577int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006578{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006579 char *cur_ptr, *cur_end;
6580 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006581 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006582 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006583
Willy Tarreau3d300592007-03-18 18:34:41 +01006584 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006585 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006586 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006587 (exp->action == ACT_ALLOW ||
6588 exp->action == ACT_DENY))
6589 return 0;
6590 else if (exp->action == ACT_REMOVE)
6591 return 0;
6592
6593 done = 0;
6594
Willy Tarreauf37954d2018-06-15 18:31:02 +02006595 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006596 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006597
6598 /* Now we have the status line between cur_ptr and cur_end */
6599
Willy Tarreau15a53a42015-01-21 13:39:42 +01006600 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006601 switch (exp->action) {
6602 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006603 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006604 done = 1;
6605 break;
6606
6607 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006608 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006609 done = 1;
6610 break;
6611
6612 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006613 len = exp_replace(trash.area, trash.size,
6614 cur_ptr, exp->replace, pmatch);
6615 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006616 return -1;
6617
Willy Tarreau6e27be12018-08-22 04:46:47 +02006618 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6619
Willy Tarreaua15645d2007-03-18 16:22:39 +01006620 /* FIXME: if the user adds a newline in the replacement, the
6621 * index will not be recalculated for now, and the new line
6622 * will not be counted as a new header.
6623 */
6624
Willy Tarreaufa355d42009-11-29 18:12:29 +01006625 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006626 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006627 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006628 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006629 cur_ptr, cur_end + 1,
6630 NULL, NULL);
6631 if (unlikely(!cur_end))
6632 return -1;
6633
6634 /* we have a full respnse and we know that we have either a CR
6635 * or an LF at <ptr>.
6636 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006637 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006638 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006639 /* there is no point trying this regex on headers */
6640 return 1;
6641 }
6642 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006643 return done;
6644}
6645
6646
6647
6648/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006649 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006650 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6651 * unparsable response.
6652 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006653int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006654{
Willy Tarreau192252e2015-04-04 01:47:55 +02006655 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006656 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006657 struct hdr_exp *exp;
6658
6659 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006660 int ret;
6661
6662 /*
6663 * The interleaving of transformations and verdicts
6664 * makes it difficult to decide to continue or stop
6665 * the evaluation.
6666 */
6667
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006668 if (txn->flags & TX_SVDENY)
6669 break;
6670
Willy Tarreau3d300592007-03-18 18:34:41 +01006671 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006672 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6673 exp->action == ACT_PASS)) {
6674 exp = exp->next;
6675 continue;
6676 }
6677
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006678 /* if this filter had a condition, evaluate it now and skip to
6679 * next filter if the condition does not match.
6680 */
6681 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006682 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006683 ret = acl_pass(ret);
6684 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6685 ret = !ret;
6686 if (!ret)
6687 continue;
6688 }
6689
Willy Tarreaua15645d2007-03-18 16:22:39 +01006690 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006691 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006692 if (unlikely(ret < 0))
6693 return -1;
6694
6695 if (likely(ret == 0)) {
6696 /* The filter did not match the response, it can be
6697 * iterated through all headers.
6698 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006699 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6700 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006701 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006702 }
6703 return 0;
6704}
6705
6706
Willy Tarreaua15645d2007-03-18 16:22:39 +01006707/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006708 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006709 * desirable to call it only when needed. This function is also used when we
6710 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006711 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006712void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006713{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006714 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006715 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006716 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006717 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006718 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006719 char *hdr_beg, *hdr_end, *hdr_next;
6720 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006721
Willy Tarreaua15645d2007-03-18 16:22:39 +01006722 /* Iterate through the headers.
6723 * we start with the start line.
6724 */
6725 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006726 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006727
6728 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6729 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006730 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006731
6732 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006733 hdr_beg = hdr_next;
6734 hdr_end = hdr_beg + cur_hdr->len;
6735 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006736
Willy Tarreau24581ba2010-08-31 22:39:35 +02006737 /* We have one full header between hdr_beg and hdr_end, and the
6738 * next header starts at hdr_next. We're only interested in
6739 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006740 */
6741
Willy Tarreau24581ba2010-08-31 22:39:35 +02006742 is_cookie2 = 0;
6743 prev = hdr_beg + 10;
6744 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006745 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006746 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6747 if (!val) {
6748 old_idx = cur_idx;
6749 continue;
6750 }
6751 is_cookie2 = 1;
6752 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006753 }
6754
Willy Tarreau24581ba2010-08-31 22:39:35 +02006755 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6756 * <prev> points to the colon.
6757 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006758 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006759
Willy Tarreau24581ba2010-08-31 22:39:35 +02006760 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6761 * check-cache is enabled) and we are not interested in checking
6762 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006763 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006764 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006765 return;
6766
Willy Tarreau24581ba2010-08-31 22:39:35 +02006767 /* OK so now we know we have to process this response cookie.
6768 * The format of the Set-Cookie header is slightly different
6769 * from the format of the Cookie header in that it does not
6770 * support the comma as a cookie delimiter (thus the header
6771 * cannot be folded) because the Expires attribute described in
6772 * the original Netscape's spec may contain an unquoted date
6773 * with a comma inside. We have to live with this because
6774 * many browsers don't support Max-Age and some browsers don't
6775 * support quoted strings. However the Set-Cookie2 header is
6776 * clean.
6777 *
6778 * We have to keep multiple pointers in order to support cookie
6779 * removal at the beginning, middle or end of header without
6780 * corrupting the header (in case of set-cookie2). A special
6781 * pointer, <scav> points to the beginning of the set-cookie-av
6782 * fields after the first semi-colon. The <next> pointer points
6783 * either to the end of line (set-cookie) or next unquoted comma
6784 * (set-cookie2). All of these headers are valid :
6785 *
6786 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6787 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6788 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6789 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6790 * | | | | | | | | | |
6791 * | | | | | | | | +-> next hdr_end <--+
6792 * | | | | | | | +------------> scav
6793 * | | | | | | +--------------> val_end
6794 * | | | | | +--------------------> val_beg
6795 * | | | | +----------------------> equal
6796 * | | | +------------------------> att_end
6797 * | | +----------------------------> att_beg
6798 * | +------------------------------> prev
6799 * +-----------------------------------------> hdr_beg
6800 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006801
Willy Tarreau24581ba2010-08-31 22:39:35 +02006802 for (; prev < hdr_end; prev = next) {
6803 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006804
Willy Tarreau24581ba2010-08-31 22:39:35 +02006805 /* find att_beg */
6806 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006807 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006808 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006809
Willy Tarreau24581ba2010-08-31 22:39:35 +02006810 /* find att_end : this is the first character after the last non
6811 * space before the equal. It may be equal to hdr_end.
6812 */
6813 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006814
Willy Tarreau24581ba2010-08-31 22:39:35 +02006815 while (equal < hdr_end) {
6816 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6817 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006818 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006819 continue;
6820 att_end = equal;
6821 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006822
Willy Tarreau24581ba2010-08-31 22:39:35 +02006823 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6824 * is between <att_beg> and <equal>, both may be identical.
6825 */
6826
6827 /* look for end of cookie if there is an equal sign */
6828 if (equal < hdr_end && *equal == '=') {
6829 /* look for the beginning of the value */
6830 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006831 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006832 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006833
Willy Tarreau24581ba2010-08-31 22:39:35 +02006834 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006835 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006836
6837 /* make val_end point to the first white space or delimitor after the value */
6838 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006839 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006840 val_end--;
6841 } else {
6842 /* <equal> points to next comma, semi-colon or EOL */
6843 val_beg = val_end = next = equal;
6844 }
6845
6846 if (next < hdr_end) {
6847 /* Set-Cookie2 supports multiple cookies, and <next> points to
6848 * a colon or semi-colon before the end. So skip all attr-value
6849 * pairs and look for the next comma. For Set-Cookie, since
6850 * commas are permitted in values, skip to the end.
6851 */
6852 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02006853 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006854 else
6855 next = hdr_end;
6856 }
6857
6858 /* Now everything is as on the diagram above */
6859
6860 /* Ignore cookies with no equal sign */
6861 if (equal == val_end)
6862 continue;
6863
6864 /* If there are spaces around the equal sign, we need to
6865 * strip them otherwise we'll get trouble for cookie captures,
6866 * or even for rewrites. Since this happens extremely rarely,
6867 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006868 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006869 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6870 int stripped_before = 0;
6871 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006872
Willy Tarreau24581ba2010-08-31 22:39:35 +02006873 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006874 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006875 equal += stripped_before;
6876 val_beg += stripped_before;
6877 }
6878
6879 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006880 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006881 val_beg += stripped_after;
6882 stripped_before += stripped_after;
6883 }
6884
6885 val_end += stripped_before;
6886 next += stripped_before;
6887 hdr_end += stripped_before;
6888 hdr_next += stripped_before;
6889 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006890 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006891 }
6892
6893 /* First, let's see if we want to capture this cookie. We check
6894 * that we don't already have a server side cookie, because we
6895 * can only capture one. Also as an optimisation, we ignore
6896 * cookies shorter than the declared name.
6897 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006898 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006899 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006900 (val_end - att_beg >= sess->fe->capture_namelen) &&
6901 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006902 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006903 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006904 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01006905 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006906 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006907 if (log_len > sess->fe->capture_len)
6908 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01006909 memcpy(txn->srv_cookie, att_beg, log_len);
6910 txn->srv_cookie[log_len] = 0;
6911 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006912 }
6913
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006914 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006915 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006916 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006917 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6918 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006919 /* assume passive cookie by default */
6920 txn->flags &= ~TX_SCK_MASK;
6921 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006922
6923 /* If the cookie is in insert mode on a known server, we'll delete
6924 * this occurrence because we'll insert another one later.
6925 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006926 * a direct access.
6927 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006928 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006929 /* The "preserve" flag was set, we don't want to touch the
6930 * server's cookie.
6931 */
6932 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006933 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006934 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006935 /* this cookie must be deleted */
6936 if (*prev == ':' && next == hdr_end) {
6937 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006938 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006939 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6940 txn->hdr_idx.used--;
6941 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006942 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006943 hdr_next += delta;
6944 http_msg_move_end(&txn->rsp, delta);
6945 /* note: while both invalid now, <next> and <hdr_end>
6946 * are still equal, so the for() will stop as expected.
6947 */
6948 } else {
6949 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006950 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006951 next = prev;
6952 hdr_end += delta;
6953 hdr_next += delta;
6954 cur_hdr->len += delta;
6955 http_msg_move_end(&txn->rsp, delta);
6956 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006957 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006958 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006959 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006960 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006961 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006962 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006963 * with this server since we know it.
6964 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006965 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006966 next += delta;
6967 hdr_end += delta;
6968 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006969 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006970 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006971
Willy Tarreauf1348312010-10-07 15:54:11 +02006972 txn->flags &= ~TX_SCK_MASK;
6973 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006974 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006975 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006976 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006977 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006978 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006979 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006980 next += delta;
6981 hdr_end += delta;
6982 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006983 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006984 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006985
Willy Tarreau827aee92011-03-10 16:55:02 +01006986 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006987 txn->flags &= ~TX_SCK_MASK;
6988 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006989 }
6990 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006991 /* that's done for this cookie, check the next one on the same
6992 * line when next != hdr_end (only if is_cookie2).
6993 */
6994 }
6995 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006996 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006997 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006998}
6999
7000
Willy Tarreaua15645d2007-03-18 16:22:39 +01007001/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007002 * Parses the Cache-Control and Pragma request header fields to determine if
7003 * the request may be served from the cache and/or if it is cacheable. Updates
7004 * s->txn->flags.
7005 */
7006void check_request_for_cacheability(struct stream *s, struct channel *chn)
7007{
7008 struct http_txn *txn = s->txn;
7009 char *p1, *p2;
7010 char *cur_ptr, *cur_end, *cur_next;
7011 int pragma_found;
7012 int cc_found;
7013 int cur_idx;
7014
7015 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7016 return; /* nothing more to do here */
7017
7018 cur_idx = 0;
7019 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007020 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007021
7022 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7023 struct hdr_idx_elem *cur_hdr;
7024 int val;
7025
7026 cur_hdr = &txn->hdr_idx.v[cur_idx];
7027 cur_ptr = cur_next;
7028 cur_end = cur_ptr + cur_hdr->len;
7029 cur_next = cur_end + cur_hdr->cr + 1;
7030
7031 /* We have one full header between cur_ptr and cur_end, and the
7032 * next header starts at cur_next.
7033 */
7034
7035 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7036 if (val) {
7037 if ((cur_end - (cur_ptr + val) >= 8) &&
7038 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7039 pragma_found = 1;
7040 continue;
7041 }
7042 }
7043
William Lallemand8a16fe02018-05-22 11:04:33 +02007044 /* Don't use the cache and don't try to store if we found the
7045 * Authorization header */
7046 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7047 if (val) {
7048 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7049 txn->flags |= TX_CACHE_IGNORE;
7050 continue;
7051 }
7052
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007053 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7054 if (!val)
7055 continue;
7056
7057 /* OK, right now we know we have a cache-control header at cur_ptr */
7058 cc_found = 1;
7059 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7060
7061 if (p1 >= cur_end) /* no more info */
7062 continue;
7063
7064 /* p1 is at the beginning of the value */
7065 p2 = p1;
7066 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7067 p2++;
7068
7069 /* we have a complete value between p1 and p2. We don't check the
7070 * values after max-age, max-stale nor min-fresh, we simply don't
7071 * use the cache when they're specified.
7072 */
7073 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7074 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7075 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7076 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7077 txn->flags |= TX_CACHE_IGNORE;
7078 continue;
7079 }
7080
7081 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7082 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7083 continue;
7084 }
7085 }
7086
7087 /* RFC7234#5.4:
7088 * When the Cache-Control header field is also present and
7089 * understood in a request, Pragma is ignored.
7090 * When the Cache-Control header field is not present in a
7091 * request, caches MUST consider the no-cache request
7092 * pragma-directive as having the same effect as if
7093 * "Cache-Control: no-cache" were present.
7094 */
7095 if (!cc_found && pragma_found)
7096 txn->flags |= TX_CACHE_IGNORE;
7097}
7098
7099/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007100 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007101 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007102void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007103{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007104 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007105 char *p1, *p2;
7106
7107 char *cur_ptr, *cur_end, *cur_next;
7108 int cur_idx;
7109
Willy Tarreau12b32f22017-12-21 16:08:09 +01007110 if (txn->status < 200) {
7111 /* do not try to cache interim responses! */
7112 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007113 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007114 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007115
7116 /* Iterate through the headers.
7117 * we start with the start line.
7118 */
7119 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007120 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007121
7122 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7123 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007124 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007125
7126 cur_hdr = &txn->hdr_idx.v[cur_idx];
7127 cur_ptr = cur_next;
7128 cur_end = cur_ptr + cur_hdr->len;
7129 cur_next = cur_end + cur_hdr->cr + 1;
7130
7131 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007132 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007133 */
7134
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007135 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7136 if (val) {
7137 if ((cur_end - (cur_ptr + val) >= 8) &&
7138 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7139 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7140 return;
7141 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007142 }
7143
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007144 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7145 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007146 continue;
7147
7148 /* OK, right now we know we have a cache-control header at cur_ptr */
7149
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007150 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007151
7152 if (p1 >= cur_end) /* no more info */
7153 continue;
7154
7155 /* p1 is at the beginning of the value */
7156 p2 = p1;
7157
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007158 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007159 p2++;
7160
7161 /* we have a complete value between p1 and p2 */
7162 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007163 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7164 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7165 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7166 continue;
7167 }
7168
Willy Tarreaua15645d2007-03-18 16:22:39 +01007169 /* we have something of the form no-cache="set-cookie" */
7170 if ((cur_end - p1 >= 21) &&
7171 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7172 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007173 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007174 continue;
7175 }
7176
7177 /* OK, so we know that either p2 points to the end of string or to a comma */
7178 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007179 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007180 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007181 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007182 return;
7183 }
7184
7185 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007186 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007187 continue;
7188 }
7189 }
7190}
7191
Willy Tarreau58f10d72006-12-04 02:26:12 +01007192
Willy Tarreaub2513902006-12-17 14:52:38 +01007193/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007194 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007195 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007196 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007197 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007198 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007199 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007200 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007201 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007202int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007203{
7204 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007205 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007206 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007207
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007208 if (!uri_auth)
7209 return 0;
7210
Cyril Bonté70be45d2010-10-12 00:14:35 +02007211 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007212 return 0;
7213
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007214 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007215 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007216 return 0;
7217
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007218 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007219 return 0;
7220
Willy Tarreaub2513902006-12-17 14:52:38 +01007221 return 1;
7222}
7223
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007224/* Append the description of what is present in error snapshot <es> into <out>.
7225 * The description must be small enough to always fit in a trash. The output
7226 * buffer may be the trash so the trash must not be used inside this function.
7227 */
7228void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7229{
7230 chunk_appendf(&trash,
7231 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7232 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7233 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7234 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7235 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7236 es->ctx.http.m_flags, es->ctx.http.m_clen,
7237 es->ctx.http.m_blen, es->ctx.http.b_flags);
7238}
7239
Willy Tarreau4076a152009-04-02 15:18:36 +02007240/*
7241 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007242 * By default it tries to report the error position as msg->err_pos. However if
7243 * this one is not set, it will then report msg->next, which is the last known
7244 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007245 * displays buffers as a contiguous area starting at buf->p. The direction is
7246 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007247 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007248void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007249 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007250 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007251{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007252 union error_snapshot_ctx ctx;
7253 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007254
Willy Tarreauef3ca732018-09-07 15:47:35 +02007255 /* http-specific part now */
7256 ctx.http.sid = s->uniq_id;
7257 ctx.http.state = state;
7258 ctx.http.b_flags = msg->chn->flags;
7259 ctx.http.s_flags = s->flags;
7260 ctx.http.t_flags = s->txn->flags;
7261 ctx.http.m_flags = msg->flags;
7262 ctx.http.m_clen = msg->chunk_len;
7263 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007264
Willy Tarreauef3ca732018-09-07 15:47:35 +02007265 ofs = msg->chn->total - ci_data(msg->chn);
7266 if (ofs < 0)
7267 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007268
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007269 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007270 other_end, s->target,
7271 strm_sess(s), &msg->chn->buf,
7272 ofs, co_data(msg->chn),
7273 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7274 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007275}
Willy Tarreaub2513902006-12-17 14:52:38 +01007276
Willy Tarreau294c4732011-12-16 21:35:50 +01007277/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7278 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7279 * performed over the whole headers. Otherwise it must contain a valid header
7280 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7281 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7282 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7283 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007284 * -1. The value fetch stops at commas, so this function is suited for use with
7285 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007286 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007287 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007288unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007289 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007290 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007291{
Willy Tarreau294c4732011-12-16 21:35:50 +01007292 struct hdr_ctx local_ctx;
7293 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007294 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007295 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007296 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007297
Willy Tarreau294c4732011-12-16 21:35:50 +01007298 if (!ctx) {
7299 local_ctx.idx = 0;
7300 ctx = &local_ctx;
7301 }
7302
Willy Tarreaubce70882009-09-07 11:51:47 +02007303 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007304 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007305 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007306 occ--;
7307 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007308 *vptr = ctx->line + ctx->val;
7309 *vlen = ctx->vlen;
7310 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007311 }
7312 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007313 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007314 }
7315
7316 /* negative occurrence, we scan all the list then walk back */
7317 if (-occ > MAX_HDR_HISTORY)
7318 return 0;
7319
Willy Tarreau294c4732011-12-16 21:35:50 +01007320 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007321 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007322 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7323 len_hist[hist_ptr] = ctx->vlen;
7324 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007325 hist_ptr = 0;
7326 found++;
7327 }
7328 if (-occ > found)
7329 return 0;
7330 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007331 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7332 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7333 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007334 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007335 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007336 if (hist_ptr >= MAX_HDR_HISTORY)
7337 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007338 *vptr = ptr_hist[hist_ptr];
7339 *vlen = len_hist[hist_ptr];
7340 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007341}
7342
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007343/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7344 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7345 * performed over the whole headers. Otherwise it must contain a valid header
7346 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7347 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7348 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7349 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7350 * -1. This function differs from http_get_hdr() in that it only returns full
7351 * line header values and does not stop at commas.
7352 * The return value is 0 if nothing was found, or non-zero otherwise.
7353 */
7354unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7355 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007356 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007357{
7358 struct hdr_ctx local_ctx;
7359 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007360 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007361 unsigned int hist_ptr;
7362 int found;
7363
7364 if (!ctx) {
7365 local_ctx.idx = 0;
7366 ctx = &local_ctx;
7367 }
7368
7369 if (occ >= 0) {
7370 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007371 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007372 occ--;
7373 if (occ <= 0) {
7374 *vptr = ctx->line + ctx->val;
7375 *vlen = ctx->vlen;
7376 return 1;
7377 }
7378 }
7379 return 0;
7380 }
7381
7382 /* negative occurrence, we scan all the list then walk back */
7383 if (-occ > MAX_HDR_HISTORY)
7384 return 0;
7385
7386 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007387 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007388 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7389 len_hist[hist_ptr] = ctx->vlen;
7390 if (++hist_ptr >= MAX_HDR_HISTORY)
7391 hist_ptr = 0;
7392 found++;
7393 }
7394 if (-occ > found)
7395 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007396
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007397 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007398 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7399 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7400 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007401 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007402 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007403 if (hist_ptr >= MAX_HDR_HISTORY)
7404 hist_ptr -= MAX_HDR_HISTORY;
7405 *vptr = ptr_hist[hist_ptr];
7406 *vlen = len_hist[hist_ptr];
7407 return 1;
7408}
7409
Willy Tarreaubaaee002006-06-26 02:48:02 +02007410/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007411 * Print a debug line with a header. Always stop at the first CR or LF char,
7412 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7413 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007414 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007415void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007416{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007417 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007418 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007419
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007420 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007421 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007422 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007423 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 +02007424
7425 for (max = 0; start + max < end; max++)
7426 if (start[max] == '\r' || start[max] == '\n')
7427 break;
7428
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007429 UBOUND(max, trash.size - trash.data - 3);
7430 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7431 trash.area[trash.data++] = '\n';
7432 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007433}
7434
Willy Tarreaueee5b512015-04-03 23:46:31 +02007435
7436/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7437 * The hdr_idx is allocated as well. In case of allocation failure, everything
7438 * allocated is freed and NULL is returned. Otherwise the new transaction is
7439 * assigned to the stream and returned.
7440 */
7441struct http_txn *http_alloc_txn(struct stream *s)
7442{
7443 struct http_txn *txn = s->txn;
7444
7445 if (txn)
7446 return txn;
7447
Willy Tarreaubafbe012017-11-24 17:34:44 +01007448 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007449 if (!txn)
7450 return txn;
7451
7452 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007453 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007454 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007455 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007456 return NULL;
7457 }
7458
7459 s->txn = txn;
7460 return txn;
7461}
7462
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007463void http_txn_reset_req(struct http_txn *txn)
7464{
7465 txn->req.flags = 0;
7466 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7467 txn->req.next = 0;
7468 txn->req.chunk_len = 0LL;
7469 txn->req.body_len = 0LL;
7470 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7471}
7472
7473void http_txn_reset_res(struct http_txn *txn)
7474{
7475 txn->rsp.flags = 0;
7476 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7477 txn->rsp.next = 0;
7478 txn->rsp.chunk_len = 0LL;
7479 txn->rsp.body_len = 0LL;
7480 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7481}
7482
Willy Tarreau0937bc42009-12-22 15:03:09 +01007483/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007484 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007485 * the required fields are properly allocated and that we only need to (re)init
7486 * them. This should be used before processing any new request.
7487 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007488void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007489{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007490 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007491 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007492
7493 txn->flags = 0;
7494 txn->status = -1;
7495
Willy Tarreauf64d1412010-10-07 20:06:11 +02007496 txn->cookie_first_date = 0;
7497 txn->cookie_last_date = 0;
7498
Willy Tarreaueee5b512015-04-03 23:46:31 +02007499 txn->srv_cookie = NULL;
7500 txn->cli_cookie = NULL;
7501 txn->uri = NULL;
7502
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007503 http_txn_reset_req(txn);
7504 http_txn_reset_res(txn);
7505
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007506 txn->req.chn = &s->req;
7507 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007508
7509 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007510
7511 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7512 if (fe->options2 & PR_O2_REQBUG_OK)
7513 txn->req.err_pos = -1; /* let buggy requests pass */
7514
Willy Tarreau0937bc42009-12-22 15:03:09 +01007515 if (txn->hdr_idx.v)
7516 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007517
7518 vars_init(&s->vars_txn, SCOPE_TXN);
7519 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007520}
7521
7522/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007523void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007524{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007525 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007526 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007527
7528 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007529 pool_free(pool_head_requri, txn->uri);
7530 pool_free(pool_head_capture, txn->cli_cookie);
7531 pool_free(pool_head_capture, txn->srv_cookie);
7532 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007533
William Lallemanda73203e2012-03-12 12:48:57 +01007534 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007535 txn->uri = NULL;
7536 txn->srv_cookie = NULL;
7537 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007538
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007539 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007540 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007541 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007542 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007543 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007544 }
7545
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007546 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007547 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007548 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007549 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007550 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007551 }
7552
Willy Tarreau6204cd92016-03-10 16:33:04 +01007553 vars_prune(&s->vars_txn, s->sess, s);
7554 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007555}
7556
7557/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007558void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007559{
7560 http_end_txn(s);
7561 http_init_txn(s);
7562
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007563 /* reinitialise the current rule list pointer to NULL. We are sure that
7564 * any rulelist match the NULL pointer.
7565 */
7566 s->current_rule_list = NULL;
7567
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007568 s->be = strm_fe(s);
7569 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007570 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007571 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007572 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007573 /* re-init store persistence */
7574 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007575 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007576
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007577 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007578
Willy Tarreau739cfba2010-01-25 23:11:14 +01007579 /* We must trim any excess data from the response buffer, because we
7580 * may have blocked an invalid response from a server that we don't
7581 * want to accidentely forward once we disable the analysers, nor do
7582 * we want those data to come along with next response. A typical
7583 * example of such data would be from a buggy server responding to
7584 * a HEAD with some data, or sending more than the advertised
7585 * content-length.
7586 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007587 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007588 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007589
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007590 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007591 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007592
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007593 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007594 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007595
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007596 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007597 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007598
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007599 s->req.rex = TICK_ETERNITY;
7600 s->req.wex = TICK_ETERNITY;
7601 s->req.analyse_exp = TICK_ETERNITY;
7602 s->res.rex = TICK_ETERNITY;
7603 s->res.wex = TICK_ETERNITY;
7604 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007605 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007606}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007607
Willy Tarreau79e57332018-10-02 16:01:16 +02007608/* This function executes one of the set-{method,path,query,uri} actions. It
7609 * takes the string from the variable 'replace' with length 'len', then modifies
7610 * the relevant part of the request line accordingly. Then it updates various
7611 * pointers to the next elements which were moved, and the total buffer length.
7612 * It finds the action to be performed in p[2], previously filled by function
7613 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7614 * error, though this can be revisited when this code is finally exploited.
7615 *
7616 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7617 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007618 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007619 * In query string case, the mark question '?' must be set at the start of the
7620 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007621 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007622int http_replace_req_line(int action, const char *replace, int len,
7623 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007624{
Willy Tarreau79e57332018-10-02 16:01:16 +02007625 struct http_txn *txn = s->txn;
7626 char *cur_ptr, *cur_end;
7627 int offset = 0;
7628 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007629
Willy Tarreau79e57332018-10-02 16:01:16 +02007630 switch (action) {
7631 case 0: // method
7632 cur_ptr = ci_head(&s->req);
7633 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007634
Willy Tarreau79e57332018-10-02 16:01:16 +02007635 /* adjust req line offsets and lengths */
7636 delta = len - offset - (cur_end - cur_ptr);
7637 txn->req.sl.rq.m_l += delta;
7638 txn->req.sl.rq.u += delta;
7639 txn->req.sl.rq.v += delta;
7640 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007641
Willy Tarreau79e57332018-10-02 16:01:16 +02007642 case 1: // path
7643 cur_ptr = http_txn_get_path(txn);
7644 if (!cur_ptr)
7645 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007646
Willy Tarreau79e57332018-10-02 16:01:16 +02007647 cur_end = cur_ptr;
7648 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7649 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007650
Willy Tarreau79e57332018-10-02 16:01:16 +02007651 /* adjust req line offsets and lengths */
7652 delta = len - offset - (cur_end - cur_ptr);
7653 txn->req.sl.rq.u_l += delta;
7654 txn->req.sl.rq.v += delta;
7655 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007656
Willy Tarreau79e57332018-10-02 16:01:16 +02007657 case 2: // query
7658 offset = 1;
7659 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7660 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7661 while (cur_ptr < cur_end && *cur_ptr != '?')
7662 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007663
Willy Tarreau79e57332018-10-02 16:01:16 +02007664 /* skip the question mark or indicate that we must insert it
7665 * (but only if the format string is not empty then).
7666 */
7667 if (cur_ptr < cur_end)
7668 cur_ptr++;
7669 else if (len > 1)
7670 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007671
Willy Tarreau79e57332018-10-02 16:01:16 +02007672 /* adjust req line offsets and lengths */
7673 delta = len - offset - (cur_end - cur_ptr);
7674 txn->req.sl.rq.u_l += delta;
7675 txn->req.sl.rq.v += delta;
7676 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007677
Willy Tarreau79e57332018-10-02 16:01:16 +02007678 case 3: // uri
7679 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7680 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007681
Willy Tarreau79e57332018-10-02 16:01:16 +02007682 /* adjust req line offsets and lengths */
7683 delta = len - offset - (cur_end - cur_ptr);
7684 txn->req.sl.rq.u_l += delta;
7685 txn->req.sl.rq.v += delta;
7686 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007687
Willy Tarreau79e57332018-10-02 16:01:16 +02007688 default:
7689 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007690 }
7691
Willy Tarreau79e57332018-10-02 16:01:16 +02007692 /* commit changes and adjust end of message */
7693 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7694 txn->req.sl.rq.l += delta;
7695 txn->hdr_idx.v[0].len += delta;
7696 http_msg_move_end(&txn->req, delta);
7697 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007698}
7699
Willy Tarreau79e57332018-10-02 16:01:16 +02007700/* This function replace the HTTP status code and the associated message. The
7701 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007702 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007703void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007704{
Willy Tarreau79e57332018-10-02 16:01:16 +02007705 struct http_txn *txn = s->txn;
7706 char *cur_ptr, *cur_end;
7707 int delta;
7708 char *res;
7709 int c_l;
7710 const char *msg = reason;
7711 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007712
Willy Tarreau79e57332018-10-02 16:01:16 +02007713 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007714
Willy Tarreau79e57332018-10-02 16:01:16 +02007715 res = ultoa_o(status, trash.area, trash.size);
7716 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007717
Willy Tarreau79e57332018-10-02 16:01:16 +02007718 trash.area[c_l] = ' ';
7719 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007720
Willy Tarreau79e57332018-10-02 16:01:16 +02007721 /* Do we have a custom reason format string? */
7722 if (msg == NULL)
7723 msg = http_get_reason(status);
7724 msg_len = strlen(msg);
7725 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7726 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007727
Willy Tarreau79e57332018-10-02 16:01:16 +02007728 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7729 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007730
Willy Tarreau79e57332018-10-02 16:01:16 +02007731 /* commit changes and adjust message */
7732 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7733 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007734
Willy Tarreau79e57332018-10-02 16:01:16 +02007735 /* adjust res line offsets and lengths */
7736 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7737 txn->rsp.sl.st.c_l = c_l;
7738 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007739
Willy Tarreau79e57332018-10-02 16:01:16 +02007740 delta = trash.data - (cur_end - cur_ptr);
7741 txn->rsp.sl.st.l += delta;
7742 txn->hdr_idx.v[0].len += delta;
7743 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007744}
7745
Willy Tarreau8797c062007-05-07 00:55:35 +02007746__attribute__((constructor))
7747static void __http_protocol_init(void)
7748{
Willy Tarreau8797c062007-05-07 00:55:35 +02007749}
7750
7751
Willy Tarreau58f10d72006-12-04 02:26:12 +01007752/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007753 * Local variables:
7754 * c-indent-level: 8
7755 * c-basic-offset: 8
7756 * End:
7757 */