blob: 7ffcede555afb57969c1e38e6d14afaf909fd0bb [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
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200797 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
798 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200799 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
800 tmp = TX_CON_WANT_TUN;
801
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200802 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200803 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
804 tmp = TX_CON_WANT_TUN;
805 }
806
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200807 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200808 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
809 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200810 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200811 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
812 tmp = TX_CON_WANT_CLO;
813 else
814 tmp = TX_CON_WANT_SCL;
815 }
816
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200817 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200818 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
819 tmp = TX_CON_WANT_CLO;
820
821 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
822 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
823
824 if (!(txn->flags & TX_HDR_CONN_PRS) &&
825 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
826 /* parse the Connection header and possibly clean it */
827 int to_del = 0;
828 if ((msg->flags & HTTP_MSGF_VER_11) ||
829 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200830 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200831 to_del |= 2; /* remove "keep-alive" */
832 if (!(msg->flags & HTTP_MSGF_VER_11))
833 to_del |= 1; /* remove "close" */
834 http_parse_connection_header(txn, msg, to_del);
835 }
836
837 /* check if client or config asks for explicit close in KAL/SCL */
838 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
839 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
840 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
841 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
842 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200843 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200844 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
845}
William Lallemand82fe75c2012-10-23 10:25:10 +0200846
Willy Tarreaud787e662009-07-07 10:14:51 +0200847/* This stream analyser waits for a complete HTTP request. It returns 1 if the
848 * processing can continue on next analysers, or zero if it either needs more
849 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100850 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200851 * when it has nothing left to do, and may remove any analyser when it wants to
852 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100853 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200854int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100855{
Willy Tarreau59234e92008-11-30 23:51:27 +0100856 /*
857 * We will parse the partial (or complete) lines.
858 * We will check the request syntax, and also join multi-line
859 * headers. An index of all the lines will be elaborated while
860 * parsing.
861 *
862 * For the parsing, we use a 28 states FSM.
863 *
864 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200865 * ci_head(req) = beginning of request
866 * ci_head(req) + msg->eoh = end of processed headers / start of current one
867 * ci_tail(req) = end of input data
868 * msg->eol = end of current header or line (LF or CRLF)
869 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200870 *
871 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200872 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200873 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
874 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100875 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100876
Willy Tarreau59234e92008-11-30 23:51:27 +0100877 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200878 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200879 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100880 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200881 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100882
Christopher Faulet45073512018-07-20 10:16:29 +0200883 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 +0100884 now_ms, __FUNCTION__,
885 s,
886 req,
887 req->rex, req->wex,
888 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200889 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100890 req->analysers);
891
Willy Tarreau52a0c602009-08-16 22:45:38 +0200892 /* we're speaking HTTP here, so let's speak HTTP to the client */
893 s->srv_error = http_return_srv_error;
894
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300895 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200896 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300897 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
898
Willy Tarreau83e3af02009-12-28 17:39:57 +0100899 /* There's a protected area at the end of the buffer for rewriting
900 * purposes. We don't want to start to parse the request if the
901 * protected area is affected, because we may have to move processed
902 * data later, which is much more complicated.
903 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200904 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200905 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100906 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200907 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100908 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100909 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200910 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200911 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100912 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100913 return 0;
914 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200915 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200916 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200917 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100918 }
919
Willy Tarreauf37954d2018-06-15 18:31:02 +0200920 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100921 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100922 }
923
Willy Tarreau59234e92008-11-30 23:51:27 +0100924 /* 1: we might have to print this header in debug mode */
925 if (unlikely((global.mode & MODE_DEBUG) &&
926 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200927 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100928 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100929
Willy Tarreauf37954d2018-06-15 18:31:02 +0200930 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200931 /* this is a bit complex : in case of error on the request line,
932 * we know that rq.l is still zero, so we display only the part
933 * up to the end of the line (truncated by debug_hdr).
934 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200935 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100936 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100937
Willy Tarreau59234e92008-11-30 23:51:27 +0100938 sol += hdr_idx_first_pos(&txn->hdr_idx);
939 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100940
Willy Tarreau59234e92008-11-30 23:51:27 +0100941 while (cur_idx) {
942 eol = sol + txn->hdr_idx.v[cur_idx].len;
943 debug_hdr("clihdr", s, sol, eol);
944 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
945 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100946 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100947 }
948
Willy Tarreau58f10d72006-12-04 02:26:12 +0100949
Willy Tarreau59234e92008-11-30 23:51:27 +0100950 /*
951 * Now we quickly check if we have found a full valid request.
952 * If not so, we check the FD and buffer states before leaving.
953 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100954 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100955 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200956 * on a keep-alive stream, if we encounter and error, close, t/o,
957 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100958 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200959 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200960 * Last, we may increase some tracked counters' http request errors on
961 * the cases that are deliberately the client's fault. For instance,
962 * a timeout or connection reset is not counted as an error. However
963 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100964 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100965
Willy Tarreau655dce92009-11-08 13:10:58 +0100966 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100967 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100968 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100969 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100970 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200971 stream_inc_http_req_ctr(s);
972 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200973 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100974 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100975 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100976
Willy Tarreau59234e92008-11-30 23:51:27 +0100977 /* 1: Since we are in header mode, if there's no space
978 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200979 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100980 * must terminate it now.
981 */
Willy Tarreau23752332018-06-15 14:54:53 +0200982 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100983 /* FIXME: check if URI is set and return Status
984 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100985 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200986 stream_inc_http_req_ctr(s);
987 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200988 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +0200989 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +0200990 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +0100991 goto return_bad_req;
992 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100993
Willy Tarreau59234e92008-11-30 23:51:27 +0100994 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200995 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200996 if (!(s->flags & SF_ERR_MASK))
997 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100998
Willy Tarreaufcffa692010-01-10 14:21:19 +0100999 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001000 goto failed_keep_alive;
1001
Willy Tarreau0f228a02015-05-01 15:37:53 +02001002 if (sess->fe->options & PR_O_IGNORE_PRB)
1003 goto failed_keep_alive;
1004
Willy Tarreau59234e92008-11-30 23:51:27 +01001005 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001006 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001007 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001008 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001009 }
1010
Willy Tarreaudc979f22012-12-04 10:39:01 +01001011 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001012 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001013 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001014 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001015 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001016 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001017 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001018 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001019 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001020 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001021
Willy Tarreaue7dff022015-04-03 01:14:29 +02001022 if (!(s->flags & SF_FINST_MASK))
1023 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001024 return 0;
1025 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001026
Willy Tarreau59234e92008-11-30 23:51:27 +01001027 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001028 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001029 if (!(s->flags & SF_ERR_MASK))
1030 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001031
Willy Tarreaufcffa692010-01-10 14:21:19 +01001032 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001033 goto failed_keep_alive;
1034
Willy Tarreau0f228a02015-05-01 15:37:53 +02001035 if (sess->fe->options & PR_O_IGNORE_PRB)
1036 goto failed_keep_alive;
1037
Willy Tarreau59234e92008-11-30 23:51:27 +01001038 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001039 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001040 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001041 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001042 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001043 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001044 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001045 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001046 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001047 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001048
Willy Tarreau87b09662015-04-03 00:22:06 +02001049 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001050 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001051 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001052 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001053 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001054
Willy Tarreaue7dff022015-04-03 01:14:29 +02001055 if (!(s->flags & SF_FINST_MASK))
1056 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001057 return 0;
1058 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001059
Willy Tarreau59234e92008-11-30 23:51:27 +01001060 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001061 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001062 if (!(s->flags & SF_ERR_MASK))
1063 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001064
Willy Tarreaufcffa692010-01-10 14:21:19 +01001065 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001066 goto failed_keep_alive;
1067
Willy Tarreau0f228a02015-05-01 15:37:53 +02001068 if (sess->fe->options & PR_O_IGNORE_PRB)
1069 goto failed_keep_alive;
1070
Willy Tarreau4076a152009-04-02 15:18:36 +02001071 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001072 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001073 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001074 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001075 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001076 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001077 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001078 stream_inc_http_err_ctr(s);
1079 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001080 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001081 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001082 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001083 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001084
Willy Tarreaue7dff022015-04-03 01:14:29 +02001085 if (!(s->flags & SF_FINST_MASK))
1086 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001087 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001088 }
1089
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001090 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001091 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001092 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001093#ifdef TCP_QUICKACK
Willy Tarreauf37954d2018-06-15 18:31:02 +02001094 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) &&
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001095 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001096 /* We need more data, we have to re-enable quick-ack in case we
1097 * previously disabled it, otherwise we might cause the client
1098 * to delay next data.
1099 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001100 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001101 }
1102#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001103
Willy Tarreaufcffa692010-01-10 14:21:19 +01001104 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1105 /* If the client starts to talk, let's fall back to
1106 * request timeout processing.
1107 */
1108 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001109 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001110 }
1111
Willy Tarreau59234e92008-11-30 23:51:27 +01001112 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001113 if (!tick_isset(req->analyse_exp)) {
1114 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1115 (txn->flags & TX_WAIT_NEXT_RQ) &&
1116 tick_isset(s->be->timeout.httpka))
1117 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1118 else
1119 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1120 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001121
Willy Tarreau59234e92008-11-30 23:51:27 +01001122 /* we're not ready yet */
1123 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001124
1125 failed_keep_alive:
1126 /* Here we process low-level errors for keep-alive requests. In
1127 * short, if the request is not the first one and it experiences
1128 * a timeout, read error or shutdown, we just silently close so
1129 * that the client can try again.
1130 */
1131 txn->status = 0;
1132 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001133 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001134 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001135 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001136 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001137 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001138 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001139 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001140
Willy Tarreaud787e662009-07-07 10:14:51 +02001141 /* OK now we have a complete HTTP request with indexed headers. Let's
1142 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001143 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001144 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001145 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001146 * byte after the last LF. msg->sov points to the first byte of data.
1147 * msg->eol cannot be trusted because it may have been left uninitialized
1148 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001149 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001150
Willy Tarreau87b09662015-04-03 00:22:06 +02001151 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001152 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001153
Willy Tarreaub16a5742010-01-10 14:46:16 +01001154 if (txn->flags & TX_WAIT_NEXT_RQ) {
1155 /* kill the pending keep-alive timeout */
1156 txn->flags &= ~TX_WAIT_NEXT_RQ;
1157 req->analyse_exp = TICK_ETERNITY;
1158 }
1159
1160
Willy Tarreaud787e662009-07-07 10:14:51 +02001161 /* Maybe we found in invalid header name while we were configured not
1162 * to block on that, so we have to capture it now.
1163 */
1164 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001165 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001166
Willy Tarreau59234e92008-11-30 23:51:27 +01001167 /*
1168 * 1: identify the method
1169 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001170 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001171
1172 /* we can make use of server redirect on GET and HEAD */
1173 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001174 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +01001175 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001176 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +01001177 /* PRI is reserved for the HTTP/2 preface */
1178 msg->err_pos = 0;
1179 goto return_bad_req;
1180 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001181
Willy Tarreau59234e92008-11-30 23:51:27 +01001182 /*
1183 * 2: check if the URI matches the monitor_uri.
1184 * We have to do this for every request which gets in, because
1185 * the monitor-uri is defined by the frontend.
1186 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001187 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1188 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001189 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001190 sess->fe->monitor_uri,
1191 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001192 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001193 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001194 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001195 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001196
Willy Tarreaue7dff022015-04-03 01:14:29 +02001197 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001198 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001199
Willy Tarreau59234e92008-11-30 23:51:27 +01001200 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001201 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001202 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001203
Willy Tarreau59234e92008-11-30 23:51:27 +01001204 ret = acl_pass(ret);
1205 if (cond->pol == ACL_COND_UNLESS)
1206 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001207
Willy Tarreau59234e92008-11-30 23:51:27 +01001208 if (ret) {
1209 /* we fail this request, let's return 503 service unavail */
1210 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001211 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001212 if (!(s->flags & SF_ERR_MASK))
1213 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001214 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001215 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001216 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001217
Willy Tarreau59234e92008-11-30 23:51:27 +01001218 /* nothing to fail, let's reply normaly */
1219 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001220 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001221 if (!(s->flags & SF_ERR_MASK))
1222 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001223 goto return_prx_cond;
1224 }
1225
1226 /*
1227 * 3: Maybe we have to copy the original REQURI for the logs ?
1228 * Note: we cannot log anymore if the request has been
1229 * classified as invalid.
1230 */
1231 if (unlikely(s->logs.logwait & LW_REQ)) {
1232 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001233 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001234 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001235
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001236 if (urilen >= global.tune.requri_len )
1237 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001238 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001239 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001240
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001241 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001242 s->do_log(s);
1243 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001244 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001245 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001246 }
Willy Tarreau06619262006-12-17 08:37:22 +01001247
Willy Tarreau91852eb2015-05-01 13:26:00 +02001248 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1249 * exactly one digit "." one digit. This check may be disabled using
1250 * option accept-invalid-http-request.
1251 */
1252 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1253 if (msg->sl.rq.v_l != 8) {
1254 msg->err_pos = msg->sl.rq.v;
1255 goto return_bad_req;
1256 }
1257
Willy Tarreauf37954d2018-06-15 18:31:02 +02001258 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
1259 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
1260 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
1261 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02001262 msg->err_pos = msg->sl.rq.v + 4;
1263 goto return_bad_req;
1264 }
1265 }
Willy Tarreau13317662015-05-01 13:47:08 +02001266 else {
1267 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1268 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1269 goto return_bad_req;
1270 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001271
Willy Tarreau5b154472009-12-21 20:11:07 +01001272 /* ... and check if the request is HTTP/1.1 or above */
1273 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001274 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
1275 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
1276 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001277 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001278
1279 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001280 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 +01001281
Willy Tarreau88d349d2010-01-25 12:15:43 +01001282 /* if the frontend has "option http-use-proxy-header", we'll check if
1283 * we have what looks like a proxied connection instead of a connection,
1284 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1285 * Note that this is *not* RFC-compliant, however browsers and proxies
1286 * happen to do that despite being non-standard :-(
1287 * We consider that a request not beginning with either '/' or '*' is
1288 * a proxied connection, which covers both "scheme://location" and
1289 * CONNECT ip:port.
1290 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001291 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02001292 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01001293 txn->flags |= TX_USE_PX_CONN;
1294
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001295 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001296 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001297
Willy Tarreau59234e92008-11-30 23:51:27 +01001298 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001299 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02001300 http_capture_headers(ci_head(req), &txn->hdr_idx,
1301 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001302
Willy Tarreau557f1992015-05-01 10:05:17 +02001303 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1304 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001305 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001306 * The length of a message body is determined by one of the following
1307 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001308 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001309 * 1. Any response to a HEAD request and any response with a 1xx
1310 * (Informational), 204 (No Content), or 304 (Not Modified) status
1311 * code is always terminated by the first empty line after the
1312 * header fields, regardless of the header fields present in the
1313 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001314 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001315 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1316 * the connection will become a tunnel immediately after the empty
1317 * line that concludes the header fields. A client MUST ignore any
1318 * Content-Length or Transfer-Encoding header fields received in
1319 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001320 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001321 * 3. If a Transfer-Encoding header field is present and the chunked
1322 * transfer coding (Section 4.1) is the final encoding, the message
1323 * body length is determined by reading and decoding the chunked
1324 * data until the transfer coding indicates the data is complete.
1325 *
1326 * If a Transfer-Encoding header field is present in a response and
1327 * the chunked transfer coding is not the final encoding, the
1328 * message body length is determined by reading the connection until
1329 * it is closed by the server. If a Transfer-Encoding header field
1330 * is present in a request and the chunked transfer coding is not
1331 * the final encoding, the message body length cannot be determined
1332 * reliably; the server MUST respond with the 400 (Bad Request)
1333 * status code and then close the connection.
1334 *
1335 * If a message is received with both a Transfer-Encoding and a
1336 * Content-Length header field, the Transfer-Encoding overrides the
1337 * Content-Length. Such a message might indicate an attempt to
1338 * perform request smuggling (Section 9.5) or response splitting
1339 * (Section 9.4) and ought to be handled as an error. A sender MUST
1340 * remove the received Content-Length field prior to forwarding such
1341 * a message downstream.
1342 *
1343 * 4. If a message is received without Transfer-Encoding and with
1344 * either multiple Content-Length header fields having differing
1345 * field-values or a single Content-Length header field having an
1346 * invalid value, then the message framing is invalid and the
1347 * recipient MUST treat it as an unrecoverable error. If this is a
1348 * request message, the server MUST respond with a 400 (Bad Request)
1349 * status code and then close the connection. If this is a response
1350 * message received by a proxy, the proxy MUST close the connection
1351 * to the server, discard the received response, and send a 502 (Bad
1352 * Gateway) response to the client. If this is a response message
1353 * received by a user agent, the user agent MUST close the
1354 * connection to the server and discard the received response.
1355 *
1356 * 5. If a valid Content-Length header field is present without
1357 * Transfer-Encoding, its decimal value defines the expected message
1358 * body length in octets. If the sender closes the connection or
1359 * the recipient times out before the indicated number of octets are
1360 * received, the recipient MUST consider the message to be
1361 * incomplete and close the connection.
1362 *
1363 * 6. If this is a request message and none of the above are true, then
1364 * the message body length is zero (no message body is present).
1365 *
1366 * 7. Otherwise, this is a response message without a declared message
1367 * body length, so the message body length is determined by the
1368 * number of octets received prior to the server closing the
1369 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001370 */
1371
Willy Tarreau32b47f42009-10-18 20:55:02 +02001372 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001373 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001374 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001375 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001376 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001377 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001378 /* chunked not last, return badreq */
1379 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001380 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001381 }
1382
Willy Tarreau1c913912015-04-30 10:57:51 +02001383 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001384 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001385 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001386 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001387 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1388 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001389 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001390 signed long long cl;
1391
Willy Tarreauad14f752011-09-02 20:33:27 +02001392 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001393 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001394 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001395 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001396
Willy Tarreauad14f752011-09-02 20:33:27 +02001397 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001398 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001399 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001400 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001401
Willy Tarreauad14f752011-09-02 20:33:27 +02001402 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001403 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001404 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001405 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001406
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001407 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001408 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001409 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001410 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001411
Christopher Fauletbe821b92017-03-30 11:21:53 +02001412 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001413 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001414 }
1415
Willy Tarreau34dfc602015-05-01 10:09:49 +02001416 /* even bodyless requests have a known length */
1417 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001418
Willy Tarreau179085c2014-04-28 16:48:56 +02001419 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1420 * only change if both the request and the config reference something else.
1421 * Option httpclose by itself sets tunnel mode where headers are mangled.
1422 * However, if another mode is set, it will affect it (eg: server-close/
1423 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1424 * if FE and BE have the same settings (common). The method consists in
1425 * checking if options changed between the two calls (implying that either
1426 * one is non-null, or one of them is non-null and we are there for the first
1427 * time.
1428 */
1429 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001430 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001431 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001432
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001433 /* we may have to wait for the request's body */
1434 if ((s->be->options & PR_O_WREQ_BODY) &&
1435 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1436 req->analysers |= AN_REQ_HTTP_BODY;
1437
Willy Tarreau83ece462017-12-21 15:13:09 +01001438 /*
1439 * RFC7234#4:
1440 * A cache MUST write through requests with methods
1441 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1442 * the origin server; i.e., a cache is not allowed
1443 * to generate a reply to such a request before
1444 * having forwarded the request and having received
1445 * a corresponding response.
1446 *
1447 * RFC7231#4.2.1:
1448 * Of the request methods defined by this
1449 * specification, the GET, HEAD, OPTIONS, and TRACE
1450 * methods are defined to be safe.
1451 */
1452 if (likely(txn->meth == HTTP_METH_GET ||
1453 txn->meth == HTTP_METH_HEAD ||
1454 txn->meth == HTTP_METH_OPTIONS ||
1455 txn->meth == HTTP_METH_TRACE))
1456 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1457
Willy Tarreaud787e662009-07-07 10:14:51 +02001458 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001459 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001460 req->analyse_exp = TICK_ETERNITY;
1461 return 1;
1462
1463 return_bad_req:
1464 /* We centralize bad requests processing here */
1465 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1466 /* we detected a parsing error. We want to archive this request
1467 * in the dedicated proxy area for later troubleshooting.
1468 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001469 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001470 }
1471
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001472 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001473 txn->req.msg_state = HTTP_MSG_ERROR;
1474 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001475 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001476
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001477 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001478 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001479 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001480
1481 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001482 if (!(s->flags & SF_ERR_MASK))
1483 s->flags |= SF_ERR_PRXCOND;
1484 if (!(s->flags & SF_FINST_MASK))
1485 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001486
Christopher Faulet0184ea72017-01-05 14:06:34 +01001487 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001488 req->analyse_exp = TICK_ETERNITY;
1489 return 0;
1490}
1491
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001492
Willy Tarreau347a35d2013-11-22 17:51:09 +01001493/* This function prepares an applet to handle the stats. It can deal with the
1494 * "100-continue" expectation, check that admin rules are met for POST requests,
1495 * and program a response message if something was unexpected. It cannot fail
1496 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001497 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001498 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001499 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001500 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001501int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001502{
1503 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001504 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001505 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001506 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001507 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001508 struct uri_auth *uri_auth = s->be->uri_auth;
1509 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001510 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001511
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001512 appctx = si_appctx(si);
1513 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1514 appctx->st1 = appctx->st2 = 0;
1515 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1516 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001517 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001518 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001519
Willy Tarreauf37954d2018-06-15 18:31:02 +02001520 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001521 lookup = uri + uri_auth->uri_len;
1522
1523 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1524 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001525 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001526 break;
1527 }
1528 }
1529
1530 if (uri_auth->refresh) {
1531 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1532 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001533 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001534 break;
1535 }
1536 }
1537 }
1538
1539 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1540 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001541 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001542 break;
1543 }
1544 }
1545
Willy Tarreau1e62df92016-01-11 18:57:53 +01001546 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1547 if (memcmp(h, ";typed", 6) == 0) {
1548 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1549 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1550 break;
1551 }
1552 }
1553
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001554 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1555 if (memcmp(h, ";st=", 4) == 0) {
1556 int i;
1557 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001558 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001559 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1560 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001561 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001562 break;
1563 }
1564 }
1565 break;
1566 }
1567 }
1568
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001569 appctx->ctx.stats.scope_str = 0;
1570 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001571 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1572 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1573 int itx = 0;
1574 const char *h2;
1575 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1576 const char *err;
1577
1578 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1579 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001580 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001581 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1582 itx++;
1583 h++;
1584 }
1585
1586 if (itx > STAT_SCOPE_TXT_MAXLEN)
1587 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001588 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001589
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001590 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001591 memcpy(scope_txt, h2, itx);
1592 scope_txt[itx] = '\0';
1593 err = invalid_char(scope_txt);
1594 if (err) {
1595 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001596 appctx->ctx.stats.scope_str = 0;
1597 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001598 }
1599 break;
1600 }
1601 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001602
1603 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001604 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001605 int ret = 1;
1606
1607 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001608 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 +01001609 ret = acl_pass(ret);
1610 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1611 ret = !ret;
1612 }
1613
1614 if (ret) {
1615 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001616 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001617 break;
1618 }
1619 }
1620
1621 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001622 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001623 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001624 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001625 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1626 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001627 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001628 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001629 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001630 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1631 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001632 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001633 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001634 else {
1635 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001636 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001637 }
1638
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001639 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001640 return 1;
1641}
1642
Lukas Tribus67db8df2013-06-23 17:37:13 +02001643/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
1644 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
1645 */
Vincent Bernat6e615892016-05-18 16:17:44 +02001646void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001647{
1648#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02001649 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02001650 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1651#endif
1652#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02001653 if (from->ss_family == AF_INET6) {
1654 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02001655 /* v4-mapped addresses need IP_TOS */
1656 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
1657 else
1658 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
1659 }
1660#endif
1661}
1662
Willy Tarreau87b09662015-04-03 00:22:06 +02001663int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001664 const char* name, unsigned int name_len,
1665 const char *str, struct my_regex *re,
1666 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001667{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001668 struct hdr_ctx ctx;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001669 char *buf = ci_head(msg->chn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02001670 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001671 int (*http_find_hdr_func)(const char *name, int len, char *sol,
1672 struct hdr_idx *idx, struct hdr_ctx *ctx);
Willy Tarreau83061a82018-07-13 11:56:34 +02001673 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001674
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001675 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06001676
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001677 /* Choose the header browsing function. */
1678 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001679 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001680 http_find_hdr_func = http_find_header2;
1681 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001682 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001683 http_find_hdr_func = http_find_full_header2;
1684 break;
1685 default: /* impossible */
1686 return -1;
1687 }
1688
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001689 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
1690 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Willy Tarreau6e27be12018-08-22 04:46:47 +02001691 int delta, len;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001692 char *val = ctx.line + ctx.val;
1693 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06001694
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001695 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
1696 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06001697
Willy Tarreau6e27be12018-08-22 04:46:47 +02001698 len = exp_replace(output->area, output->size, val, str, pmatch);
1699 if (len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06001700 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06001701
Willy Tarreau6e27be12018-08-22 04:46:47 +02001702 delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len);
Sasha Pachev218f0642014-06-16 12:05:59 -06001703
1704 hdr->len += delta;
1705 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001706
1707 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001708 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06001709 }
1710
1711 return 0;
1712}
1713
Willy Tarreau87b09662015-04-03 00:22:06 +02001714static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001715 const char* name, unsigned int name_len,
1716 struct list *fmt, struct my_regex *re,
1717 int action)
1718{
Willy Tarreau83061a82018-07-13 11:56:34 +02001719 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001720 int ret = -1;
1721
1722 replace = alloc_trash_chunk();
1723 if (!replace)
1724 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001725
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001726 replace->data = build_logline(s, replace->area, replace->size, fmt);
1727 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001728 goto leave;
1729
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001730 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1731 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001732
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001733 leave:
1734 free_trash_chunk(replace);
1735 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001736}
1737
Willy Tarreau87b09662015-04-03 00:22:06 +02001738/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001739 * transaction <txn>. Returns the verdict of the first rule that prevents
1740 * further processing of the request (auth, deny, ...), and defaults to
1741 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1742 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001743 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1744 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1745 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001746 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001747enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001748http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001749{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001750 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001751 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02001752 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001753 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001754 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001755 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02001756 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001757 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001758
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001759 /* If "the current_rule_list" match the executed rule list, we are in
1760 * resume condition. If a resume is needed it is always in the action
1761 * and never in the ACL or converters. In this case, we initialise the
1762 * current rule, and go to the action execution point.
1763 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001764 if (s->current_rule) {
1765 rule = s->current_rule;
1766 s->current_rule = NULL;
1767 if (s->current_rule_list == rules)
1768 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001769 }
1770 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001771
Willy Tarreauff011f22011-01-06 17:51:27 +01001772 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001773
Willy Tarreau96257ec2012-12-27 10:46:37 +01001774 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001775 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001776 int ret;
1777
Willy Tarreau192252e2015-04-04 01:47:55 +02001778 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001779 ret = acl_pass(ret);
1780
Willy Tarreauff011f22011-01-06 17:51:27 +01001781 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001782 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001783
1784 if (!ret) /* condition not matched */
1785 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001786 }
1787
Willy Tarreauacc98002015-09-27 23:34:39 +02001788 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001789resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001790 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001791 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02001792 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001793
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001794 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001795 if (deny_status)
1796 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02001797 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001798
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001799 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001800 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001801 if (deny_status)
1802 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02001803 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001804
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001805 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02001806 /* Auth might be performed on regular http-req rules as well as on stats */
1807 auth_realm = rule->arg.auth.realm;
1808 if (!auth_realm) {
1809 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1810 auth_realm = STATS_DEFAULT_REALM;
1811 else
1812 auth_realm = px->id;
1813 }
1814 /* send 401/407 depending on whether we use a proxy or not. We still
1815 * count one error, because normal browsing won't significantly
1816 * increase the counter but brute force attempts will.
1817 */
1818 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1819 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001820 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001821 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02001822 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001823
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001824 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02001825 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
1826 return HTTP_RULE_RES_BADREQ;
1827 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001828
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001829 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001830 s->task->nice = rule->arg.nice;
1831 break;
1832
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001833 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001834 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001835 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001836 break;
1837
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001838 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02001839#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001840 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02001841 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02001842#endif
1843 break;
1844
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001845 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001846 s->logs.level = rule->arg.loglevel;
1847 break;
1848
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001849 case ACT_HTTP_REPLACE_HDR:
1850 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001851 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1852 rule->arg.hdr_add.name_len,
1853 &rule->arg.hdr_add.fmt,
1854 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06001855 return HTTP_RULE_RES_BADREQ;
1856 break;
1857
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001858 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001859 ctx.idx = 0;
1860 /* remove all occurrences of the header */
1861 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001862 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001863 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001864 }
Willy Tarreau85603282015-01-21 20:39:27 +01001865 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001866
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001867 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001868 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001869 /* The scope of the trash buffer must be limited to this function. The
1870 * build_logline() function can execute a lot of other function which
1871 * can use the trash buffer. So for limiting the scope of this global
1872 * buffer, we build first the header value using build_logline, and
1873 * after we store the header name.
1874 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001875 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001876
1877 replace = alloc_trash_chunk();
1878 if (!replace)
1879 return HTTP_RULE_RES_BADREQ;
1880
Thierry Fournier4b788f72016-06-01 13:35:36 +02001881 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001882 len += build_logline(s, replace->area + len,
1883 replace->size - len,
1884 &rule->arg.hdr_add.fmt);
1885 memcpy(replace->area, rule->arg.hdr_add.name,
1886 rule->arg.hdr_add.name_len);
1887 replace->area[rule->arg.hdr_add.name_len] = ':';
1888 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1889 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001890
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001891 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001892 /* remove all occurrences of the header */
1893 ctx.idx = 0;
1894 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001895 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001896 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1897 }
1898 }
1899
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001900 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001901 static unsigned char rate_limit = 0;
1902
1903 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001904 replace->area[rule->arg.hdr_add.name_len] = 0;
1905 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,
1906 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001907 }
1908
1909 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
1910 if (sess->fe != s->be)
1911 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
1912 if (sess->listener->counters)
1913 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
1914 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001915
1916 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001917 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001918 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001919
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001920 case ACT_HTTP_DEL_ACL:
1921 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001922 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001923 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001924
1925 /* collect reference */
1926 ref = pat_ref_lookup(rule->arg.map.ref);
1927 if (!ref)
1928 continue;
1929
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001930 /* allocate key */
1931 key = alloc_trash_chunk();
1932 if (!key)
1933 return HTTP_RULE_RES_BADREQ;
1934
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001935 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001936 key->data = build_logline(s, key->area, key->size,
1937 &rule->arg.map.key);
1938 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001939
1940 /* perform update */
1941 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001942 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001943 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001944 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001945
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001946 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001947 break;
1948 }
1949
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001950 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001951 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001952 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001953
1954 /* collect reference */
1955 ref = pat_ref_lookup(rule->arg.map.ref);
1956 if (!ref)
1957 continue;
1958
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001959 /* allocate key */
1960 key = alloc_trash_chunk();
1961 if (!key)
1962 return HTTP_RULE_RES_BADREQ;
1963
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001964 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001965 key->data = build_logline(s, key->area, key->size,
1966 &rule->arg.map.key);
1967 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001968
1969 /* perform update */
1970 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001971 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001972 if (pat_ref_find_elt(ref, key->area) == NULL)
1973 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001974 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001975
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001976 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001977 break;
1978 }
1979
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001980 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001981 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001982 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001983
1984 /* collect reference */
1985 ref = pat_ref_lookup(rule->arg.map.ref);
1986 if (!ref)
1987 continue;
1988
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001989 /* allocate key */
1990 key = alloc_trash_chunk();
1991 if (!key)
1992 return HTTP_RULE_RES_BADREQ;
1993
1994 /* allocate value */
1995 value = alloc_trash_chunk();
1996 if (!value) {
1997 free_trash_chunk(key);
1998 return HTTP_RULE_RES_BADREQ;
1999 }
2000
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002001 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002002 key->data = build_logline(s, key->area, key->size,
2003 &rule->arg.map.key);
2004 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002005
2006 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002007 value->data = build_logline(s, value->area,
2008 value->size,
2009 &rule->arg.map.value);
2010 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002011
2012 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002013 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002014 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002015 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002016 else
2017 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002018 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002019
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002020 free_trash_chunk(key);
2021 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002022 break;
2023 }
William Lallemand73025dd2014-04-24 14:38:37 +02002024
Thierry FOURNIER42148732015-09-02 17:17:33 +02002025 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002026 if ((s->req.flags & CF_READ_ERROR) ||
2027 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2028 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2029 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002030 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002031
Willy Tarreauacc98002015-09-27 23:34:39 +02002032 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002033 case ACT_RET_ERR:
2034 case ACT_RET_CONT:
2035 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002036 case ACT_RET_STOP:
2037 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002038 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002039 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002040 return HTTP_RULE_RES_YIELD;
2041 }
William Lallemand73025dd2014-04-24 14:38:37 +02002042 break;
2043
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002044 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002045 /* Note: only the first valid tracking parameter of each
2046 * applies.
2047 */
2048
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002049 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002050 struct stktable *t;
2051 struct stksess *ts;
2052 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002053 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002054
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002055 t = rule->arg.trk_ctr.table.t;
2056 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 +02002057
2058 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002059 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002060
2061 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002062 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2063 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2064 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002065 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002066
Emeric Brun819fc6f2017-06-13 19:37:32 +02002067 if (ptr1)
2068 stktable_data_cast(ptr1, http_req_cnt)++;
2069
2070 if (ptr2)
2071 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2072 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2073
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002074 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01002075
2076 /* If data was modified, we need to touch to re-schedule sync */
2077 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002078 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002079
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002080 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002081 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002082 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002083 }
2084 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002085 break;
2086
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002087 /* other flags exists, but normaly, they never be matched. */
2088 default:
2089 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002090 }
2091 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002092
2093 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002094 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002095}
2096
Willy Tarreau71241ab2012-12-27 11:30:54 +01002097
Willy Tarreau51d861a2015-05-22 17:30:48 +02002098/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2099 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2100 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2101 * is returned, the process can continue the evaluation of next rule list. If
2102 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2103 * is returned, it means the operation could not be processed and a server error
2104 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2105 * deny rule. If *YIELD is returned, the caller must call again the function
2106 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002107 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002108enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002109http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002110{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002111 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002112 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002113 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002114 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002115 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002116 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002117
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002118 /* If "the current_rule_list" match the executed rule list, we are in
2119 * resume condition. If a resume is needed it is always in the action
2120 * and never in the ACL or converters. In this case, we initialise the
2121 * current rule, and go to the action execution point.
2122 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002123 if (s->current_rule) {
2124 rule = s->current_rule;
2125 s->current_rule = NULL;
2126 if (s->current_rule_list == rules)
2127 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002128 }
2129 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002130
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002131 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002132
2133 /* check optional condition */
2134 if (rule->cond) {
2135 int ret;
2136
Willy Tarreau192252e2015-04-04 01:47:55 +02002137 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002138 ret = acl_pass(ret);
2139
2140 if (rule->cond->pol == ACL_COND_UNLESS)
2141 ret = !ret;
2142
2143 if (!ret) /* condition not matched */
2144 continue;
2145 }
2146
Willy Tarreauacc98002015-09-27 23:34:39 +02002147 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002148resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002149 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002150 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002151 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002152
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002153 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002154 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002155 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002156
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002157 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002158 s->task->nice = rule->arg.nice;
2159 break;
2160
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002161 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002162 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002163 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002164 break;
2165
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002166 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002167#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002168 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002169 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002170#endif
2171 break;
2172
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002173 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002174 s->logs.level = rule->arg.loglevel;
2175 break;
2176
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002177 case ACT_HTTP_REPLACE_HDR:
2178 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002179 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2180 rule->arg.hdr_add.name_len,
2181 &rule->arg.hdr_add.fmt,
2182 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002183 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002184 break;
2185
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002186 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002187 ctx.idx = 0;
2188 /* remove all occurrences of the header */
2189 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002190 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002191 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2192 }
Willy Tarreau85603282015-01-21 20:39:27 +01002193 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002194
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002195 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002196 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02002197 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002198
2199 replace = alloc_trash_chunk();
2200 if (!replace)
2201 return HTTP_RULE_RES_BADREQ;
2202
2203 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002204 memcpy(replace->area, rule->arg.hdr_add.name,
2205 rule->arg.hdr_add.name_len);
2206 replace->data = rule->arg.hdr_add.name_len;
2207 replace->area[replace->data++] = ':';
2208 replace->area[replace->data++] = ' ';
2209 replace->data += build_logline(s,
2210 replace->area + replace->data,
2211 replace->size - replace->data,
2212 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002213
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002214 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002215 /* remove all occurrences of the header */
2216 ctx.idx = 0;
2217 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002218 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01002219 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2220 }
2221 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002222
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002223 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002224 static unsigned char rate_limit = 0;
2225
2226 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002227 replace->area[rule->arg.hdr_add.name_len] = 0;
2228 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,
2229 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002230 }
2231
2232 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2233 if (sess->fe != s->be)
2234 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2235 if (sess->listener->counters)
2236 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2237 if (objt_server(s->target))
2238 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
2239 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002240
2241 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002242 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002243 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002244
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002245 case ACT_HTTP_DEL_ACL:
2246 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002247 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002248 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002249
2250 /* collect reference */
2251 ref = pat_ref_lookup(rule->arg.map.ref);
2252 if (!ref)
2253 continue;
2254
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002255 /* allocate key */
2256 key = alloc_trash_chunk();
2257 if (!key)
2258 return HTTP_RULE_RES_BADREQ;
2259
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002260 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002261 key->data = build_logline(s, key->area, key->size,
2262 &rule->arg.map.key);
2263 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002264
2265 /* perform update */
2266 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002267 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002268 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002269 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002270
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002271 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002272 break;
2273 }
2274
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002275 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002276 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002277 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002278
2279 /* collect reference */
2280 ref = pat_ref_lookup(rule->arg.map.ref);
2281 if (!ref)
2282 continue;
2283
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002284 /* allocate key */
2285 key = alloc_trash_chunk();
2286 if (!key)
2287 return HTTP_RULE_RES_BADREQ;
2288
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002289 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002290 key->data = build_logline(s, key->area, key->size,
2291 &rule->arg.map.key);
2292 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002293
2294 /* perform update */
2295 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002296 if (pat_ref_find_elt(ref, key->area) == NULL)
2297 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002298
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002299 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002300 break;
2301 }
2302
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002303 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002304 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002305 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002306
2307 /* collect reference */
2308 ref = pat_ref_lookup(rule->arg.map.ref);
2309 if (!ref)
2310 continue;
2311
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002312 /* allocate key */
2313 key = alloc_trash_chunk();
2314 if (!key)
2315 return HTTP_RULE_RES_BADREQ;
2316
2317 /* allocate value */
2318 value = alloc_trash_chunk();
2319 if (!value) {
2320 free_trash_chunk(key);
2321 return HTTP_RULE_RES_BADREQ;
2322 }
2323
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002324 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002325 key->data = build_logline(s, key->area, key->size,
2326 &rule->arg.map.key);
2327 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002328
2329 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002330 value->data = build_logline(s, value->area,
2331 value->size,
2332 &rule->arg.map.value);
2333 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002334
2335 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002336 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002337 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002338 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002339 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002340 else
2341 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002342 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002343 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002344 free_trash_chunk(key);
2345 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002346 break;
2347 }
William Lallemand73025dd2014-04-24 14:38:37 +02002348
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002349 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02002350 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2351 return HTTP_RULE_RES_BADREQ;
2352 return HTTP_RULE_RES_DONE;
2353
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002354 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2355 /* Note: only the first valid tracking parameter of each
2356 * applies.
2357 */
2358
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002359 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002360 struct stktable *t;
2361 struct stksess *ts;
2362 struct stktable_key *key;
2363 void *ptr;
2364
2365 t = rule->arg.trk_ctr.table.t;
2366 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2367
2368 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002369 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002370
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002371 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002372
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002373 /* let's count a new HTTP request as it's the first time we do it */
2374 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2375 if (ptr)
2376 stktable_data_cast(ptr, http_req_cnt)++;
2377
2378 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2379 if (ptr)
2380 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2381 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2382
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002383 /* When the client triggers a 4xx from the server, it's most often due
2384 * to a missing object or permission. These events should be tracked
2385 * because if they happen often, it may indicate a brute force or a
2386 * vulnerability scan. Normally this is done when receiving the response
2387 * but here we're tracking after this ought to have been done so we have
2388 * to do it on purpose.
2389 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002390 if ((unsigned)(txn->status - 400) < 100) {
2391 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2392 if (ptr)
2393 stktable_data_cast(ptr, http_err_cnt)++;
2394
2395 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2396 if (ptr)
2397 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2398 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2399 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002400
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002401 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002402
Emeric Brun0fed0b02017-11-29 16:15:07 +01002403 /* If data was modified, we need to touch to re-schedule sync */
2404 stktable_touch_local(t, ts, 0);
2405
Emeric Brun819fc6f2017-06-13 19:37:32 +02002406 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2407 if (sess->fe != s->be)
2408 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2409
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002410 }
2411 }
2412 break;
2413
Thierry FOURNIER42148732015-09-02 17:17:33 +02002414 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002415 if ((s->req.flags & CF_READ_ERROR) ||
2416 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2417 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2418 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002419 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002420
Willy Tarreauacc98002015-09-27 23:34:39 +02002421 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002422 case ACT_RET_ERR:
2423 case ACT_RET_CONT:
2424 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002425 case ACT_RET_STOP:
2426 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002427 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002428 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002429 return HTTP_RULE_RES_YIELD;
2430 }
William Lallemand73025dd2014-04-24 14:38:37 +02002431 break;
2432
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002433 /* other flags exists, but normaly, they never be matched. */
2434 default:
2435 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002436 }
2437 }
2438
2439 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002440 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002441}
2442
2443
Willy Tarreau71241ab2012-12-27 11:30:54 +01002444/* Perform an HTTP redirect based on the information in <rule>. The function
2445 * returns non-zero on success, or zero in case of a, irrecoverable error such
2446 * as too large a request to build a valid response.
2447 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002448int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002449{
Willy Tarreaub329a312015-05-22 16:27:37 +02002450 struct http_msg *req = &txn->req;
2451 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002452 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002453 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002454 int ret = 0;
2455
2456 chunk = alloc_trash_chunk();
2457 if (!chunk)
2458 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002459
2460 /* build redirect message */
2461 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002462 case 308:
2463 msg_fmt = HTTP_308;
2464 break;
2465 case 307:
2466 msg_fmt = HTTP_307;
2467 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002468 case 303:
2469 msg_fmt = HTTP_303;
2470 break;
2471 case 301:
2472 msg_fmt = HTTP_301;
2473 break;
2474 case 302:
2475 default:
2476 msg_fmt = HTTP_302;
2477 break;
2478 }
2479
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002480 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2481 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002482
2483 switch(rule->type) {
2484 case REDIRECT_TYPE_SCHEME: {
2485 const char *path;
2486 const char *host;
2487 struct hdr_ctx ctx;
2488 int pathlen;
2489 int hostlen;
2490
2491 host = "";
2492 hostlen = 0;
2493 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002494 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002495 host = ctx.line + ctx.val;
2496 hostlen = ctx.vlen;
2497 }
2498
Willy Tarreau6b952c82018-09-10 17:45:34 +02002499 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002500 /* build message using path */
2501 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002502 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002503 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2504 int qs = 0;
2505 while (qs < pathlen) {
2506 if (path[qs] == '?') {
2507 pathlen = qs;
2508 break;
2509 }
2510 qs++;
2511 }
2512 }
2513 } else {
2514 path = "/";
2515 pathlen = 1;
2516 }
2517
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002518 if (rule->rdr_str) { /* this is an old "redirect" rule */
2519 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002520 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002521 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002522
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002523 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002524 memcpy(chunk->area + chunk->data, rule->rdr_str,
2525 rule->rdr_len);
2526 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002527 }
2528 else {
2529 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002530 chunk->data += build_logline(s,
2531 chunk->area + chunk->data,
2532 chunk->size - chunk->data,
2533 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002534
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002535 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002536 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002537 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002538 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002539 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002540 memcpy(chunk->area + chunk->data, "://", 3);
2541 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002542
2543 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002544 memcpy(chunk->area + chunk->data, host, hostlen);
2545 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002546
2547 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002548 memcpy(chunk->area + chunk->data, path, pathlen);
2549 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002550
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002551 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002552 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002553 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002554 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002555 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002556 chunk->area[chunk->data] = '/';
2557 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002558 }
2559
2560 break;
2561 }
2562 case REDIRECT_TYPE_PREFIX: {
2563 const char *path;
2564 int pathlen;
2565
Willy Tarreau6b952c82018-09-10 17:45:34 +02002566 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002567 /* build message using path */
2568 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002569 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002570 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2571 int qs = 0;
2572 while (qs < pathlen) {
2573 if (path[qs] == '?') {
2574 pathlen = qs;
2575 break;
2576 }
2577 qs++;
2578 }
2579 }
2580 } else {
2581 path = "/";
2582 pathlen = 1;
2583 }
2584
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002585 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002586 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002587 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002588
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002589 /* add prefix. Note that if prefix == "/", we don't want to
2590 * add anything, otherwise it makes it hard for the user to
2591 * configure a self-redirection.
2592 */
2593 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002594 memcpy(chunk->area + chunk->data,
2595 rule->rdr_str, rule->rdr_len);
2596 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002597 }
2598 }
2599 else {
2600 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002601 chunk->data += build_logline(s,
2602 chunk->area + chunk->data,
2603 chunk->size - chunk->data,
2604 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002605
2606 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002607 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002608 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002609 }
2610
2611 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002612 memcpy(chunk->area + chunk->data, path, pathlen);
2613 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002614
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002615 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002616 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002617 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002618 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002619 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002620 chunk->area[chunk->data] = '/';
2621 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002622 }
2623
2624 break;
2625 }
2626 case REDIRECT_TYPE_LOCATION:
2627 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002628 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002629 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002630 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002631
2632 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002633 memcpy(chunk->area + chunk->data, rule->rdr_str,
2634 rule->rdr_len);
2635 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002636 }
2637 else {
2638 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002639 chunk->data += build_logline(s,
2640 chunk->area + chunk->data,
2641 chunk->size - chunk->data,
2642 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002643
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002644 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002645 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002646 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002647 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002648 break;
2649 }
2650
2651 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002652 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2653 chunk->data += 14;
2654 memcpy(chunk->area + chunk->data, rule->cookie_str,
2655 rule->cookie_len);
2656 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002657 }
2658
Willy Tarreau19b14122017-02-28 09:48:11 +01002659 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002660 txn->status = rule->code;
2661 /* let's log the request time */
2662 s->logs.tv_request = now;
2663
Christopher Fauletbe821b92017-03-30 11:21:53 +02002664 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002665 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2666 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2667 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002668 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002669 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002670 memcpy(chunk->area + chunk->data,
2671 "\r\nProxy-Connection: keep-alive", 30);
2672 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002673 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002674 memcpy(chunk->area + chunk->data,
2675 "\r\nConnection: keep-alive", 24);
2676 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002677 }
2678 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002679 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2680 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002681 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002682 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002683 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002684 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002685 req->next -= req->sov;
2686 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002687 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002688 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002689 req->msg_state = HTTP_MSG_CLOSED;
2690 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002691 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002692 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002693 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002694 /* let the server side turn to SI_ST_CLO */
2695 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002696 } else {
2697 /* keep-alive not possible */
2698 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002699 memcpy(chunk->area + chunk->data,
2700 "\r\nProxy-Connection: close\r\n\r\n", 29);
2701 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002702 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002703 memcpy(chunk->area + chunk->data,
2704 "\r\nConnection: close\r\n\r\n", 23);
2705 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002706 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002707 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002708 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002709 }
2710
Willy Tarreaue7dff022015-04-03 01:14:29 +02002711 if (!(s->flags & SF_ERR_MASK))
2712 s->flags |= SF_ERR_LOCAL;
2713 if (!(s->flags & SF_FINST_MASK))
2714 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002715
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002716 ret = 1;
2717 leave:
2718 free_trash_chunk(chunk);
2719 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002720}
2721
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002722/* This stream analyser runs all HTTP request processing which is common to
2723 * frontends and backends, which means blocking ACLs, filters, connection-close,
2724 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002725 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002726 * either needs more data or wants to immediately abort the request (eg: deny,
2727 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002728 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002729int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002730{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002731 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002732 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002733 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002734 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002735 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002736 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002737 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002738 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002739
Willy Tarreau655dce92009-11-08 13:10:58 +01002740 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002741 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002742 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002743 }
2744
Christopher Faulet45073512018-07-20 10:16:29 +02002745 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 +02002746 now_ms, __FUNCTION__,
2747 s,
2748 req,
2749 req->rex, req->wex,
2750 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002751 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002752 req->analysers);
2753
Willy Tarreau65410832014-04-28 21:25:43 +02002754 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002755 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002756
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002757 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002758 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002759 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002760
Willy Tarreau0b748332014-04-29 00:13:29 +02002761 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002762 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2763 goto return_prx_yield;
2764
Willy Tarreau0b748332014-04-29 00:13:29 +02002765 case HTTP_RULE_RES_CONT:
2766 case HTTP_RULE_RES_STOP: /* nothing to do */
2767 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002768
Willy Tarreau0b748332014-04-29 00:13:29 +02002769 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2770 if (txn->flags & TX_CLTARPIT)
2771 goto tarpit;
2772 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002773
Willy Tarreau0b748332014-04-29 00:13:29 +02002774 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2775 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002776
Willy Tarreau0b748332014-04-29 00:13:29 +02002777 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002778 goto done;
2779
Willy Tarreau0b748332014-04-29 00:13:29 +02002780 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2781 goto return_bad_req;
2782 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002783 }
2784
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002785 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2786 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002787 struct hdr_ctx ctx;
2788
2789 ctx.idx = 0;
2790 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002791 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002792 if (unlikely(http_header_add_tail2(&txn->req,
2793 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002794 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002795 goto return_bad_req;
2796 }
2797 }
2798
2799 }
2800
Willy Tarreau52542592014-04-28 18:33:26 +02002801 /* OK at this stage, we know that the request was accepted according to
2802 * the http-request rules, we can check for the stats. Note that the
2803 * URI is detected *before* the req* rules in order not to be affected
2804 * by a possible reqrep, while they are processed *after* so that a
2805 * reqdeny can still block them. This clearly needs to change in 1.6!
2806 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002807 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002808 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01002809 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002810 txn->status = 500;
2811 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002812 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002813
Willy Tarreaue7dff022015-04-03 01:14:29 +02002814 if (!(s->flags & SF_ERR_MASK))
2815 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002816 goto return_prx_cond;
2817 }
2818
2819 /* parse the whole stats request and extract the relevant information */
2820 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002821 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002822 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002823
Willy Tarreau0b748332014-04-29 00:13:29 +02002824 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2825 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002826
Willy Tarreau0b748332014-04-29 00:13:29 +02002827 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2828 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002829 }
2830
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002831 /* evaluate the req* rules except reqadd */
2832 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002833 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002834 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002835
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002836 if (txn->flags & TX_CLDENY)
2837 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002838
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002839 if (txn->flags & TX_CLTARPIT) {
2840 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002841 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002842 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002843 }
Willy Tarreau06619262006-12-17 08:37:22 +01002844
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002845 /* add request headers from the rule sets in the same order */
2846 list_for_each_entry(wl, &px->req_add, list) {
2847 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002848 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002849 ret = acl_pass(ret);
2850 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2851 ret = !ret;
2852 if (!ret)
2853 continue;
2854 }
2855
Christopher Faulet10079f52018-10-03 15:17:28 +02002856 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002857 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002858 }
2859
Willy Tarreau52542592014-04-28 18:33:26 +02002860
2861 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002862 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2863 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002864 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002865 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002866 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002867
Willy Tarreaue7dff022015-04-03 01:14:29 +02002868 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2869 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2870 if (!(s->flags & SF_FINST_MASK))
2871 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002872
Willy Tarreau70730dd2014-04-24 18:06:27 +02002873 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002874 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2875 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002876 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002877 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002878 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002879
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002880 /* check whether we have some ACLs set to redirect this request */
2881 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002882 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002883 int ret;
2884
Willy Tarreau192252e2015-04-04 01:47:55 +02002885 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002886 ret = acl_pass(ret);
2887 if (rule->cond->pol == ACL_COND_UNLESS)
2888 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002889 if (!ret)
2890 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01002891 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002892 if (!http_apply_redirect_rule(rule, s, txn))
2893 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002894 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002895 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002896
Willy Tarreau2be39392010-01-03 17:24:51 +01002897 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2898 * If this happens, then the data will not come immediately, so we must
2899 * send all what we have without waiting. Note that due to the small gain
2900 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002901 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01002902 * itself once used.
2903 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002904 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01002905
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002906 done: /* done with this analyser, continue with next ones that the calling
2907 * points will have set, if any.
2908 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002909 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002910 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
2911 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002912 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002913
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002914 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02002915 /* Allow cookie logging
2916 */
2917 if (s->be->cookie_name || sess->fe->capture_name)
2918 manage_client_side_cookies(s, req);
2919
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002920 /* When a connection is tarpitted, we use the tarpit timeout,
2921 * which may be the same as the connect timeout if unspecified.
2922 * If unset, then set it to zero because we really want it to
2923 * eventually expire. We build the tarpit as an analyser.
2924 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002925 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002926
2927 /* wipe the request out so that we can drop the connection early
2928 * if the client closes first.
2929 */
2930 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002931
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002932 txn->status = http_err_codes[deny_status];
2933
Christopher Faulet0184ea72017-01-05 14:06:34 +01002934 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002935 req->analysers |= AN_REQ_HTTP_TARPIT;
2936 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2937 if (!req->analyse_exp)
2938 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02002939 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002940 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002941 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002942 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002943 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002944 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002945 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002946
2947 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002948
2949 /* Allow cookie logging
2950 */
2951 if (s->be->cookie_name || sess->fe->capture_name)
2952 manage_client_side_cookies(s, req);
2953
Willy Tarreau0b748332014-04-29 00:13:29 +02002954 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002955 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002956 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002957 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02002958 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002959 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002960 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002961 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002962 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002963 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002964 goto return_prx_cond;
2965
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002966 return_bad_req:
2967 /* We centralize bad requests processing here */
2968 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2969 /* we detected a parsing error. We want to archive this request
2970 * in the dedicated proxy area for later troubleshooting.
2971 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02002972 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002973 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002974
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002975 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002976 txn->req.msg_state = HTTP_MSG_ERROR;
2977 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002978 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002979
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002980 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002981 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002982 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002983
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002984 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002985 if (!(s->flags & SF_ERR_MASK))
2986 s->flags |= SF_ERR_PRXCOND;
2987 if (!(s->flags & SF_FINST_MASK))
2988 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002989
Christopher Faulet0184ea72017-01-05 14:06:34 +01002990 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002991 req->analyse_exp = TICK_ETERNITY;
2992 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002993
2994 return_prx_yield:
2995 channel_dont_connect(req);
2996 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002997}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002998
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002999/* This function performs all the processing enabled for the current request.
3000 * It returns 1 if the processing can continue on next analysers, or zero if it
3001 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003002 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003003 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003004int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003005{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003006 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003007 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003008 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003009 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003010
Willy Tarreau655dce92009-11-08 13:10:58 +01003011 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003012 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003013 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003014 return 0;
3015 }
3016
Christopher Faulet45073512018-07-20 10:16:29 +02003017 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 +02003018 now_ms, __FUNCTION__,
3019 s,
3020 req,
3021 req->rex, req->wex,
3022 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003023 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003024 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003025
Willy Tarreau59234e92008-11-30 23:51:27 +01003026 /*
3027 * Right now, we know that we have processed the entire headers
3028 * and that unwanted requests have been filtered out. We can do
3029 * whatever we want with the remaining request. Also, now we
3030 * may have separate values for ->fe, ->be.
3031 */
Willy Tarreau06619262006-12-17 08:37:22 +01003032
Willy Tarreau59234e92008-11-30 23:51:27 +01003033 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003034 * If HTTP PROXY is set we simply get remote server address parsing
3035 * incoming request. Note that this requires that a connection is
3036 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003037 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003038 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003039 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003040 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003041
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003042 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003043 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003044 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003045 txn->req.msg_state = HTTP_MSG_ERROR;
3046 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003047 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003048 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003049
Willy Tarreaue7dff022015-04-03 01:14:29 +02003050 if (!(s->flags & SF_ERR_MASK))
3051 s->flags |= SF_ERR_RESOURCE;
3052 if (!(s->flags & SF_FINST_MASK))
3053 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003054
3055 return 0;
3056 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003057
Willy Tarreau6b952c82018-09-10 17:45:34 +02003058 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02003059 if (url2sa(ci_head(req) + msg->sl.rq.u,
3060 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02003061 &conn->addr.to, NULL) == -1)
3062 goto return_bad_req;
3063
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003064 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02003065 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
3066 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003067 * u_l characters by a single "/".
3068 */
3069 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003070 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003071 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3072 int delta;
3073
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003074 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
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 }
3080 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003081 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003082 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3083 int delta;
3084
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003085 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003086 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003087 http_msg_move_end(&txn->req, delta);
3088 cur_end += delta;
3089 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3090 goto return_bad_req;
3091 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003092 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003093
Willy Tarreau59234e92008-11-30 23:51:27 +01003094 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003095 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003096 * Note that doing so might move headers in the request, but
3097 * the fields will stay coherent and the URI will not move.
3098 * This should only be performed in the backend.
3099 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003100 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003101 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003102
William Lallemanda73203e2012-03-12 12:48:57 +01003103 /* add unique-id if "header-unique-id" is specified */
3104
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003105 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01003106 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003107 goto return_bad_req;
3108 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003109 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003110 }
William Lallemanda73203e2012-03-12 12:48:57 +01003111
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003112 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02003113 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003114 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003115 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003116 goto return_bad_req;
3117 }
3118
Cyril Bontéb21570a2009-11-29 20:04:48 +01003119 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003120 * 9: add X-Forwarded-For if either the frontend or the backend
3121 * asks for it.
3122 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003123 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003124 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003125 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3126 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3127 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02003128 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003129 /* The header is set to be added only if none is present
3130 * and we found it, so don't do anything.
3131 */
3132 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003133 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003134 /* Add an X-Forwarded-For header unless the source IP is
3135 * in the 'except' network range.
3136 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003137 if ((!sess->fe->except_mask.s_addr ||
3138 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3139 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003140 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003141 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003142 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003143 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003144 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003145 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003146
3147 /* Note: we rely on the backend to get the header name to be used for
3148 * x-forwarded-for, because the header is really meant for the backends.
3149 * However, if the backend did not specify any option, we have to rely
3150 * on the frontend's header name.
3151 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003152 if (s->be->fwdfor_hdr_len) {
3153 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003154 memcpy(trash.area,
3155 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003156 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003157 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003158 memcpy(trash.area,
3159 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003160 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003161 len += snprintf(trash.area + len,
3162 trash.size - len,
3163 ": %d.%d.%d.%d", pn[0], pn[1],
3164 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003165
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003166 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003167 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003168 }
3169 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003170 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003171 /* FIXME: for the sake of completeness, we should also support
3172 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003173 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003174 int len;
3175 char pn[INET6_ADDRSTRLEN];
3176 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003177 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003178 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003179
Willy Tarreau59234e92008-11-30 23:51:27 +01003180 /* Note: we rely on the backend to get the header name to be used for
3181 * x-forwarded-for, because the header is really meant for the backends.
3182 * However, if the backend did not specify any option, we have to rely
3183 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003184 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003185 if (s->be->fwdfor_hdr_len) {
3186 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003187 memcpy(trash.area, s->be->fwdfor_hdr_name,
3188 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003189 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003190 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003191 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
3192 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003193 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003194 len += snprintf(trash.area + len, trash.size - len,
3195 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003196
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003197 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003198 goto return_bad_req;
3199 }
3200 }
3201
3202 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003203 * 10: add X-Original-To if either the frontend or the backend
3204 * asks for it.
3205 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003206 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003207
3208 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003209 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003210 /* Add an X-Original-To header unless the destination IP is
3211 * in the 'except' network range.
3212 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003213 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003214
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003215 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003216 ((!sess->fe->except_mask_to.s_addr ||
3217 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3218 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003219 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003220 (((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 +02003221 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003222 int len;
3223 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003224 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003225
3226 /* Note: we rely on the backend to get the header name to be used for
3227 * x-original-to, because the header is really meant for the backends.
3228 * However, if the backend did not specify any option, we have to rely
3229 * on the frontend's header name.
3230 */
3231 if (s->be->orgto_hdr_len) {
3232 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003233 memcpy(trash.area,
3234 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003235 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003236 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003237 memcpy(trash.area,
3238 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003239 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003240 len += snprintf(trash.area + len,
3241 trash.size - len,
3242 ": %d.%d.%d.%d", pn[0], pn[1],
3243 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003244
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003245 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003246 goto return_bad_req;
3247 }
3248 }
3249 }
3250
Willy Tarreau50fc7772012-11-11 22:19:57 +01003251 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3252 * If an "Upgrade" token is found, the header is left untouched in order not to have
3253 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3254 * "Upgrade" is present in the Connection header.
3255 */
3256 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3257 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003258 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003259 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003260 unsigned int want_flags = 0;
3261
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003262 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003263 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003264 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003265 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003266 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003267 want_flags |= TX_CON_CLO_SET;
3268 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003269 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003270 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003271 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003272 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003273 want_flags |= TX_CON_KAL_SET;
3274 }
3275
3276 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003277 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003278 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003279
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003280
Willy Tarreau522d6c02009-12-06 18:49:18 +01003281 /* If we have no server assigned yet and we're balancing on url_param
3282 * with a POST request, we may be interested in checking the body for
3283 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003284 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003285 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003286 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003287 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003288 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003289 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003290 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003291
Christopher Fauletbe821b92017-03-30 11:21:53 +02003292 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3293 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003294#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003295 /* We expect some data from the client. Unless we know for sure
3296 * we already have a full request, we have to re-enable quick-ack
3297 * in case we previously disabled it, otherwise we might cause
3298 * the client to delay further data.
3299 */
3300 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3301 cli_conn && conn_ctrl_ready(cli_conn) &&
3302 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003303 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003304 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003305#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003306
Willy Tarreau59234e92008-11-30 23:51:27 +01003307 /*************************************************************
3308 * OK, that's finished for the headers. We have done what we *
3309 * could. Let's switch to the DATA state. *
3310 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003311 req->analyse_exp = TICK_ETERNITY;
3312 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003313
Willy Tarreau59234e92008-11-30 23:51:27 +01003314 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003315 /* OK let's go on with the BODY now */
3316 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003317
Willy Tarreau59234e92008-11-30 23:51:27 +01003318 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003319 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003320 /* we detected a parsing error. We want to archive this request
3321 * in the dedicated proxy area for later troubleshooting.
3322 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003323 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003324 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003325
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003326 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003327 txn->req.msg_state = HTTP_MSG_ERROR;
3328 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003329 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003330 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003331
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003332 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003333 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003334 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003335
Willy Tarreaue7dff022015-04-03 01:14:29 +02003336 if (!(s->flags & SF_ERR_MASK))
3337 s->flags |= SF_ERR_PRXCOND;
3338 if (!(s->flags & SF_FINST_MASK))
3339 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003340 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003341}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003342
Willy Tarreau60b85b02008-11-30 23:28:40 +01003343/* This function is an analyser which processes the HTTP tarpit. It always
3344 * returns zero, at the beginning because it prevents any other processing
3345 * from occurring, and at the end because it terminates the request.
3346 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003347int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003348{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003349 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003350
3351 /* This connection is being tarpitted. The CLIENT side has
3352 * already set the connect expiration date to the right
3353 * timeout. We just have to check that the client is still
3354 * there and that the timeout has not expired.
3355 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003356 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003357 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003358 !tick_is_expired(req->analyse_exp, now_ms))
3359 return 0;
3360
3361 /* We will set the queue timer to the time spent, just for
3362 * logging purposes. We fake a 500 server error, so that the
3363 * attacker will not suspect his connection has been tarpitted.
3364 * It will not cause trouble to the logs because we can exclude
3365 * the tarpitted connections by filtering on the 'PT' status flags.
3366 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003367 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3368
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003369 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003370 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003371
Christopher Faulet0184ea72017-01-05 14:06:34 +01003372 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003373 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003374
Willy Tarreaue7dff022015-04-03 01:14:29 +02003375 if (!(s->flags & SF_ERR_MASK))
3376 s->flags |= SF_ERR_PRXCOND;
3377 if (!(s->flags & SF_FINST_MASK))
3378 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003379 return 0;
3380}
3381
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003382/* This function is an analyser which waits for the HTTP request body. It waits
3383 * for either the buffer to be full, or the full advertised contents to have
3384 * reached the buffer. It must only be called after the standard HTTP request
3385 * processing has occurred, because it expects the request to be parsed and will
3386 * look for the Expect header. It may send a 100-Continue interim response. It
3387 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3388 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3389 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003390 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003391int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003392{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003393 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003394 struct http_txn *txn = s->txn;
3395 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003396
3397 /* We have to parse the HTTP request body to find any required data.
3398 * "balance url_param check_post" should have been the only way to get
3399 * into this. We were brought here after HTTP header analysis, so all
3400 * related structures are ready.
3401 */
3402
Willy Tarreau890988f2014-04-10 11:59:33 +02003403 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3404 /* This is the first call */
3405 if (msg->msg_state < HTTP_MSG_BODY)
3406 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003407
Willy Tarreau890988f2014-04-10 11:59:33 +02003408 if (msg->msg_state < HTTP_MSG_100_SENT) {
3409 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3410 * send an HTTP/1.1 100 Continue intermediate response.
3411 */
3412 if (msg->flags & HTTP_MSGF_VER_11) {
3413 struct hdr_ctx ctx;
3414 ctx.idx = 0;
3415 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003416 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003417 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003418 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003419 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003420 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003421 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003422 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003423 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003424
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003425 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003426 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003427 * must save the body in msg->next because it survives buffer
3428 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003429 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003430 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003431
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003432 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003433 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3434 else
3435 msg->msg_state = HTTP_MSG_DATA;
3436 }
3437
Willy Tarreau890988f2014-04-10 11:59:33 +02003438 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3439 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003440 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003441 goto missing_data;
3442
3443 /* OK we have everything we need now */
3444 goto http_end;
3445 }
3446
3447 /* OK here we're parsing a chunked-encoded message */
3448
Willy Tarreau522d6c02009-12-06 18:49:18 +01003449 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003450 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003451 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003452 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003453 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003454 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003455 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003456
Willy Tarreau115acb92009-12-26 13:56:06 +01003457 if (!ret)
3458 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003459 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003460 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003461 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003462 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003463 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003464 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003465 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003466
3467 msg->chunk_len = chunk;
3468 msg->body_len += chunk;
3469
3470 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003471 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003472 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003473 }
3474
Willy Tarreaud98cf932009-12-27 22:54:55 +01003475 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003476 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3477 * for at least a whole chunk or the whole content length bytes after
3478 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003479 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003480 if (msg->msg_state == HTTP_MSG_TRAILERS)
3481 goto http_end;
3482
Willy Tarreaue115b492015-05-01 23:05:14 +02003483 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003484 goto http_end;
3485
3486 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003487 /* we get here if we need to wait for more data. If the buffer is full,
3488 * we have the maximum we can expect.
3489 */
Willy Tarreau23752332018-06-15 14:54:53 +02003490 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003491 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003492
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003493 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003494 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003495 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003496
Willy Tarreaue7dff022015-04-03 01:14:29 +02003497 if (!(s->flags & SF_ERR_MASK))
3498 s->flags |= SF_ERR_CLITO;
3499 if (!(s->flags & SF_FINST_MASK))
3500 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003501 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003502 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003503
3504 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003505 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003506 /* Not enough data. We'll re-use the http-request
3507 * timeout here. Ideally, we should set the timeout
3508 * relative to the accept() date. We just set the
3509 * request timeout once at the beginning of the
3510 * request.
3511 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003512 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003513 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003514 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003515 return 0;
3516 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003517
3518 http_end:
3519 /* The situation will not evolve, so let's give up on the analysis. */
3520 s->logs.tv_request = now; /* update the request timer to reflect full request */
3521 req->analysers &= ~an_bit;
3522 req->analyse_exp = TICK_ETERNITY;
3523 return 1;
3524
3525 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003526 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003527 txn->req.msg_state = HTTP_MSG_ERROR;
3528 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003529 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003530
Willy Tarreaue7dff022015-04-03 01:14:29 +02003531 if (!(s->flags & SF_ERR_MASK))
3532 s->flags |= SF_ERR_PRXCOND;
3533 if (!(s->flags & SF_FINST_MASK))
3534 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003535
Willy Tarreau522d6c02009-12-06 18:49:18 +01003536 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003537 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003538 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003539 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003540 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003541 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003542}
3543
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003544/* send a server's name with an outgoing request over an established connection.
3545 * Note: this function is designed to be called once the request has been scheduled
3546 * for being forwarded. This is the reason why it rewinds the buffer before
3547 * proceeding.
3548 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003549int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003550
3551 struct hdr_ctx ctx;
3552
Mark Lamourinec2247f02012-01-04 13:02:01 -05003553 char *hdr_name = be->server_id_hdr_name;
3554 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003555 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003556 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003557 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003558
William Lallemandd9e90662012-01-30 17:27:17 +01003559 ctx.idx = 0;
3560
Willy Tarreau211cdec2014-04-17 20:18:08 +02003561 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003562 if (old_o) {
3563 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003564 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003565 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003566 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003567 }
3568
Willy Tarreauf37954d2018-06-15 18:31:02 +02003569 old_i = ci_data(chn);
3570 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 -05003571 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003572 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003573 }
3574
3575 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003576 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003577 memcpy(hdr_val, hdr_name, hdr_name_len);
3578 hdr_val += hdr_name_len;
3579 *hdr_val++ = ':';
3580 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003581 hdr_val += strlcpy2(hdr_val, srv_name,
3582 trash.area + trash.size - hdr_val);
3583 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3584 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003585
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003586 if (old_o) {
3587 /* If this was a forwarded request, we must readjust the amount of
3588 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003589 * variations. Note that the current state is >= HTTP_MSG_BODY,
3590 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003591 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003592 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003593 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003594 txn->req.next -= old_o;
3595 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003596 }
3597
Mark Lamourinec2247f02012-01-04 13:02:01 -05003598 return 0;
3599}
3600
Willy Tarreau610ecce2010-01-04 21:15:02 +01003601/* Terminate current transaction and prepare a new one. This is very tricky
3602 * right now but it works.
3603 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003604void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003605{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003606 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003607 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003608 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003609 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003610 struct connection *srv_conn;
3611 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003612 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003613
Willy Tarreau610ecce2010-01-04 21:15:02 +01003614 /* FIXME: We need a more portable way of releasing a backend's and a
3615 * server's connections. We need a safer way to reinitialize buffer
3616 * flags. We also need a more accurate method for computing per-request
3617 * data.
3618 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003619 /*
3620 * XXX cognet: This is probably wrong, this is killing a whole
3621 * connection, in the new world order, we probably want to just kill
3622 * the stream, this is to be revisited the day we handle multiple
3623 * streams in one server connection.
3624 */
3625 cs = objt_cs(s->si[1].end);
3626 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003627
Willy Tarreau4213a112013-12-15 10:25:42 +01003628 /* unless we're doing keep-alive, we want to quickly close the connection
3629 * to the server.
3630 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003631 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003632 !si_conn_ready(&s->si[1])) {
3633 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3634 si_shutr(&s->si[1]);
3635 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003636 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003637
Willy Tarreaue7dff022015-04-03 01:14:29 +02003638 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003639 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003640 if (unlikely(s->srv_conn))
3641 sess_change_server(s, NULL);
3642 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003643
3644 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003645 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003646
Willy Tarreaueee5b512015-04-03 23:46:31 +02003647 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003648 int n;
3649
Willy Tarreaueee5b512015-04-03 23:46:31 +02003650 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003651 if (n < 1 || n > 5)
3652 n = 0;
3653
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003654 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003655 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003656 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003657 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003658 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003659 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3660 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003661 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003662 }
3663
3664 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003665 s->logs.bytes_in -= ci_data(&s->req);
3666 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003667
Willy Tarreau66425e32018-07-25 06:55:12 +02003668 /* we may need to know the position in the queue */
3669 pendconn_free(s);
3670
Willy Tarreau610ecce2010-01-04 21:15:02 +01003671 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003672 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003673 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003674 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003675 s->do_log(s);
3676 }
3677
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003678 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003679 stream_stop_content_counters(s);
3680 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003681
Willy Tarreau610ecce2010-01-04 21:15:02 +01003682 s->logs.accept_date = date; /* user-visible date for logging */
3683 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003684 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3685 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003686 tv_zero(&s->logs.tv_request);
3687 s->logs.t_queue = -1;
3688 s->logs.t_connect = -1;
3689 s->logs.t_data = -1;
3690 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003691 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3692 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003693
Willy Tarreauf37954d2018-06-15 18:31:02 +02003694 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3695 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003696
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003697 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003698 if (s->flags & SF_CURR_SESS) {
3699 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02003700 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003701 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003702 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003703 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003704 }
3705
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003706 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003707
Willy Tarreau4213a112013-12-15 10:25:42 +01003708 /* only release our endpoint if we don't intend to reuse the
3709 * connection.
3710 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003711 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01003712 !si_conn_ready(&s->si[1])) {
3713 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02003714 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003715 }
3716
Willy Tarreau350f4872014-11-28 14:42:25 +01003717 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3718 s->si[1].err_type = SI_ET_NONE;
3719 s->si[1].conn_retries = 0; /* used for logging too */
3720 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003721 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 +01003722 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 +01003723 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 +02003724 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3725 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3726 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003727
Patrick Hemmere3faf022018-08-22 10:02:00 -04003728 hlua_ctx_destroy(s->hlua);
3729 s->hlua = NULL;
3730
Willy Tarreaueee5b512015-04-03 23:46:31 +02003731 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003732 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003733 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003734
3735 if (prev_status == 401 || prev_status == 407) {
3736 /* In HTTP keep-alive mode, if we receive a 401, we still have
3737 * a chance of being able to send the visitor again to the same
3738 * server over the same connection. This is required by some
3739 * broken protocols such as NTLM, and anyway whenever there is
3740 * an opportunity for sending the challenge to the proper place,
3741 * it's better to do it (at least it helps with debugging).
3742 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003743 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02003744 if (srv_conn)
3745 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01003746 }
3747
Willy Tarreau53f96852016-02-02 18:50:47 +01003748 /* Never ever allow to reuse a connection from a non-reuse backend */
3749 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3750 srv_conn->flags |= CO_FL_PRIVATE;
3751
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003752 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003753 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003754
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003755 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003756 s->req.flags |= CF_NEVER_WAIT;
3757 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003758 }
3759
Willy Tarreau714ea782015-11-25 20:11:11 +01003760 /* we're removing the analysers, we MUST re-enable events detection.
3761 * We don't enable close on the response channel since it's either
3762 * already closed, or in keep-alive with an idle connection handler.
3763 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003764 channel_auto_read(&s->req);
3765 channel_auto_close(&s->req);
3766 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003767
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003768 /* we're in keep-alive with an idle connection, monitor it if not already done */
3769 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003770 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02003771 if (!srv)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003772 si_idle_cs(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01003773 else if (srv_conn->flags & CO_FL_PRIVATE)
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003774 si_idle_cs(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02003775 else if (prev_flags & TX_NOT_FIRST)
3776 /* note: we check the request, not the connection, but
3777 * this is valid for strategies SAFE and AGGR, and in
3778 * case of ALWS, we don't care anyway.
3779 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003780 si_idle_cs(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02003781 else
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003782 si_idle_cs(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003783 }
Christopher Faulete6006242017-03-10 11:52:44 +01003784 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3785 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003786}
3787
3788
3789/* This function updates the request state machine according to the response
3790 * state machine and buffer flags. It returns 1 if it changes anything (flag
3791 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3792 * it is only used to find when a request/response couple is complete. Both
3793 * this function and its equivalent should loop until both return zero. It
3794 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3795 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003796int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003797{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003798 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003799 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003800 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003801 unsigned int old_state = txn->req.msg_state;
3802
Christopher Faulet4be98032017-07-18 10:48:24 +02003803 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003804 return 0;
3805
3806 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003807 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003808 * We can shut the read side unless we want to abort_on_close,
3809 * or we have a POST request. The issue with POST requests is
3810 * that some browsers still send a CRLF after the request, and
3811 * this CRLF must be read so that it does not remain in the kernel
3812 * buffers, otherwise a close could cause an RST on some systems
3813 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003814 * Note that if we're using keep-alive on the client side, we'd
3815 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003816 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003817 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003818 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003819 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3820 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003821 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3822 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003823 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003824 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003825
Willy Tarreau40f151a2012-12-20 12:10:09 +01003826 /* if the server closes the connection, we want to immediately react
3827 * and close the socket to save packets and syscalls.
3828 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003829 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003830
Willy Tarreau7f876a12015-11-18 11:59:55 +01003831 /* In any case we've finished parsing the request so we must
3832 * disable Nagle when sending data because 1) we're not going
3833 * to shut this side, and 2) the server is waiting for us to
3834 * send pending data.
3835 */
3836 chn->flags |= CF_NEVER_WAIT;
3837
Willy Tarreau610ecce2010-01-04 21:15:02 +01003838 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3839 goto wait_other_side;
3840
3841 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3842 /* The server has not finished to respond, so we
3843 * don't want to move in order not to upset it.
3844 */
3845 goto wait_other_side;
3846 }
3847
Willy Tarreau610ecce2010-01-04 21:15:02 +01003848 /* When we get here, it means that both the request and the
3849 * response have finished receiving. Depending on the connection
3850 * mode, we'll have to wait for the last bytes to leave in either
3851 * direction, and sometimes for a close to be effective.
3852 */
3853
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003854 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3855 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003856 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3857 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003858 }
3859 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3860 /* Option forceclose is set, or either side wants to close,
3861 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003862 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003863 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003864 *
3865 * However, there is an exception if the response
3866 * length is undefined. In this case, we need to wait
3867 * the close from the server. The response will be
3868 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003869 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003870 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3871 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3872 goto check_channel_flags;
3873
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003874 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3875 channel_shutr_now(chn);
3876 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003877 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003878 }
3879 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003880 /* The last possible modes are keep-alive and tunnel. Tunnel mode
3881 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003882 */
Willy Tarreau4213a112013-12-15 10:25:42 +01003883 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
3884 channel_auto_read(chn);
3885 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003886 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003887 }
3888
Christopher Faulet4be98032017-07-18 10:48:24 +02003889 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003890 }
3891
3892 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3893 http_msg_closing:
3894 /* nothing else to forward, just waiting for the output buffer
3895 * to be empty and for the shutw_now to take effect.
3896 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003897 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003898 txn->req.msg_state = HTTP_MSG_CLOSED;
3899 goto http_msg_closed;
3900 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003901 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003902 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003903 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003904 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003905 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003906 }
3907
3908 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3909 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01003910 /* if we don't know whether the server will close, we need to hard close */
3911 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
3912 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3913
Willy Tarreau3988d932013-12-27 23:03:08 +01003914 /* see above in MSG_DONE why we only do this in these states */
3915 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3916 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003917 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3918 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01003919 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003920 goto wait_other_side;
3921 }
3922
Christopher Faulet4be98032017-07-18 10:48:24 +02003923 check_channel_flags:
3924 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
3925 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
3926 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02003927 txn->req.msg_state = HTTP_MSG_CLOSING;
3928 goto http_msg_closing;
3929 }
3930
3931
Willy Tarreau610ecce2010-01-04 21:15:02 +01003932 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003933 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003934}
3935
3936
3937/* This function updates the response state machine according to the request
3938 * state machine and buffer flags. It returns 1 if it changes anything (flag
3939 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3940 * it is only used to find when a request/response couple is complete. Both
3941 * this function and its equivalent should loop until both return zero. It
3942 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3943 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003944int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003945{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003946 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003947 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003948 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003949 unsigned int old_state = txn->rsp.msg_state;
3950
Christopher Faulet4be98032017-07-18 10:48:24 +02003951 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003952 return 0;
3953
3954 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3955 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003956 * still monitor the server connection for a possible close
3957 * while the request is being uploaded, so we don't disable
3958 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003959 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003960 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003961
3962 if (txn->req.msg_state == HTTP_MSG_ERROR)
3963 goto wait_other_side;
3964
3965 if (txn->req.msg_state < HTTP_MSG_DONE) {
3966 /* The client seems to still be sending data, probably
3967 * because we got an error response during an upload.
3968 * We have the choice of either breaking the connection
3969 * or letting it pass through. Let's do the later.
3970 */
3971 goto wait_other_side;
3972 }
3973
Willy Tarreau610ecce2010-01-04 21:15:02 +01003974 /* When we get here, it means that both the request and the
3975 * response have finished receiving. Depending on the connection
3976 * mode, we'll have to wait for the last bytes to leave in either
3977 * direction, and sometimes for a close to be effective.
3978 */
3979
3980 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3981 /* Server-close mode : shut read and wait for the request
3982 * side to close its output buffer. The caller will detect
3983 * when we're in DONE and the other is in CLOSED and will
3984 * catch that for the final cleanup.
3985 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003986 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
3987 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003988 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003989 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3990 /* Option forceclose is set, or either side wants to close,
3991 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003992 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003993 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003994 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01003995 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003996 channel_shutr_now(chn);
3997 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003998 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003999 }
4000 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004001 /* The last possible modes are keep-alive and tunnel. Tunnel will
4002 * need to forward remaining data. Keep-alive will need to monitor
4003 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004004 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004005 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004006 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004007 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4008 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004009 }
4010
Christopher Faulet4be98032017-07-18 10:48:24 +02004011 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004012 }
4013
4014 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4015 http_msg_closing:
4016 /* nothing else to forward, just waiting for the output buffer
4017 * to be empty and for the shutw_now to take effect.
4018 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004019 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004020 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4021 goto http_msg_closed;
4022 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004023 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004024 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004025 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004026 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004027 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004028 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004029 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004030 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004031 }
4032
4033 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4034 http_msg_closed:
4035 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004036 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004037 channel_auto_close(chn);
4038 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004039 goto wait_other_side;
4040 }
4041
Christopher Faulet4be98032017-07-18 10:48:24 +02004042 check_channel_flags:
4043 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4044 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4045 /* if we've just closed an output, let's switch */
4046 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4047 goto http_msg_closing;
4048 }
4049
Willy Tarreau610ecce2010-01-04 21:15:02 +01004050 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004051 /* We force the response to leave immediately if we're waiting for the
4052 * other side, since there is no pending shutdown to push it out.
4053 */
4054 if (!channel_is_empty(chn))
4055 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004056 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004057}
4058
4059
Christopher Faulet894da4c2017-07-18 11:29:07 +02004060/* Resync the request and response state machines. */
4061void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004062{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004063 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004064#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004065 int old_req_state = txn->req.msg_state;
4066 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004067#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004068
Willy Tarreau610ecce2010-01-04 21:15:02 +01004069 http_sync_req_state(s);
4070 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004071 if (!http_sync_res_state(s))
4072 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004073 if (!http_sync_req_state(s))
4074 break;
4075 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004076
Christopher Faulet894da4c2017-07-18 11:29:07 +02004077 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4078 "req->analysers=0x%08x res->analysers=0x%08x\n",
4079 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004080 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4081 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004082 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004083
4084
Willy Tarreau610ecce2010-01-04 21:15:02 +01004085 /* OK, both state machines agree on a compatible state.
4086 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004087 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4088 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004089 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4090 * means we must abort the request.
4091 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4092 * corresponding channel.
4093 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4094 * on the response with server-close mode means we've completed one
4095 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004096 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004097 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4098 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004099 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004100 channel_auto_close(&s->req);
4101 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004102 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004103 channel_auto_close(&s->res);
4104 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004105 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004106 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4107 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004108 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004109 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004110 channel_auto_close(&s->res);
4111 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004112 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004113 channel_abort(&s->req);
4114 channel_auto_close(&s->req);
4115 channel_auto_read(&s->req);
4116 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004117 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004118 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4119 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4120 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4121 s->req.analysers &= AN_REQ_FLT_END;
4122 if (HAS_REQ_DATA_FILTERS(s))
4123 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4124 }
4125 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4126 s->res.analysers &= AN_RES_FLT_END;
4127 if (HAS_RSP_DATA_FILTERS(s))
4128 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4129 }
4130 channel_auto_close(&s->req);
4131 channel_auto_read(&s->req);
4132 channel_auto_close(&s->res);
4133 channel_auto_read(&s->res);
4134 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004135 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4136 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004137 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004138 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4139 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4140 /* server-close/keep-alive: terminate this transaction,
4141 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004142 * a fresh-new transaction, but only once we're sure there's
4143 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004144 * another request. They must not hold any pending output data
4145 * and the response buffer must realigned
4146 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004147 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004148 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004149 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004150 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004151 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004152 else {
4153 s->req.analysers = AN_REQ_FLT_END;
4154 s->res.analysers = AN_RES_FLT_END;
4155 txn->flags |= TX_WAIT_CLEANUP;
4156 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004157 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004158}
4159
Willy Tarreaud98cf932009-12-27 22:54:55 +01004160/* This function is an analyser which forwards request body (including chunk
4161 * sizes if any). It is called as soon as we must forward, even if we forward
4162 * zero byte. The only situation where it must not be called is when we're in
4163 * tunnel mode and we want to forward till the close. It's used both to forward
4164 * remaining data and to resync after end of body. It expects the msg_state to
4165 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004166 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004167 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004168 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004169 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004170int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004171{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004172 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004173 struct http_txn *txn = s->txn;
4174 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004175 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004176
Christopher Faulet45073512018-07-20 10:16:29 +02004177 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 +02004178 now_ms, __FUNCTION__,
4179 s,
4180 req,
4181 req->rex, req->wex,
4182 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02004183 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02004184 req->analysers);
4185
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004186 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4187 return 0;
4188
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004189 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004190 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004191 /* Output closed while we were sending data. We must abort and
4192 * wake the other side up.
4193 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004194 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004195 msg->msg_state = HTTP_MSG_ERROR;
4196 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004197 return 1;
4198 }
4199
Willy Tarreaud98cf932009-12-27 22:54:55 +01004200 /* Note that we don't have to send 100-continue back because we don't
4201 * need the data to complete our job, and it's up to the server to
4202 * decide whether to return 100, 417 or anything else in return of
4203 * an "Expect: 100-continue" header.
4204 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004205 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004206 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4207 ? HTTP_MSG_CHUNK_SIZE
4208 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004209
4210 /* TODO/filters: when http-buffer-request option is set or if a
4211 * rule on url_param exists, the first chunk size could be
4212 * already parsed. In that case, msg->next is after the chunk
4213 * size (including the CRLF after the size). So this case should
4214 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004215 }
4216
Willy Tarreau7ba23542014-04-17 21:50:00 +02004217 /* Some post-connect processing might want us to refrain from starting to
4218 * forward data. Currently, the only reason for this is "balance url_param"
4219 * whichs need to parse/process the request after we've enabled forwarding.
4220 */
4221 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004222 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004223 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004224 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004225 channel_dont_close(req); /* don't fail on early shutr */
4226 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004227 }
4228 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4229 }
4230
Willy Tarreau80a92c02014-03-12 10:41:13 +01004231 /* in most states, we should abort in case of early close */
4232 channel_auto_close(req);
4233
Willy Tarreauefdf0942014-04-24 20:08:57 +02004234 if (req->to_forward) {
4235 /* We can't process the buffer's contents yet */
4236 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004237 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004238 }
4239
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004240 if (msg->msg_state < HTTP_MSG_DONE) {
4241 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4242 ? http_msg_forward_chunked_body(s, msg)
4243 : http_msg_forward_body(s, msg));
4244 if (!ret)
4245 goto missing_data_or_waiting;
4246 if (ret < 0)
4247 goto return_bad_req;
4248 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004249
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004250 /* other states, DONE...TUNNEL */
4251 /* we don't want to forward closes on DONE except in tunnel mode. */
4252 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4253 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004254
Christopher Faulet894da4c2017-07-18 11:29:07 +02004255 http_resync_states(s);
4256 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004257 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4258 if (req->flags & CF_SHUTW) {
4259 /* request errors are most likely due to the
4260 * server aborting the transfer. */
4261 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004262 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004263 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004264 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004265 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004266 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004267 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004268 }
4269
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004270 /* If "option abortonclose" is set on the backend, we want to monitor
4271 * the client's connection and forward any shutdown notification to the
4272 * server, which will decide whether to close or to go on processing the
4273 * request. We only do that in tunnel mode, and not in other modes since
4274 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004275 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004276 channel_auto_read(req);
4277 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4278 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4279 s->si[1].flags |= SI_FL_NOLINGER;
4280 channel_auto_close(req);
4281 }
4282 else if (s->txn->meth == HTTP_METH_POST) {
4283 /* POST requests may require to read extra CRLF sent by broken
4284 * browsers and which could cause an RST to be sent upon close
4285 * on some systems (eg: Linux). */
4286 channel_auto_read(req);
4287 }
4288 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004289
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004290 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004291 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004292 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004293 if (!(s->flags & SF_ERR_MASK))
4294 s->flags |= SF_ERR_CLICL;
4295 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004296 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004297 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004298 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004299 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004300 }
4301
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004302 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4303 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004304 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004305 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004306
4307 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004308 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004309
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004310 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004311 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004312 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004313 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004314
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004315 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004316 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004317 * And when content-length is used, we never want to let the possible
4318 * shutdown be forwarded to the other side, as the state machine will
4319 * take care of it once the client responds. It's also important to
4320 * prevent TIME_WAITs from accumulating on the backend side, and for
4321 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004322 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004323 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004324 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004325
Willy Tarreau5c620922011-05-11 19:56:11 +02004326 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004327 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004328 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004329 * modes are already handled by the stream sock layer. We must not do
4330 * this in content-length mode because it could present the MSG_MORE
4331 * flag with the last block of forwarded data, which would cause an
4332 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004333 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004334 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004335 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004336
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004337 return 0;
4338
Willy Tarreaud98cf932009-12-27 22:54:55 +01004339 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004340 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004341 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004342 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004343
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004344 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004345 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004346 txn->req.msg_state = HTTP_MSG_ERROR;
4347 if (txn->status) {
4348 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004349 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004350 } else {
4351 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004352 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004353 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004354 req->analysers &= AN_REQ_FLT_END;
4355 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 +01004356
Willy Tarreaue7dff022015-04-03 01:14:29 +02004357 if (!(s->flags & SF_ERR_MASK))
4358 s->flags |= SF_ERR_PRXCOND;
4359 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004360 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004361 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004362 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004363 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004364 }
4365 return 0;
4366
4367 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004368 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004369 txn->req.msg_state = HTTP_MSG_ERROR;
4370 if (txn->status) {
4371 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004372 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004373 } else {
4374 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004375 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004376 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004377 req->analysers &= AN_REQ_FLT_END;
4378 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 +01004379
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004380 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4381 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004382 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004383 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004384
Willy Tarreaue7dff022015-04-03 01:14:29 +02004385 if (!(s->flags & SF_ERR_MASK))
4386 s->flags |= SF_ERR_SRVCL;
4387 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004388 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004389 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004390 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004391 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004392 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004393 return 0;
4394}
4395
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004396/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4397 * processing can continue on next analysers, or zero if it either needs more
4398 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004399 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004400 * when it has nothing left to do, and may remove any analyser when it wants to
4401 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004402 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004403int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004404{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004405 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004406 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004407 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004408 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004409 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004410 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004411 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004412
Christopher Faulet45073512018-07-20 10:16:29 +02004413 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 +02004414 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004415 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004416 rep,
4417 rep->rex, rep->wex,
4418 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004419 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004420 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004421
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004422 /*
4423 * Now parse the partial (or complete) lines.
4424 * We will check the response syntax, and also join multi-line
4425 * headers. An index of all the lines will be elaborated while
4426 * parsing.
4427 *
4428 * For the parsing, we use a 28 states FSM.
4429 *
4430 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004431 * ci_head(rep) = beginning of response
4432 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4433 * ci_tail(rep) = end of input data
4434 * msg->eol = end of current header or line (LF or CRLF)
4435 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004436 */
4437
Willy Tarreau628c40c2014-04-24 19:11:26 +02004438 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004439 /* There's a protected area at the end of the buffer for rewriting
4440 * purposes. We don't want to start to parse the request if the
4441 * protected area is affected, because we may have to move processed
4442 * data later, which is much more complicated.
4443 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004444 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004445 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004446 /* some data has still not left the buffer, wake us once that's done */
4447 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4448 goto abort_response;
4449 channel_dont_close(rep);
4450 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004451 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004452 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004453 }
4454
Willy Tarreau188e2302018-06-15 11:11:53 +02004455 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004456 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004457 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004458
Willy Tarreauf37954d2018-06-15 18:31:02 +02004459 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004460 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004461 }
4462
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004463 /* 1: we might have to print this header in debug mode */
4464 if (unlikely((global.mode & MODE_DEBUG) &&
4465 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004466 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004467 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004468
Willy Tarreauf37954d2018-06-15 18:31:02 +02004469 sol = ci_head(rep);
4470 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004471 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004472
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004473 sol += hdr_idx_first_pos(&txn->hdr_idx);
4474 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004475
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004476 while (cur_idx) {
4477 eol = sol + txn->hdr_idx.v[cur_idx].len;
4478 debug_hdr("srvhdr", s, sol, eol);
4479 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4480 cur_idx = txn->hdr_idx.v[cur_idx].next;
4481 }
4482 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004483
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004484 /*
4485 * Now we quickly check if we have found a full valid response.
4486 * If not so, we check the FD and buffer states before leaving.
4487 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004488 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004489 * responses are checked first.
4490 *
4491 * Depending on whether the client is still there or not, we
4492 * may send an error response back or not. Note that normally
4493 * we should only check for HTTP status there, and check I/O
4494 * errors somewhere else.
4495 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004496
Willy Tarreau655dce92009-11-08 13:10:58 +01004497 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004498 /* Invalid response */
4499 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4500 /* we detected a parsing error. We want to archive this response
4501 * in the dedicated proxy area for later troubleshooting.
4502 */
4503 hdr_response_bad:
4504 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004505 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004506
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004507 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004508 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004509 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004510 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004511 }
Willy Tarreau64648412010-03-05 10:41:54 +01004512 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004513 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004514 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004515 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004516 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004517 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004518 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004519
Willy Tarreaue7dff022015-04-03 01:14:29 +02004520 if (!(s->flags & SF_ERR_MASK))
4521 s->flags |= SF_ERR_PRXCOND;
4522 if (!(s->flags & SF_FINST_MASK))
4523 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004524
4525 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004526 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004527
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004528 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004529 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004530 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004531 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004532 goto hdr_response_bad;
4533 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004534
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004535 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004536 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004537 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004538 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004539 else if (txn->flags & TX_NOT_FIRST)
4540 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004541
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004542 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004543 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004544 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004545 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004546 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004547
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004548 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004549 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004550 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004551
4552 /* Check to see if the server refused the early data.
4553 * If so, just send a 425
4554 */
4555 if (objt_cs(s->si[1].end)) {
4556 struct connection *conn = objt_cs(s->si[1].end)->conn;
4557
4558 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4559 txn->status = 425;
4560 }
4561
Willy Tarreau350f4872014-11-28 14:42:25 +01004562 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004563 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004564 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004565
Willy Tarreaue7dff022015-04-03 01:14:29 +02004566 if (!(s->flags & SF_ERR_MASK))
4567 s->flags |= SF_ERR_SRVCL;
4568 if (!(s->flags & SF_FINST_MASK))
4569 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004570 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004571 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004572
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004573 /* read timeout : return a 504 to the client. */
4574 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004575 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004576 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004577
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004578 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004579 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004580 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004581 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004582 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004583
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004584 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004585 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004586 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004587 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004588 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004589 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004590
Willy Tarreaue7dff022015-04-03 01:14:29 +02004591 if (!(s->flags & SF_ERR_MASK))
4592 s->flags |= SF_ERR_SRVTO;
4593 if (!(s->flags & SF_FINST_MASK))
4594 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004595 return 0;
4596 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004597
Willy Tarreauf003d372012-11-26 13:35:37 +01004598 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004599 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004600 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4601 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004602 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004603 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004604
Christopher Faulet0184ea72017-01-05 14:06:34 +01004605 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004606 channel_auto_close(rep);
4607
4608 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004609 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004610 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004611
Willy Tarreaue7dff022015-04-03 01:14:29 +02004612 if (!(s->flags & SF_ERR_MASK))
4613 s->flags |= SF_ERR_CLICL;
4614 if (!(s->flags & SF_FINST_MASK))
4615 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004616
Willy Tarreau87b09662015-04-03 00:22:06 +02004617 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004618 return 0;
4619 }
4620
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004621 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004622 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004623 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004624 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004625 else if (txn->flags & TX_NOT_FIRST)
4626 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004627
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004628 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004629 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02004630 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004631 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004632 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004633
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004634 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004635 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004636 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004637 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004638 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004639 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004640
Willy Tarreaue7dff022015-04-03 01:14:29 +02004641 if (!(s->flags & SF_ERR_MASK))
4642 s->flags |= SF_ERR_SRVCL;
4643 if (!(s->flags & SF_FINST_MASK))
4644 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004645 return 0;
4646 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004647
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004648 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004649 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004650 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004651 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004652 else if (txn->flags & TX_NOT_FIRST)
4653 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004654
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004655 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004656 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004657 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004658
Willy Tarreaue7dff022015-04-03 01:14:29 +02004659 if (!(s->flags & SF_ERR_MASK))
4660 s->flags |= SF_ERR_CLICL;
4661 if (!(s->flags & SF_FINST_MASK))
4662 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004663
Willy Tarreau87b09662015-04-03 00:22:06 +02004664 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004665 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004666 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004667
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004668 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004669 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004670 return 0;
4671 }
4672
4673 /* More interesting part now : we know that we have a complete
4674 * response which at least looks like HTTP. We have an indicator
4675 * of each header's length, so we can parse them quickly.
4676 */
4677
4678 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004679 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004680
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004681 /*
4682 * 1: get the status code
4683 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004684 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004685 if (n < 1 || n > 5)
4686 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004687 /* when the client triggers a 4xx from the server, it's most often due
4688 * to a missing object or permission. These events should be tracked
4689 * because if they happen often, it may indicate a brute force or a
4690 * vulnerability scan.
4691 */
4692 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004693 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004694
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004695 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004696 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004697
Willy Tarreau91852eb2015-05-01 13:26:00 +02004698 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4699 * exactly one digit "." one digit. This check may be disabled using
4700 * option accept-invalid-http-response.
4701 */
4702 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4703 if (msg->sl.st.v_l != 8) {
4704 msg->err_pos = 0;
4705 goto hdr_response_bad;
4706 }
4707
Willy Tarreauf37954d2018-06-15 18:31:02 +02004708 if (ci_head(rep)[4] != '/' ||
4709 !isdigit((unsigned char)ci_head(rep)[5]) ||
4710 ci_head(rep)[6] != '.' ||
4711 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004712 msg->err_pos = 4;
4713 goto hdr_response_bad;
4714 }
4715 }
4716
Willy Tarreau5b154472009-12-21 20:11:07 +01004717 /* check if the response is HTTP/1.1 or above */
4718 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004719 ((ci_head(rep)[5] > '1') ||
4720 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004721 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004722
4723 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004724 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 +01004725
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004726 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004727 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004728
Willy Tarreauf37954d2018-06-15 18:31:02 +02004729 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004730
Willy Tarreau39650402010-03-15 19:44:39 +01004731 /* Adjust server's health based on status code. Note: status codes 501
4732 * and 505 are triggered on demand by client request, so we must not
4733 * count them as server failures.
4734 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004735 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004736 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004737 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004738 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004739 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004740 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004741
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004742 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004743 * We may be facing a 100-continue response, or any other informational
4744 * 1xx response which is non-final, in which case this is not the right
4745 * response, and we're waiting for the next one. Let's allow this response
4746 * to go to the client and wait for the next one. There's an exception for
4747 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004748 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004749 if (txn->status < 200 &&
4750 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004751 hdr_idx_init(&txn->hdr_idx);
4752 msg->next -= channel_forward(rep, msg->next);
4753 msg->msg_state = HTTP_MSG_RPBEFORE;
4754 txn->status = 0;
4755 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004756 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004757 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004758 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004759
Willy Tarreaua14ad722017-07-07 11:36:32 +02004760 /*
4761 * 2: check for cacheability.
4762 */
4763
4764 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004765 case 200:
4766 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004767 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004768 case 206:
4769 case 300:
4770 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004771 case 404:
4772 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004773 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004774 case 414:
4775 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004776 break;
4777 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004778 /* RFC7231#6.1:
4779 * Responses with status codes that are defined as
4780 * cacheable by default (e.g., 200, 203, 204, 206,
4781 * 300, 301, 404, 405, 410, 414, and 501 in this
4782 * specification) can be reused by a cache with
4783 * heuristic expiration unless otherwise indicated
4784 * by the method definition or explicit cache
4785 * controls [RFC7234]; all other status codes are
4786 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004787 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004788 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004789 break;
4790 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004791
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004792 /*
4793 * 3: we may need to capture headers
4794 */
4795 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004796 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004797 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4798 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004799
Willy Tarreau557f1992015-05-01 10:05:17 +02004800 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4801 * by RFC7230#3.3.3 :
4802 *
4803 * The length of a message body is determined by one of the following
4804 * (in order of precedence):
4805 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004806 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4807 * the connection will become a tunnel immediately after the empty
4808 * line that concludes the header fields. A client MUST ignore
4809 * any Content-Length or Transfer-Encoding header fields received
4810 * in such a message. Any 101 response (Switching Protocols) is
4811 * managed in the same manner.
4812 *
4813 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004814 * (Informational), 204 (No Content), or 304 (Not Modified) status
4815 * code is always terminated by the first empty line after the
4816 * header fields, regardless of the header fields present in the
4817 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004818 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004819 * 3. If a Transfer-Encoding header field is present and the chunked
4820 * transfer coding (Section 4.1) is the final encoding, the message
4821 * body length is determined by reading and decoding the chunked
4822 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004823 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004824 * If a Transfer-Encoding header field is present in a response and
4825 * the chunked transfer coding is not the final encoding, the
4826 * message body length is determined by reading the connection until
4827 * it is closed by the server. If a Transfer-Encoding header field
4828 * is present in a request and the chunked transfer coding is not
4829 * the final encoding, the message body length cannot be determined
4830 * reliably; the server MUST respond with the 400 (Bad Request)
4831 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004832 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004833 * If a message is received with both a Transfer-Encoding and a
4834 * Content-Length header field, the Transfer-Encoding overrides the
4835 * Content-Length. Such a message might indicate an attempt to
4836 * perform request smuggling (Section 9.5) or response splitting
4837 * (Section 9.4) and ought to be handled as an error. A sender MUST
4838 * remove the received Content-Length field prior to forwarding such
4839 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004840 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004841 * 4. If a message is received without Transfer-Encoding and with
4842 * either multiple Content-Length header fields having differing
4843 * field-values or a single Content-Length header field having an
4844 * invalid value, then the message framing is invalid and the
4845 * recipient MUST treat it as an unrecoverable error. If this is a
4846 * request message, the server MUST respond with a 400 (Bad Request)
4847 * status code and then close the connection. If this is a response
4848 * message received by a proxy, the proxy MUST close the connection
4849 * to the server, discard the received response, and send a 502 (Bad
4850 * Gateway) response to the client. If this is a response message
4851 * received by a user agent, the user agent MUST close the
4852 * connection to the server and discard the received response.
4853 *
4854 * 5. If a valid Content-Length header field is present without
4855 * Transfer-Encoding, its decimal value defines the expected message
4856 * body length in octets. If the sender closes the connection or
4857 * the recipient times out before the indicated number of octets are
4858 * received, the recipient MUST consider the message to be
4859 * incomplete and close the connection.
4860 *
4861 * 6. If this is a request message and none of the above are true, then
4862 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004863 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004864 * 7. Otherwise, this is a response message without a declared message
4865 * body length, so the message body length is determined by the
4866 * number of octets received prior to the server closing the
4867 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004868 */
4869
4870 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004871 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004872 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004873 * FIXME: should we parse anyway and return an error on chunked encoding ?
4874 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004875 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4876 txn->status == 101)) {
4877 /* Either we've established an explicit tunnel, or we're
4878 * switching the protocol. In both cases, we're very unlikely
4879 * to understand the next protocols. We have to switch to tunnel
4880 * mode, so that we transfer the request and responses then let
4881 * this protocol pass unmodified. When we later implement specific
4882 * parsers for such protocols, we'll want to check the Upgrade
4883 * header which contains information about that protocol for
4884 * responses with status 101 (eg: see RFC2817 about TLS).
4885 */
4886 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4887 msg->flags |= HTTP_MSGF_XFER_LEN;
4888 goto end;
4889 }
4890
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004891 if (txn->meth == HTTP_METH_HEAD ||
4892 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004893 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004894 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004895 goto skip_content_length;
4896 }
4897
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004898 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004899 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004900 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004901 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004902 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4903 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004904 /* bad transfer-encoding (chunked followed by something else) */
4905 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004906 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004907 break;
4908 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004909 }
4910
Willy Tarreau1c913912015-04-30 10:57:51 +02004911 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004912 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02004913 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004914 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02004915 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4916 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02004917 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004918 signed long long cl;
4919
Willy Tarreauad14f752011-09-02 20:33:27 +02004920 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004921 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004922 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004923 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004924
Willy Tarreauad14f752011-09-02 20:33:27 +02004925 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004926 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004927 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004928 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004929
Willy Tarreauad14f752011-09-02 20:33:27 +02004930 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004931 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004932 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004933 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004934
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004935 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004936 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004937 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004938 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004939
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004940 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004941 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004942 }
4943
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004944 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01004945 /* Now we have to check if we need to modify the Connection header.
4946 * This is more difficult on the response than it is on the request,
4947 * because we can have two different HTTP versions and we don't know
4948 * how the client will interprete a response. For instance, let's say
4949 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4950 * HTTP/1.1 response without any header. Maybe it will bound itself to
4951 * HTTP/1.0 because it only knows about it, and will consider the lack
4952 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4953 * the lack of header as a keep-alive. Thus we will use two flags
4954 * indicating how a request MAY be understood by the client. In case
4955 * of multiple possibilities, we'll fix the header to be explicit. If
4956 * ambiguous cases such as both close and keepalive are seen, then we
4957 * will fall back to explicit close. Note that we won't take risks with
4958 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004959 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004960 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004961 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4962 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4963 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4964 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004965 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004966
Willy Tarreau70dffda2014-01-30 03:07:23 +01004967 /* this situation happens when combining pretend-keepalive with httpclose. */
4968 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004969 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02004970 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01004971 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
4972
Willy Tarreau60466522010-01-18 19:08:45 +01004973 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004974 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01004975 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4976 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004977
Willy Tarreau60466522010-01-18 19:08:45 +01004978 /* now adjust header transformations depending on current state */
4979 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4980 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4981 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004982 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004983 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004984 }
Willy Tarreau60466522010-01-18 19:08:45 +01004985 else { /* SCL / KAL */
4986 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004987 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004988 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004989 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004990
Willy Tarreau60466522010-01-18 19:08:45 +01004991 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004992 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004993
Willy Tarreau60466522010-01-18 19:08:45 +01004994 /* Some keep-alive responses are converted to Server-close if
4995 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004996 */
Willy Tarreau60466522010-01-18 19:08:45 +01004997 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4998 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004999 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005000 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005001 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005002 }
5003
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005004 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005005 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005006 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005007
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005008 /* end of job, return OK */
5009 rep->analysers &= ~an_bit;
5010 rep->analyse_exp = TICK_ETERNITY;
5011 channel_auto_close(rep);
5012 return 1;
5013
5014 abort_keep_alive:
5015 /* A keep-alive request to the server failed on a network error.
5016 * The client is required to retry. We need to close without returning
5017 * any other information so that the client retries.
5018 */
5019 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005020 rep->analysers &= AN_RES_FLT_END;
5021 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005022 channel_auto_close(rep);
5023 s->logs.logwait = 0;
5024 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005025 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005026 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005027 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005028 return 0;
5029}
5030
5031/* This function performs all the processing enabled for the current response.
5032 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005033 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005034 * other functions. It works like process_request (see indications above).
5035 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005036int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005037{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005038 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005039 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005040 struct http_msg *msg = &txn->rsp;
5041 struct proxy *cur_proxy;
5042 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005043 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005044
Christopher Faulet45073512018-07-20 10:16:29 +02005045 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 +02005046 now_ms, __FUNCTION__,
5047 s,
5048 rep,
5049 rep->rex, rep->wex,
5050 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02005051 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005052 rep->analysers);
5053
5054 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5055 return 0;
5056
Willy Tarreau70730dd2014-04-24 18:06:27 +02005057 /* The stats applet needs to adjust the Connection header but we don't
5058 * apply any filter there.
5059 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005060 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5061 rep->analysers &= ~an_bit;
5062 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005063 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005064 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005065
Willy Tarreau58975672014-04-24 21:13:57 +02005066 /*
5067 * We will have to evaluate the filters.
5068 * As opposed to version 1.2, now they will be evaluated in the
5069 * filters order and not in the header order. This means that
5070 * each filter has to be validated among all headers.
5071 *
5072 * Filters are tried with ->be first, then with ->fe if it is
5073 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005074 *
5075 * Maybe we are in resume condiion. In this case I choose the
5076 * "struct proxy" which contains the rule list matching the resume
5077 * pointer. If none of theses "struct proxy" match, I initialise
5078 * the process with the first one.
5079 *
5080 * In fact, I check only correspondance betwwen the current list
5081 * pointer and the ->fe rule list. If it doesn't match, I initialize
5082 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005083 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005084 if (s->current_rule_list == &sess->fe->http_res_rules)
5085 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005086 else
5087 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005088 while (1) {
5089 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005090
Willy Tarreau58975672014-04-24 21:13:57 +02005091 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005092 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005093 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005094
Willy Tarreau51d861a2015-05-22 17:30:48 +02005095 if (ret == HTTP_RULE_RES_BADREQ)
5096 goto return_srv_prx_502;
5097
5098 if (ret == HTTP_RULE_RES_DONE) {
5099 rep->analysers &= ~an_bit;
5100 rep->analyse_exp = TICK_ETERNITY;
5101 return 1;
5102 }
5103 }
5104
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005105 /* we need to be called again. */
5106 if (ret == HTTP_RULE_RES_YIELD) {
5107 channel_dont_close(rep);
5108 return 0;
5109 }
5110
Willy Tarreau58975672014-04-24 21:13:57 +02005111 /* try headers filters */
5112 if (rule_set->rsp_exp != NULL) {
5113 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5114 return_bad_resp:
5115 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005116 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005117 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005118 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005119 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005120 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005121 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005122 txn->status = 502;
5123 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005124 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005125 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005126 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005127 if (!(s->flags & SF_ERR_MASK))
5128 s->flags |= SF_ERR_PRXCOND;
5129 if (!(s->flags & SF_FINST_MASK))
5130 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005131 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005132 }
Willy Tarreau58975672014-04-24 21:13:57 +02005133 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005134
Willy Tarreau58975672014-04-24 21:13:57 +02005135 /* has the response been denied ? */
5136 if (txn->flags & TX_SVDENY) {
5137 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005138 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005139
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005140 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5141 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005142 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005143 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005144
Willy Tarreau58975672014-04-24 21:13:57 +02005145 goto return_srv_prx_502;
5146 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005147
Willy Tarreau58975672014-04-24 21:13:57 +02005148 /* add response headers from the rule sets in the same order */
5149 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005150 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005151 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005152 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005153 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005154 ret = acl_pass(ret);
5155 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5156 ret = !ret;
5157 if (!ret)
5158 continue;
5159 }
Christopher Faulet10079f52018-10-03 15:17:28 +02005160 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005161 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005162 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005163
Willy Tarreau58975672014-04-24 21:13:57 +02005164 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005165 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005166 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005167 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005168 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005169
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005170 /* After this point, this anayzer can't return yield, so we can
5171 * remove the bit corresponding to this analyzer from the list.
5172 *
5173 * Note that the intermediate returns and goto found previously
5174 * reset the analyzers.
5175 */
5176 rep->analysers &= ~an_bit;
5177 rep->analyse_exp = TICK_ETERNITY;
5178
Willy Tarreau58975672014-04-24 21:13:57 +02005179 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005180 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005181 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005182
Willy Tarreau58975672014-04-24 21:13:57 +02005183 /*
5184 * Now check for a server cookie.
5185 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005186 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005187 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005188
Willy Tarreau58975672014-04-24 21:13:57 +02005189 /*
5190 * Check for cache-control or pragma headers if required.
5191 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005192 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005193 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005194
Willy Tarreau58975672014-04-24 21:13:57 +02005195 /*
5196 * Add server cookie in the response if needed
5197 */
5198 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5199 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005200 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005201 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5202 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5203 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5204 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5205 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005206 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005207 /* the server is known, it's not the one the client requested, or the
5208 * cookie's last seen date needs to be refreshed. We have to
5209 * insert a set-cookie here, except if we want to insert only on POST
5210 * requests and this one isn't. Note that servers which don't have cookies
5211 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005212 */
Willy Tarreau58975672014-04-24 21:13:57 +02005213 if (!objt_server(s->target)->cookie) {
5214 chunk_printf(&trash,
5215 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5216 s->be->cookie_name);
5217 }
5218 else {
5219 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005220
Willy Tarreau58975672014-04-24 21:13:57 +02005221 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5222 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005223 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5224 s30tob64((date.tv_sec+3) >> 2,
5225 trash.area + trash.data);
5226 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005227
Willy Tarreau58975672014-04-24 21:13:57 +02005228 if (s->be->cookie_maxlife) {
5229 /* emit first_date, which is either the original one or
5230 * the current date.
5231 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005232 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005233 s30tob64(txn->cookie_first_date ?
5234 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005235 (date.tv_sec+3) >> 2,
5236 trash.area + trash.data);
5237 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005238 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005239 }
Willy Tarreau58975672014-04-24 21:13:57 +02005240 chunk_appendf(&trash, "; path=/");
5241 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005242
Willy Tarreau58975672014-04-24 21:13:57 +02005243 if (s->be->cookie_domain)
5244 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005245
Willy Tarreau58975672014-04-24 21:13:57 +02005246 if (s->be->ck_opts & PR_CK_HTTPONLY)
5247 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005248
Willy Tarreau58975672014-04-24 21:13:57 +02005249 if (s->be->ck_opts & PR_CK_SECURE)
5250 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005251
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005252 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005253 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005254
Willy Tarreau58975672014-04-24 21:13:57 +02005255 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005256 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005257 /* the server did not change, only the date was updated */
5258 txn->flags |= TX_SCK_UPDATED;
5259 else
5260 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005261
Willy Tarreau58975672014-04-24 21:13:57 +02005262 /* Here, we will tell an eventual cache on the client side that we don't
5263 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5264 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5265 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005266 */
Willy Tarreau58975672014-04-24 21:13:57 +02005267 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005268
Willy Tarreau58975672014-04-24 21:13:57 +02005269 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005270
Willy Tarreau58975672014-04-24 21:13:57 +02005271 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5272 "Cache-control: private", 22) < 0))
5273 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005274 }
Willy Tarreau58975672014-04-24 21:13:57 +02005275 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005276
Willy Tarreau58975672014-04-24 21:13:57 +02005277 /*
5278 * Check if result will be cacheable with a cookie.
5279 * We'll block the response if security checks have caught
5280 * nasty things such as a cacheable cookie.
5281 */
5282 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5283 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5284 (s->be->options & PR_O_CHK_CACHE)) {
5285 /* we're in presence of a cacheable response containing
5286 * a set-cookie header. We'll block it as requested by
5287 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005288 */
Willy Tarreau58975672014-04-24 21:13:57 +02005289 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005290 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005291
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005292 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5293 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005294 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005295 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005296
Christopher Faulet767a84b2017-11-24 16:50:31 +01005297 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5298 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005299 send_log(s->be, LOG_ALERT,
5300 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5301 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5302 goto return_srv_prx_502;
5303 }
Willy Tarreau03945942009-12-22 16:50:27 +01005304
Willy Tarreau70730dd2014-04-24 18:06:27 +02005305 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005306 /*
5307 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5308 * If an "Upgrade" token is found, the header is left untouched in order
5309 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005310 * if anything but "Upgrade" is present in the Connection header. We don't
5311 * want to touch any 101 response either since it's switching to another
5312 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005313 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005314 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005315 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005316 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005317 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5318 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005319
Willy Tarreau58975672014-04-24 21:13:57 +02005320 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5321 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5322 /* we want a keep-alive response here. Keep-alive header
5323 * required if either side is not 1.1.
5324 */
5325 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5326 want_flags |= TX_CON_KAL_SET;
5327 }
5328 else {
5329 /* we want a close response here. Close header required if
5330 * the server is 1.1, regardless of the client.
5331 */
5332 if (msg->flags & HTTP_MSGF_VER_11)
5333 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005334 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005335
Willy Tarreau58975672014-04-24 21:13:57 +02005336 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5337 http_change_connection_header(txn, msg, want_flags);
5338 }
5339
5340 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005341 /* Always enter in the body analyzer */
5342 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5343 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005344
Willy Tarreau58975672014-04-24 21:13:57 +02005345 /* if the user wants to log as soon as possible, without counting
5346 * bytes from the server, then this is the right moment. We have
5347 * to temporarily assign bytes_out to log what we currently have.
5348 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005349 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005350 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5351 s->logs.bytes_out = txn->rsp.eoh;
5352 s->do_log(s);
5353 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005354 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005355 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005356}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005357
Willy Tarreaud98cf932009-12-27 22:54:55 +01005358/* This function is an analyser which forwards response body (including chunk
5359 * sizes if any). It is called as soon as we must forward, even if we forward
5360 * zero byte. The only situation where it must not be called is when we're in
5361 * tunnel mode and we want to forward till the close. It's used both to forward
5362 * remaining data and to resync after end of body. It expects the msg_state to
5363 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005364 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005365 *
5366 * It is capable of compressing response data both in content-length mode and
5367 * in chunked mode. The state machines follows different flows depending on
5368 * whether content-length and chunked modes are used, since there are no
5369 * trailers in content-length :
5370 *
5371 * chk-mode cl-mode
5372 * ,----- BODY -----.
5373 * / \
5374 * V size > 0 V chk-mode
5375 * .--> SIZE -------------> DATA -------------> CRLF
5376 * | | size == 0 | last byte |
5377 * | v final crlf v inspected |
5378 * | TRAILERS -----------> DONE |
5379 * | |
5380 * `----------------------------------------------'
5381 *
5382 * Compression only happens in the DATA state, and must be flushed in final
5383 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5384 * is performed at once on final states for all bytes parsed, or when leaving
5385 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005386 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005387int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005388{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005389 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005390 struct http_txn *txn = s->txn;
5391 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005392 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005393
Christopher Faulet45073512018-07-20 10:16:29 +02005394 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 +02005395 now_ms, __FUNCTION__,
5396 s,
5397 res,
5398 res->rex, res->wex,
5399 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005400 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005401 res->analysers);
5402
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005403 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5404 return 0;
5405
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005406 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005407 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005408 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005409 /* Output closed while we were sending data. We must abort and
5410 * wake the other side up.
5411 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005412 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005413 msg->msg_state = HTTP_MSG_ERROR;
5414 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005415 return 1;
5416 }
5417
Willy Tarreau4fe41902010-06-07 22:27:41 +02005418 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005419 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005420
Christopher Fauletd7c91962015-04-30 11:48:27 +02005421 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005422 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5423 ? HTTP_MSG_CHUNK_SIZE
5424 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005425 }
5426
Willy Tarreauefdf0942014-04-24 20:08:57 +02005427 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005428 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005429 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005430 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005431 }
5432
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005433 if (msg->msg_state < HTTP_MSG_DONE) {
5434 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5435 ? http_msg_forward_chunked_body(s, msg)
5436 : http_msg_forward_body(s, msg));
5437 if (!ret)
5438 goto missing_data_or_waiting;
5439 if (ret < 0)
5440 goto return_bad_res;
5441 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005442
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005443 /* other states, DONE...TUNNEL */
5444 /* for keep-alive we don't want to forward closes on DONE */
5445 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5446 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5447 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005448
Christopher Faulet894da4c2017-07-18 11:29:07 +02005449 http_resync_states(s);
5450 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005451 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5452 if (res->flags & CF_SHUTW) {
5453 /* response errors are most likely due to the
5454 * client aborting the transfer. */
5455 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005456 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005457 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005458 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005459 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005460 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005461 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005462 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005463 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005464
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005465 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005466 if (res->flags & CF_SHUTW)
5467 goto aborted_xfer;
5468
5469 /* stop waiting for data if the input is closed before the end. If the
5470 * client side was already closed, it means that the client has aborted,
5471 * so we don't want to count this as a server abort. Otherwise it's a
5472 * server abort.
5473 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005474 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005475 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005476 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005477 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005478 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005479 if (!(s->flags & SF_ERR_MASK))
5480 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005481 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005482 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005483 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005484 goto return_bad_res_stats_ok;
5485 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005486 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005487
Willy Tarreau40dba092010-03-04 18:14:51 +01005488 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005489 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005490 goto return_bad_res;
5491
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005492 /* When TE: chunked is used, we need to get there again to parse
5493 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005494 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5495 * or if there are filters registered on the stream, we don't want to
5496 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005497 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005498 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005499 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005500 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5501 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005502 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005503
Willy Tarreau5c620922011-05-11 19:56:11 +02005504 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005505 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005506 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005507 * modes are already handled by the stream sock layer. We must not do
5508 * this in content-length mode because it could present the MSG_MORE
5509 * flag with the last block of forwarded data, which would cause an
5510 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005511 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005512 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005513 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005514
Willy Tarreau87b09662015-04-03 00:22:06 +02005515 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005516 return 0;
5517
Willy Tarreau40dba092010-03-04 18:14:51 +01005518 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005519 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005520 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005521 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005522
5523 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005524 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005525 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005526 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005527 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005528 res->analysers &= AN_RES_FLT_END;
5529 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 +01005530 if (objt_server(s->target))
5531 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005532
Willy Tarreaue7dff022015-04-03 01:14:29 +02005533 if (!(s->flags & SF_ERR_MASK))
5534 s->flags |= SF_ERR_PRXCOND;
5535 if (!(s->flags & SF_FINST_MASK))
5536 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005537 return 0;
5538
5539 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005540 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005541 txn->rsp.msg_state = HTTP_MSG_ERROR;
5542 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005543 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005544 res->analysers &= AN_RES_FLT_END;
5545 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 +01005546
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005547 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5548 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005549 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005550 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005551
Willy Tarreaue7dff022015-04-03 01:14:29 +02005552 if (!(s->flags & SF_ERR_MASK))
5553 s->flags |= SF_ERR_CLICL;
5554 if (!(s->flags & SF_FINST_MASK))
5555 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005556 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005557}
5558
5559
Christopher Faulet10079f52018-10-03 15:17:28 +02005560int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005561{
5562 struct channel *chn = msg->chn;
5563 int ret;
5564
5565 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5566
5567 if (msg->msg_state == HTTP_MSG_ENDING)
5568 goto ending;
5569
5570 /* Neither content-length, nor transfer-encoding was found, so we must
5571 * read the body until the server connection is closed. In that case, we
5572 * eat data as they come. Of course, this happens for response only. */
5573 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005574 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005575 msg->chunk_len += len;
5576 msg->body_len += len;
5577 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005578 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005579 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005580 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005581 msg->next += ret;
5582 msg->chunk_len -= ret;
5583 if (msg->chunk_len) {
5584 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005585 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005586 chn->flags |= CF_WAKE_WRITE;
5587 goto missing_data_or_waiting;
5588 }
5589
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005590 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5591 * always set for a request. */
5592 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5593 /* The server still sending data that should be filtered */
5594 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5595 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005596 msg->msg_state = HTTP_MSG_TUNNEL;
5597 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005598 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005599
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005600 msg->msg_state = HTTP_MSG_ENDING;
5601
5602 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005603 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005604 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005605 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5606 /* default_ret */ msg->next,
5607 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005608 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005609 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005610 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5611 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005612 if (msg->next)
5613 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005614
Christopher Fauletda02e172015-12-04 09:25:05 +01005615 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5616 /* default_ret */ 1,
5617 /* on_error */ goto error,
5618 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005619 if (msg->msg_state == HTTP_MSG_ENDING)
5620 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005621 return 1;
5622
5623 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005624 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005625 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5626 /* default_ret */ msg->next,
5627 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005628 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005629 msg->next -= ret;
5630 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5631 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005632 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005633 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005634 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005635 return 0;
5636 error:
5637 return -1;
5638}
5639
Christopher Faulet10079f52018-10-03 15:17:28 +02005640int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005641{
5642 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005643 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005644 int ret;
5645
5646 /* Here we have the guarantee to be in one of the following state:
5647 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5648 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5649
Christopher Fauletca874b82018-09-20 11:31:01 +02005650 if (msg->msg_state == HTTP_MSG_ENDING)
5651 goto ending;
5652
5653 /* Don't parse chunks if there is no input data */
5654 if (!ci_data(chn))
5655 goto waiting;
5656
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005657 switch_states:
5658 switch (msg->msg_state) {
5659 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005660 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005661 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005662 /* on_error */ goto error);
5663 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005664 msg->chunk_len -= ret;
5665 if (msg->chunk_len) {
5666 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005667 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005668 chn->flags |= CF_WAKE_WRITE;
5669 goto missing_data_or_waiting;
5670 }
5671
5672 /* nothing left to forward for this chunk*/
5673 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5674 /* fall through for HTTP_MSG_CHUNK_CRLF */
5675
5676 case HTTP_MSG_CHUNK_CRLF:
5677 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005678 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005679 if (ret == 0)
5680 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005681 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005682 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005683 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005684 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005685 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005686 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005687 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005688 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5689 /* fall through for HTTP_MSG_CHUNK_SIZE */
5690
5691 case HTTP_MSG_CHUNK_SIZE:
5692 /* read the chunk size and assign it to ->chunk_len,
5693 * then set ->next to point to the body and switch to
5694 * DATA or TRAILERS state.
5695 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005696 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005697 if (ret == 0)
5698 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005699 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005700 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005701 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005702 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005703 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005704 }
5705
5706 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005707 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005708 msg->chunk_len = chunk;
5709 msg->body_len += chunk;
5710
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005711 if (msg->chunk_len) {
5712 msg->msg_state = HTTP_MSG_DATA;
5713 goto switch_states;
5714 }
5715 msg->msg_state = HTTP_MSG_TRAILERS;
5716 /* fall through for HTTP_MSG_TRAILERS */
5717
5718 case HTTP_MSG_TRAILERS:
5719 ret = http_forward_trailers(msg);
5720 if (ret < 0)
5721 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005722 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5723 /* default_ret */ 1,
5724 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005725 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005726 if (!ret)
5727 goto missing_data_or_waiting;
5728 break;
5729
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005730 default:
5731 /* This should no happen in this function */
5732 goto error;
5733 }
5734
5735 msg->msg_state = HTTP_MSG_ENDING;
5736 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005737 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005738 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005739 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005740 /* default_ret */ msg->next,
5741 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005742 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005743 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005744 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5745 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005746 if (msg->next)
5747 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005748
Christopher Fauletda02e172015-12-04 09:25:05 +01005749 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005750 /* default_ret */ 1,
5751 /* on_error */ goto error,
5752 /* on_wait */ goto waiting);
5753 msg->msg_state = HTTP_MSG_DONE;
5754 return 1;
5755
5756 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005757 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005758 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005759 /* default_ret */ msg->next,
5760 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005761 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005762 msg->next -= ret;
5763 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5764 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005765 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005766 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005767 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005768 return 0;
5769
5770 chunk_parsing_error:
5771 if (msg->err_pos >= 0) {
5772 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005773 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005774 msg->msg_state, strm_fe(s));
5775 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005776 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005777 msg, msg->msg_state, s->be);
5778 }
5779 error:
5780 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005781}
5782
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005783
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005784/* Iterate the same filter through all request headers.
5785 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005786 * Since it can manage the switch to another backend, it updates the per-proxy
5787 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005788 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005789int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005790{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005791 char *cur_ptr, *cur_end, *cur_next;
5792 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005793 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005794 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005795 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005796
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005797 last_hdr = 0;
5798
Willy Tarreauf37954d2018-06-15 18:31:02 +02005799 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005800 old_idx = 0;
5801
5802 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005803 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005804 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005805 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005806 (exp->action == ACT_ALLOW ||
5807 exp->action == ACT_DENY ||
5808 exp->action == ACT_TARPIT))
5809 return 0;
5810
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005811 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005812 if (!cur_idx)
5813 break;
5814
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005815 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005816 cur_ptr = cur_next;
5817 cur_end = cur_ptr + cur_hdr->len;
5818 cur_next = cur_end + cur_hdr->cr + 1;
5819
5820 /* Now we have one header between cur_ptr and cur_end,
5821 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005822 */
5823
Willy Tarreau15a53a42015-01-21 13:39:42 +01005824 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005825 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005826 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005827 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005828 last_hdr = 1;
5829 break;
5830
5831 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005832 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005833 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005834 break;
5835
5836 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005837 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005838 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005839 break;
5840
5841 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005842 len = exp_replace(trash.area,
5843 trash.size, cur_ptr,
5844 exp->replace, pmatch);
5845 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005846 return -1;
5847
Willy Tarreau6e27be12018-08-22 04:46:47 +02005848 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5849
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005850 /* FIXME: if the user adds a newline in the replacement, the
5851 * index will not be recalculated for now, and the new line
5852 * will not be counted as a new header.
5853 */
5854
5855 cur_end += delta;
5856 cur_next += delta;
5857 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005858 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005859 break;
5860
5861 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005862 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005863 cur_next += delta;
5864
Willy Tarreaufa355d42009-11-29 18:12:29 +01005865 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005866 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5867 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005868 cur_hdr->len = 0;
5869 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005870 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005871 break;
5872
5873 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005874 }
5875
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005876 /* keep the link from this header to next one in case of later
5877 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005878 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005879 old_idx = cur_idx;
5880 }
5881 return 0;
5882}
5883
5884
5885/* Apply the filter to the request line.
5886 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5887 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005888 * Since it can manage the switch to another backend, it updates the per-proxy
5889 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005890 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005891int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005892{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005893 char *cur_ptr, *cur_end;
5894 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005895 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005896 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005897
Willy Tarreau3d300592007-03-18 18:34:41 +01005898 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005899 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005900 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005901 (exp->action == ACT_ALLOW ||
5902 exp->action == ACT_DENY ||
5903 exp->action == ACT_TARPIT))
5904 return 0;
5905 else if (exp->action == ACT_REMOVE)
5906 return 0;
5907
5908 done = 0;
5909
Willy Tarreauf37954d2018-06-15 18:31:02 +02005910 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005911 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005912
5913 /* Now we have the request line between cur_ptr and cur_end */
5914
Willy Tarreau15a53a42015-01-21 13:39:42 +01005915 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005916 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005917 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005918 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005919 done = 1;
5920 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005921
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005922 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005923 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005924 done = 1;
5925 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005926
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005927 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005928 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005929 done = 1;
5930 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005931
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005932 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005933 len = exp_replace(trash.area, trash.size,
5934 cur_ptr, exp->replace, pmatch);
5935 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005936 return -1;
5937
Willy Tarreau6e27be12018-08-22 04:46:47 +02005938 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5939
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005940 /* FIXME: if the user adds a newline in the replacement, the
5941 * index will not be recalculated for now, and the new line
5942 * will not be counted as a new header.
5943 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005944
Willy Tarreaufa355d42009-11-29 18:12:29 +01005945 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005946 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005947 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005948 HTTP_MSG_RQMETH,
5949 cur_ptr, cur_end + 1,
5950 NULL, NULL);
5951 if (unlikely(!cur_end))
5952 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005953
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005954 /* we have a full request and we know that we have either a CR
5955 * or an LF at <ptr>.
5956 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005957 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5958 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005959 /* there is no point trying this regex on headers */
5960 return 1;
5961 }
5962 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005963 return done;
5964}
Willy Tarreau97de6242006-12-27 17:18:38 +01005965
Willy Tarreau58f10d72006-12-04 02:26:12 +01005966
Willy Tarreau58f10d72006-12-04 02:26:12 +01005967
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005968/*
Willy Tarreau87b09662015-04-03 00:22:06 +02005969 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005970 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005971 * unparsable request. Since it can manage the switch to another backend, it
5972 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005973 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005974int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005975{
Willy Tarreau192252e2015-04-04 01:47:55 +02005976 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005977 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005978 struct hdr_exp *exp;
5979
5980 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005981 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005982
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005983 /*
5984 * The interleaving of transformations and verdicts
5985 * makes it difficult to decide to continue or stop
5986 * the evaluation.
5987 */
5988
Willy Tarreau6c123b12010-01-28 20:22:06 +01005989 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5990 break;
5991
Willy Tarreau3d300592007-03-18 18:34:41 +01005992 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005993 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005994 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005995 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005996
5997 /* if this filter had a condition, evaluate it now and skip to
5998 * next filter if the condition does not match.
5999 */
6000 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006001 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006002 ret = acl_pass(ret);
6003 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6004 ret = !ret;
6005
6006 if (!ret)
6007 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006008 }
6009
6010 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006011 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006012 if (unlikely(ret < 0))
6013 return -1;
6014
6015 if (likely(ret == 0)) {
6016 /* The filter did not match the request, it can be
6017 * iterated through all headers.
6018 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006019 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6020 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006021 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006022 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006023 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006024}
6025
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006026
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006027/* Delete a value in a header between delimiters <from> and <next> in buffer
6028 * <buf>. The number of characters displaced is returned, and the pointer to
6029 * the first delimiter is updated if required. The function tries as much as
6030 * possible to respect the following principles :
6031 * - replace <from> delimiter by the <next> one unless <from> points to a
6032 * colon, in which case <next> is simply removed
6033 * - set exactly one space character after the new first delimiter, unless
6034 * there are not enough characters in the block being moved to do so.
6035 * - remove unneeded spaces before the previous delimiter and after the new
6036 * one.
6037 *
6038 * It is the caller's responsibility to ensure that :
6039 * - <from> points to a valid delimiter or the colon ;
6040 * - <next> points to a valid delimiter or the final CR/LF ;
6041 * - there are non-space chars before <from> ;
6042 * - there is a CR/LF at or after <next>.
6043 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006044int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006045{
6046 char *prev = *from;
6047
6048 if (*prev == ':') {
6049 /* We're removing the first value, preserve the colon and add a
6050 * space if possible.
6051 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006052 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006053 next++;
6054 prev++;
6055 if (prev < next)
6056 *prev++ = ' ';
6057
Willy Tarreau2235b262016-11-05 15:50:20 +01006058 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006059 next++;
6060 } else {
6061 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006062 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006063 prev--;
6064 *from = prev;
6065
6066 /* copy the delimiter and if possible a space if we're
6067 * not at the end of the line.
6068 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006069 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006070 *prev++ = *next++;
6071 if (prev + 1 < next)
6072 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006073 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006074 next++;
6075 }
6076 }
Willy Tarreaue3128022018-07-12 15:55:34 +02006077 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006078}
6079
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006080/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006081 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006082 * desirable to call it only when needed. This code is quite complex because
6083 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6084 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006085 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006086void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006087{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006088 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006089 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006090 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006091 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006092 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6093 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006094
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006095 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006096 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006097 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006098
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006099 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006100 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006101 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006102
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006103 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006104 hdr_beg = hdr_next;
6105 hdr_end = hdr_beg + cur_hdr->len;
6106 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006107
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006108 /* We have one full header between hdr_beg and hdr_end, and the
6109 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006110 * "Cookie:" headers.
6111 */
6112
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006113 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006114 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006115 old_idx = cur_idx;
6116 continue;
6117 }
6118
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006119 del_from = NULL; /* nothing to be deleted */
6120 preserve_hdr = 0; /* assume we may kill the whole header */
6121
Willy Tarreau58f10d72006-12-04 02:26:12 +01006122 /* Now look for cookies. Conforming to RFC2109, we have to support
6123 * attributes whose name begin with a '$', and associate them with
6124 * the right cookie, if we want to delete this cookie.
6125 * So there are 3 cases for each cookie read :
6126 * 1) it's a special attribute, beginning with a '$' : ignore it.
6127 * 2) it's a server id cookie that we *MAY* want to delete : save
6128 * some pointers on it (last semi-colon, beginning of cookie...)
6129 * 3) it's an application cookie : we *MAY* have to delete a previous
6130 * "special" cookie.
6131 * At the end of loop, if a "special" cookie remains, we may have to
6132 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006133 * *MUST* delete it.
6134 *
6135 * Note: RFC2965 is unclear about the processing of spaces around
6136 * the equal sign in the ATTR=VALUE form. A careful inspection of
6137 * the RFC explicitly allows spaces before it, and not within the
6138 * tokens (attrs or values). An inspection of RFC2109 allows that
6139 * too but section 10.1.3 lets one think that spaces may be allowed
6140 * after the equal sign too, resulting in some (rare) buggy
6141 * implementations trying to do that. So let's do what servers do.
6142 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6143 * allowed quoted strings in values, with any possible character
6144 * after a backslash, including control chars and delimitors, which
6145 * causes parsing to become ambiguous. Browsers also allow spaces
6146 * within values even without quotes.
6147 *
6148 * We have to keep multiple pointers in order to support cookie
6149 * removal at the beginning, middle or end of header without
6150 * corrupting the header. All of these headers are valid :
6151 *
6152 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6153 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6154 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6155 * | | | | | | | | |
6156 * | | | | | | | | hdr_end <--+
6157 * | | | | | | | +--> next
6158 * | | | | | | +----> val_end
6159 * | | | | | +-----------> val_beg
6160 * | | | | +--------------> equal
6161 * | | | +----------------> att_end
6162 * | | +---------------------> att_beg
6163 * | +--------------------------> prev
6164 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006165 */
6166
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006167 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6168 /* Iterate through all cookies on this line */
6169
6170 /* find att_beg */
6171 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006172 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006173 att_beg++;
6174
6175 /* find att_end : this is the first character after the last non
6176 * space before the equal. It may be equal to hdr_end.
6177 */
6178 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006179
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006180 while (equal < hdr_end) {
6181 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006182 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006183 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006184 continue;
6185 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006186 }
6187
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006188 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6189 * is between <att_beg> and <equal>, both may be identical.
6190 */
6191
6192 /* look for end of cookie if there is an equal sign */
6193 if (equal < hdr_end && *equal == '=') {
6194 /* look for the beginning of the value */
6195 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006196 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006197 val_beg++;
6198
6199 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006200 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006201
6202 /* make val_end point to the first white space or delimitor after the value */
6203 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006204 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006205 val_end--;
6206 } else {
6207 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006208 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006209
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006210 /* We have nothing to do with attributes beginning with '$'. However,
6211 * they will automatically be removed if a header before them is removed,
6212 * since they're supposed to be linked together.
6213 */
6214 if (*att_beg == '$')
6215 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006216
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006217 /* Ignore cookies with no equal sign */
6218 if (equal == next) {
6219 /* This is not our cookie, so we must preserve it. But if we already
6220 * scheduled another cookie for removal, we cannot remove the
6221 * complete header, but we can remove the previous block itself.
6222 */
6223 preserve_hdr = 1;
6224 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006225 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006226 val_end += delta;
6227 next += delta;
6228 hdr_end += delta;
6229 hdr_next += delta;
6230 cur_hdr->len += delta;
6231 http_msg_move_end(&txn->req, delta);
6232 prev = del_from;
6233 del_from = NULL;
6234 }
6235 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006236 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006237
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006238 /* if there are spaces around the equal sign, we need to
6239 * strip them otherwise we'll get trouble for cookie captures,
6240 * or even for rewrites. Since this happens extremely rarely,
6241 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006242 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006243 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6244 int stripped_before = 0;
6245 int stripped_after = 0;
6246
6247 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006248 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006249 equal += stripped_before;
6250 val_beg += stripped_before;
6251 }
6252
6253 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006254 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006255 val_beg += stripped_after;
6256 stripped_before += stripped_after;
6257 }
6258
6259 val_end += stripped_before;
6260 next += stripped_before;
6261 hdr_end += stripped_before;
6262 hdr_next += stripped_before;
6263 cur_hdr->len += stripped_before;
6264 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006265 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006266 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006267
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006268 /* First, let's see if we want to capture this cookie. We check
6269 * that we don't already have a client side cookie, because we
6270 * can only capture one. Also as an optimisation, we ignore
6271 * cookies shorter than the declared name.
6272 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006273 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6274 (val_end - att_beg >= sess->fe->capture_namelen) &&
6275 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006276 int log_len = val_end - att_beg;
6277
Willy Tarreaubafbe012017-11-24 17:34:44 +01006278 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006279 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006280 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006281 if (log_len > sess->fe->capture_len)
6282 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006283 memcpy(txn->cli_cookie, att_beg, log_len);
6284 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006285 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006286 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006287
Willy Tarreaubca99692010-10-06 19:25:55 +02006288 /* Persistence cookies in passive, rewrite or insert mode have the
6289 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006290 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006291 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006292 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006293 * For cookies in prefix mode, the form is :
6294 *
6295 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006296 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006297 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6298 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6299 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006300 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006301
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006302 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6303 * have the server ID between val_beg and delim, and the original cookie between
6304 * delim+1 and val_end. Otherwise, delim==val_end :
6305 *
6306 * Cookie: NAME=SRV; # in all but prefix modes
6307 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6308 * | || || | |+-> next
6309 * | || || | +--> val_end
6310 * | || || +---------> delim
6311 * | || |+------------> val_beg
6312 * | || +-------------> att_end = equal
6313 * | |+-----------------> att_beg
6314 * | +------------------> prev
6315 * +-------------------------> hdr_beg
6316 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006317
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006318 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006319 for (delim = val_beg; delim < val_end; delim++)
6320 if (*delim == COOKIE_DELIM)
6321 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006322 } else {
6323 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006324 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006325 /* Now check if the cookie contains a date field, which would
6326 * appear after a vertical bar ('|') just after the server name
6327 * and before the delimiter.
6328 */
6329 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6330 if (vbar1) {
6331 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006332 * right is the last seen date. It is a base64 encoded
6333 * 30-bit value representing the UNIX date since the
6334 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006335 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006336 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006337 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006338 if (val_end - vbar1 >= 5) {
6339 val = b64tos30(vbar1);
6340 if (val > 0)
6341 txn->cookie_last_date = val << 2;
6342 }
6343 /* look for a second vertical bar */
6344 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6345 if (vbar1 && (val_end - vbar1 > 5)) {
6346 val = b64tos30(vbar1 + 1);
6347 if (val > 0)
6348 txn->cookie_first_date = val << 2;
6349 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006350 }
6351 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006352
Willy Tarreauf64d1412010-10-07 20:06:11 +02006353 /* if the cookie has an expiration date and the proxy wants to check
6354 * it, then we do that now. We first check if the cookie is too old,
6355 * then only if it has expired. We detect strict overflow because the
6356 * time resolution here is not great (4 seconds). Cookies with dates
6357 * in the future are ignored if their offset is beyond one day. This
6358 * allows an admin to fix timezone issues without expiring everyone
6359 * and at the same time avoids keeping unwanted side effects for too
6360 * long.
6361 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006362 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6363 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006364 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006365 txn->flags &= ~TX_CK_MASK;
6366 txn->flags |= TX_CK_OLD;
6367 delim = val_beg; // let's pretend we have not found the cookie
6368 txn->cookie_first_date = 0;
6369 txn->cookie_last_date = 0;
6370 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006371 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6372 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006373 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006374 txn->flags &= ~TX_CK_MASK;
6375 txn->flags |= TX_CK_EXPIRED;
6376 delim = val_beg; // let's pretend we have not found the cookie
6377 txn->cookie_first_date = 0;
6378 txn->cookie_last_date = 0;
6379 }
6380
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006381 /* Here, we'll look for the first running server which supports the cookie.
6382 * This allows to share a same cookie between several servers, for example
6383 * to dedicate backup servers to specific servers only.
6384 * However, to prevent clients from sticking to cookie-less backup server
6385 * when they have incidentely learned an empty cookie, we simply ignore
6386 * empty cookies and mark them as invalid.
6387 * The same behaviour is applied when persistence must be ignored.
6388 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006389 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006390 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006391
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006392 while (srv) {
6393 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6394 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006395 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006396 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006397 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006398 /* we found the server and we can use it */
6399 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006400 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006401 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006402 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006403 break;
6404 } else {
6405 /* we found a server, but it's down,
6406 * mark it as such and go on in case
6407 * another one is available.
6408 */
6409 txn->flags &= ~TX_CK_MASK;
6410 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006411 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006412 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006413 srv = srv->next;
6414 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006415
Willy Tarreauf64d1412010-10-07 20:06:11 +02006416 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006417 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006418 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006419 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006420 txn->flags |= TX_CK_UNUSED;
6421 else
6422 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006423 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006424
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006425 /* depending on the cookie mode, we may have to either :
6426 * - delete the complete cookie if we're in insert+indirect mode, so that
6427 * the server never sees it ;
6428 * - remove the server id from the cookie value, and tag the cookie as an
6429 * application cookie so that it does not get accidentely removed later,
6430 * if we're in cookie prefix mode
6431 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006432 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006433 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006434
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006435 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006436 val_end += delta;
6437 next += delta;
6438 hdr_end += delta;
6439 hdr_next += delta;
6440 cur_hdr->len += delta;
6441 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006442
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006443 del_from = NULL;
6444 preserve_hdr = 1; /* we want to keep this cookie */
6445 }
6446 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006447 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006448 del_from = prev;
6449 }
6450 } else {
6451 /* This is not our cookie, so we must preserve it. But if we already
6452 * scheduled another cookie for removal, we cannot remove the
6453 * complete header, but we can remove the previous block itself.
6454 */
6455 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006456
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006457 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006458 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006459 if (att_beg >= del_from)
6460 att_beg += delta;
6461 if (att_end >= del_from)
6462 att_end += delta;
6463 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006464 val_end += delta;
6465 next += delta;
6466 hdr_end += delta;
6467 hdr_next += delta;
6468 cur_hdr->len += delta;
6469 http_msg_move_end(&txn->req, delta);
6470 prev = del_from;
6471 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006472 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006473 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006474
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006475 /* continue with next cookie on this header line */
6476 att_beg = next;
6477 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006478
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006479 /* There are no more cookies on this line.
6480 * We may still have one (or several) marked for deletion at the
6481 * end of the line. We must do this now in two ways :
6482 * - if some cookies must be preserved, we only delete from the
6483 * mark to the end of line ;
6484 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006485 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006486 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006487 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006488 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006489 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006490 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006491 cur_hdr->len += delta;
6492 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006493 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006494
6495 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006496 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6497 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006498 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006499 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006500 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006501 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006502 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006503 }
6504
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006505 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006506 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006507 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006508}
6509
6510
Willy Tarreaua15645d2007-03-18 16:22:39 +01006511/* Iterate the same filter through all response headers contained in <rtr>.
6512 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6513 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006514int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006515{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006516 char *cur_ptr, *cur_end, *cur_next;
6517 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006518 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006519 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006520 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006521
6522 last_hdr = 0;
6523
Willy Tarreauf37954d2018-06-15 18:31:02 +02006524 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006525 old_idx = 0;
6526
6527 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006528 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006529 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006530 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006531 (exp->action == ACT_ALLOW ||
6532 exp->action == ACT_DENY))
6533 return 0;
6534
6535 cur_idx = txn->hdr_idx.v[old_idx].next;
6536 if (!cur_idx)
6537 break;
6538
6539 cur_hdr = &txn->hdr_idx.v[cur_idx];
6540 cur_ptr = cur_next;
6541 cur_end = cur_ptr + cur_hdr->len;
6542 cur_next = cur_end + cur_hdr->cr + 1;
6543
6544 /* Now we have one header between cur_ptr and cur_end,
6545 * and the next header starts at cur_next.
6546 */
6547
Willy Tarreau15a53a42015-01-21 13:39:42 +01006548 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006549 switch (exp->action) {
6550 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006551 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006552 last_hdr = 1;
6553 break;
6554
6555 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006556 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006557 last_hdr = 1;
6558 break;
6559
6560 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006561 len = exp_replace(trash.area,
6562 trash.size, cur_ptr,
6563 exp->replace, pmatch);
6564 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006565 return -1;
6566
Willy Tarreau6e27be12018-08-22 04:46:47 +02006567 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6568
Willy Tarreaua15645d2007-03-18 16:22:39 +01006569 /* FIXME: if the user adds a newline in the replacement, the
6570 * index will not be recalculated for now, and the new line
6571 * will not be counted as a new header.
6572 */
6573
6574 cur_end += delta;
6575 cur_next += delta;
6576 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006577 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006578 break;
6579
6580 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006581 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006582 cur_next += delta;
6583
Willy Tarreaufa355d42009-11-29 18:12:29 +01006584 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006585 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6586 txn->hdr_idx.used--;
6587 cur_hdr->len = 0;
6588 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006589 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006590 break;
6591
6592 }
6593 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006594
6595 /* keep the link from this header to next one in case of later
6596 * removal of next header.
6597 */
6598 old_idx = cur_idx;
6599 }
6600 return 0;
6601}
6602
6603
6604/* Apply the filter to the status line in the response buffer <rtr>.
6605 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6606 * or -1 if a replacement resulted in an invalid status line.
6607 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006608int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006609{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006610 char *cur_ptr, *cur_end;
6611 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006612 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006613 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006614
Willy Tarreau3d300592007-03-18 18:34:41 +01006615 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006616 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006617 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006618 (exp->action == ACT_ALLOW ||
6619 exp->action == ACT_DENY))
6620 return 0;
6621 else if (exp->action == ACT_REMOVE)
6622 return 0;
6623
6624 done = 0;
6625
Willy Tarreauf37954d2018-06-15 18:31:02 +02006626 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006627 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006628
6629 /* Now we have the status line between cur_ptr and cur_end */
6630
Willy Tarreau15a53a42015-01-21 13:39:42 +01006631 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006632 switch (exp->action) {
6633 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006634 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006635 done = 1;
6636 break;
6637
6638 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006639 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006640 done = 1;
6641 break;
6642
6643 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006644 len = exp_replace(trash.area, trash.size,
6645 cur_ptr, exp->replace, pmatch);
6646 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006647 return -1;
6648
Willy Tarreau6e27be12018-08-22 04:46:47 +02006649 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6650
Willy Tarreaua15645d2007-03-18 16:22:39 +01006651 /* FIXME: if the user adds a newline in the replacement, the
6652 * index will not be recalculated for now, and the new line
6653 * will not be counted as a new header.
6654 */
6655
Willy Tarreaufa355d42009-11-29 18:12:29 +01006656 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006657 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006658 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006659 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006660 cur_ptr, cur_end + 1,
6661 NULL, NULL);
6662 if (unlikely(!cur_end))
6663 return -1;
6664
6665 /* we have a full respnse and we know that we have either a CR
6666 * or an LF at <ptr>.
6667 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006668 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006669 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006670 /* there is no point trying this regex on headers */
6671 return 1;
6672 }
6673 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006674 return done;
6675}
6676
6677
6678
6679/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006680 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006681 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6682 * unparsable response.
6683 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006684int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006685{
Willy Tarreau192252e2015-04-04 01:47:55 +02006686 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006687 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006688 struct hdr_exp *exp;
6689
6690 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006691 int ret;
6692
6693 /*
6694 * The interleaving of transformations and verdicts
6695 * makes it difficult to decide to continue or stop
6696 * the evaluation.
6697 */
6698
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006699 if (txn->flags & TX_SVDENY)
6700 break;
6701
Willy Tarreau3d300592007-03-18 18:34:41 +01006702 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006703 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6704 exp->action == ACT_PASS)) {
6705 exp = exp->next;
6706 continue;
6707 }
6708
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006709 /* if this filter had a condition, evaluate it now and skip to
6710 * next filter if the condition does not match.
6711 */
6712 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006713 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006714 ret = acl_pass(ret);
6715 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6716 ret = !ret;
6717 if (!ret)
6718 continue;
6719 }
6720
Willy Tarreaua15645d2007-03-18 16:22:39 +01006721 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006722 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006723 if (unlikely(ret < 0))
6724 return -1;
6725
6726 if (likely(ret == 0)) {
6727 /* The filter did not match the response, it can be
6728 * iterated through all headers.
6729 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006730 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6731 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006732 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006733 }
6734 return 0;
6735}
6736
6737
Willy Tarreaua15645d2007-03-18 16:22:39 +01006738/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006739 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006740 * desirable to call it only when needed. This function is also used when we
6741 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006742 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006743void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006744{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006745 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006746 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006747 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006748 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006749 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006750 char *hdr_beg, *hdr_end, *hdr_next;
6751 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006752
Willy Tarreaua15645d2007-03-18 16:22:39 +01006753 /* Iterate through the headers.
6754 * we start with the start line.
6755 */
6756 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006757 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006758
6759 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6760 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006761 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006762
6763 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006764 hdr_beg = hdr_next;
6765 hdr_end = hdr_beg + cur_hdr->len;
6766 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006767
Willy Tarreau24581ba2010-08-31 22:39:35 +02006768 /* We have one full header between hdr_beg and hdr_end, and the
6769 * next header starts at hdr_next. We're only interested in
6770 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006771 */
6772
Willy Tarreau24581ba2010-08-31 22:39:35 +02006773 is_cookie2 = 0;
6774 prev = hdr_beg + 10;
6775 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006776 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006777 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6778 if (!val) {
6779 old_idx = cur_idx;
6780 continue;
6781 }
6782 is_cookie2 = 1;
6783 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006784 }
6785
Willy Tarreau24581ba2010-08-31 22:39:35 +02006786 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6787 * <prev> points to the colon.
6788 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006789 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006790
Willy Tarreau24581ba2010-08-31 22:39:35 +02006791 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6792 * check-cache is enabled) and we are not interested in checking
6793 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006794 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006795 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006796 return;
6797
Willy Tarreau24581ba2010-08-31 22:39:35 +02006798 /* OK so now we know we have to process this response cookie.
6799 * The format of the Set-Cookie header is slightly different
6800 * from the format of the Cookie header in that it does not
6801 * support the comma as a cookie delimiter (thus the header
6802 * cannot be folded) because the Expires attribute described in
6803 * the original Netscape's spec may contain an unquoted date
6804 * with a comma inside. We have to live with this because
6805 * many browsers don't support Max-Age and some browsers don't
6806 * support quoted strings. However the Set-Cookie2 header is
6807 * clean.
6808 *
6809 * We have to keep multiple pointers in order to support cookie
6810 * removal at the beginning, middle or end of header without
6811 * corrupting the header (in case of set-cookie2). A special
6812 * pointer, <scav> points to the beginning of the set-cookie-av
6813 * fields after the first semi-colon. The <next> pointer points
6814 * either to the end of line (set-cookie) or next unquoted comma
6815 * (set-cookie2). All of these headers are valid :
6816 *
6817 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6818 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6819 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6820 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6821 * | | | | | | | | | |
6822 * | | | | | | | | +-> next hdr_end <--+
6823 * | | | | | | | +------------> scav
6824 * | | | | | | +--------------> val_end
6825 * | | | | | +--------------------> val_beg
6826 * | | | | +----------------------> equal
6827 * | | | +------------------------> att_end
6828 * | | +----------------------------> att_beg
6829 * | +------------------------------> prev
6830 * +-----------------------------------------> hdr_beg
6831 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006832
Willy Tarreau24581ba2010-08-31 22:39:35 +02006833 for (; prev < hdr_end; prev = next) {
6834 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006835
Willy Tarreau24581ba2010-08-31 22:39:35 +02006836 /* find att_beg */
6837 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006838 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006839 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006840
Willy Tarreau24581ba2010-08-31 22:39:35 +02006841 /* find att_end : this is the first character after the last non
6842 * space before the equal. It may be equal to hdr_end.
6843 */
6844 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006845
Willy Tarreau24581ba2010-08-31 22:39:35 +02006846 while (equal < hdr_end) {
6847 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6848 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006849 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006850 continue;
6851 att_end = equal;
6852 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006853
Willy Tarreau24581ba2010-08-31 22:39:35 +02006854 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6855 * is between <att_beg> and <equal>, both may be identical.
6856 */
6857
6858 /* look for end of cookie if there is an equal sign */
6859 if (equal < hdr_end && *equal == '=') {
6860 /* look for the beginning of the value */
6861 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006862 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006863 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006864
Willy Tarreau24581ba2010-08-31 22:39:35 +02006865 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006866 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006867
6868 /* make val_end point to the first white space or delimitor after the value */
6869 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006870 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006871 val_end--;
6872 } else {
6873 /* <equal> points to next comma, semi-colon or EOL */
6874 val_beg = val_end = next = equal;
6875 }
6876
6877 if (next < hdr_end) {
6878 /* Set-Cookie2 supports multiple cookies, and <next> points to
6879 * a colon or semi-colon before the end. So skip all attr-value
6880 * pairs and look for the next comma. For Set-Cookie, since
6881 * commas are permitted in values, skip to the end.
6882 */
6883 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02006884 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006885 else
6886 next = hdr_end;
6887 }
6888
6889 /* Now everything is as on the diagram above */
6890
6891 /* Ignore cookies with no equal sign */
6892 if (equal == val_end)
6893 continue;
6894
6895 /* If there are spaces around the equal sign, we need to
6896 * strip them otherwise we'll get trouble for cookie captures,
6897 * or even for rewrites. Since this happens extremely rarely,
6898 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006899 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006900 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6901 int stripped_before = 0;
6902 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006903
Willy Tarreau24581ba2010-08-31 22:39:35 +02006904 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006905 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006906 equal += stripped_before;
6907 val_beg += stripped_before;
6908 }
6909
6910 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006911 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006912 val_beg += stripped_after;
6913 stripped_before += stripped_after;
6914 }
6915
6916 val_end += stripped_before;
6917 next += stripped_before;
6918 hdr_end += stripped_before;
6919 hdr_next += stripped_before;
6920 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006921 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006922 }
6923
6924 /* First, let's see if we want to capture this cookie. We check
6925 * that we don't already have a server side cookie, because we
6926 * can only capture one. Also as an optimisation, we ignore
6927 * cookies shorter than the declared name.
6928 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006929 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006930 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006931 (val_end - att_beg >= sess->fe->capture_namelen) &&
6932 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006933 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006934 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006935 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01006936 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006937 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006938 if (log_len > sess->fe->capture_len)
6939 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01006940 memcpy(txn->srv_cookie, att_beg, log_len);
6941 txn->srv_cookie[log_len] = 0;
6942 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006943 }
6944
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006945 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006946 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006947 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006948 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6949 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006950 /* assume passive cookie by default */
6951 txn->flags &= ~TX_SCK_MASK;
6952 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006953
6954 /* If the cookie is in insert mode on a known server, we'll delete
6955 * this occurrence because we'll insert another one later.
6956 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006957 * a direct access.
6958 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006959 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006960 /* The "preserve" flag was set, we don't want to touch the
6961 * server's cookie.
6962 */
6963 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006964 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006965 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006966 /* this cookie must be deleted */
6967 if (*prev == ':' && next == hdr_end) {
6968 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006969 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006970 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6971 txn->hdr_idx.used--;
6972 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006973 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006974 hdr_next += delta;
6975 http_msg_move_end(&txn->rsp, delta);
6976 /* note: while both invalid now, <next> and <hdr_end>
6977 * are still equal, so the for() will stop as expected.
6978 */
6979 } else {
6980 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006981 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006982 next = prev;
6983 hdr_end += delta;
6984 hdr_next += delta;
6985 cur_hdr->len += delta;
6986 http_msg_move_end(&txn->rsp, delta);
6987 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006988 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006989 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006990 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006991 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006992 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006993 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006994 * with this server since we know it.
6995 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006996 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006997 next += delta;
6998 hdr_end += delta;
6999 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007000 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007001 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007002
Willy Tarreauf1348312010-10-07 15:54:11 +02007003 txn->flags &= ~TX_SCK_MASK;
7004 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007005 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007006 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007007 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007008 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007009 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007010 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007011 next += delta;
7012 hdr_end += delta;
7013 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007014 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007015 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007016
Willy Tarreau827aee92011-03-10 16:55:02 +01007017 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007018 txn->flags &= ~TX_SCK_MASK;
7019 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007020 }
7021 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007022 /* that's done for this cookie, check the next one on the same
7023 * line when next != hdr_end (only if is_cookie2).
7024 */
7025 }
7026 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007027 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007028 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007029}
7030
7031
Willy Tarreaua15645d2007-03-18 16:22:39 +01007032/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007033 * Parses the Cache-Control and Pragma request header fields to determine if
7034 * the request may be served from the cache and/or if it is cacheable. Updates
7035 * s->txn->flags.
7036 */
7037void check_request_for_cacheability(struct stream *s, struct channel *chn)
7038{
7039 struct http_txn *txn = s->txn;
7040 char *p1, *p2;
7041 char *cur_ptr, *cur_end, *cur_next;
7042 int pragma_found;
7043 int cc_found;
7044 int cur_idx;
7045
7046 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
7047 return; /* nothing more to do here */
7048
7049 cur_idx = 0;
7050 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007051 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007052
7053 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7054 struct hdr_idx_elem *cur_hdr;
7055 int val;
7056
7057 cur_hdr = &txn->hdr_idx.v[cur_idx];
7058 cur_ptr = cur_next;
7059 cur_end = cur_ptr + cur_hdr->len;
7060 cur_next = cur_end + cur_hdr->cr + 1;
7061
7062 /* We have one full header between cur_ptr and cur_end, and the
7063 * next header starts at cur_next.
7064 */
7065
7066 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7067 if (val) {
7068 if ((cur_end - (cur_ptr + val) >= 8) &&
7069 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7070 pragma_found = 1;
7071 continue;
7072 }
7073 }
7074
William Lallemand8a16fe02018-05-22 11:04:33 +02007075 /* Don't use the cache and don't try to store if we found the
7076 * Authorization header */
7077 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
7078 if (val) {
7079 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7080 txn->flags |= TX_CACHE_IGNORE;
7081 continue;
7082 }
7083
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01007084 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7085 if (!val)
7086 continue;
7087
7088 /* OK, right now we know we have a cache-control header at cur_ptr */
7089 cc_found = 1;
7090 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
7091
7092 if (p1 >= cur_end) /* no more info */
7093 continue;
7094
7095 /* p1 is at the beginning of the value */
7096 p2 = p1;
7097 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
7098 p2++;
7099
7100 /* we have a complete value between p1 and p2. We don't check the
7101 * values after max-age, max-stale nor min-fresh, we simply don't
7102 * use the cache when they're specified.
7103 */
7104 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
7105 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
7106 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
7107 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
7108 txn->flags |= TX_CACHE_IGNORE;
7109 continue;
7110 }
7111
7112 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
7113 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7114 continue;
7115 }
7116 }
7117
7118 /* RFC7234#5.4:
7119 * When the Cache-Control header field is also present and
7120 * understood in a request, Pragma is ignored.
7121 * When the Cache-Control header field is not present in a
7122 * request, caches MUST consider the no-cache request
7123 * pragma-directive as having the same effect as if
7124 * "Cache-Control: no-cache" were present.
7125 */
7126 if (!cc_found && pragma_found)
7127 txn->flags |= TX_CACHE_IGNORE;
7128}
7129
7130/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007131 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007132 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007133void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007134{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007135 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007136 char *p1, *p2;
7137
7138 char *cur_ptr, *cur_end, *cur_next;
7139 int cur_idx;
7140
Willy Tarreau12b32f22017-12-21 16:08:09 +01007141 if (txn->status < 200) {
7142 /* do not try to cache interim responses! */
7143 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007144 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01007145 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007146
7147 /* Iterate through the headers.
7148 * we start with the start line.
7149 */
7150 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007151 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007152
7153 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7154 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007155 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007156
7157 cur_hdr = &txn->hdr_idx.v[cur_idx];
7158 cur_ptr = cur_next;
7159 cur_end = cur_ptr + cur_hdr->len;
7160 cur_next = cur_end + cur_hdr->cr + 1;
7161
7162 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007163 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007164 */
7165
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007166 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7167 if (val) {
7168 if ((cur_end - (cur_ptr + val) >= 8) &&
7169 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7170 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7171 return;
7172 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007173 }
7174
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007175 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7176 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007177 continue;
7178
7179 /* OK, right now we know we have a cache-control header at cur_ptr */
7180
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007181 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007182
7183 if (p1 >= cur_end) /* no more info */
7184 continue;
7185
7186 /* p1 is at the beginning of the value */
7187 p2 = p1;
7188
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007189 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007190 p2++;
7191
7192 /* we have a complete value between p1 and p2 */
7193 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007194 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7195 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7196 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7197 continue;
7198 }
7199
Willy Tarreaua15645d2007-03-18 16:22:39 +01007200 /* we have something of the form no-cache="set-cookie" */
7201 if ((cur_end - p1 >= 21) &&
7202 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7203 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007204 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007205 continue;
7206 }
7207
7208 /* OK, so we know that either p2 points to the end of string or to a comma */
7209 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007210 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007211 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007212 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007213 return;
7214 }
7215
7216 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007217 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007218 continue;
7219 }
7220 }
7221}
7222
Willy Tarreau58f10d72006-12-04 02:26:12 +01007223
Willy Tarreaub2513902006-12-17 14:52:38 +01007224/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007225 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007226 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007227 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007228 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007229 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007230 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007231 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007232 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007233int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007234{
7235 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007236 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007237 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007238
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007239 if (!uri_auth)
7240 return 0;
7241
Cyril Bonté70be45d2010-10-12 00:14:35 +02007242 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007243 return 0;
7244
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007245 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007246 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007247 return 0;
7248
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007249 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007250 return 0;
7251
Willy Tarreaub2513902006-12-17 14:52:38 +01007252 return 1;
7253}
7254
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007255/* Append the description of what is present in error snapshot <es> into <out>.
7256 * The description must be small enough to always fit in a trash. The output
7257 * buffer may be the trash so the trash must not be used inside this function.
7258 */
7259void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7260{
7261 chunk_appendf(&trash,
7262 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7263 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7264 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7265 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7266 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7267 es->ctx.http.m_flags, es->ctx.http.m_clen,
7268 es->ctx.http.m_blen, es->ctx.http.b_flags);
7269}
7270
Willy Tarreau4076a152009-04-02 15:18:36 +02007271/*
7272 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007273 * By default it tries to report the error position as msg->err_pos. However if
7274 * this one is not set, it will then report msg->next, which is the last known
7275 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007276 * displays buffers as a contiguous area starting at buf->p. The direction is
7277 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007278 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007279void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007280 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007281 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007282{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007283 union error_snapshot_ctx ctx;
7284 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007285
Willy Tarreauef3ca732018-09-07 15:47:35 +02007286 /* http-specific part now */
7287 ctx.http.sid = s->uniq_id;
7288 ctx.http.state = state;
7289 ctx.http.b_flags = msg->chn->flags;
7290 ctx.http.s_flags = s->flags;
7291 ctx.http.t_flags = s->txn->flags;
7292 ctx.http.m_flags = msg->flags;
7293 ctx.http.m_clen = msg->chunk_len;
7294 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007295
Willy Tarreauef3ca732018-09-07 15:47:35 +02007296 ofs = msg->chn->total - ci_data(msg->chn);
7297 if (ofs < 0)
7298 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007299
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007300 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007301 other_end, s->target,
7302 strm_sess(s), &msg->chn->buf,
7303 ofs, co_data(msg->chn),
7304 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7305 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007306}
Willy Tarreaub2513902006-12-17 14:52:38 +01007307
Willy Tarreau294c4732011-12-16 21:35:50 +01007308/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7309 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7310 * performed over the whole headers. Otherwise it must contain a valid header
7311 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7312 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7313 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7314 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007315 * -1. The value fetch stops at commas, so this function is suited for use with
7316 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007317 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007318 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007319unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007320 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007321 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007322{
Willy Tarreau294c4732011-12-16 21:35:50 +01007323 struct hdr_ctx local_ctx;
7324 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007325 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007326 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007327 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007328
Willy Tarreau294c4732011-12-16 21:35:50 +01007329 if (!ctx) {
7330 local_ctx.idx = 0;
7331 ctx = &local_ctx;
7332 }
7333
Willy Tarreaubce70882009-09-07 11:51:47 +02007334 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007335 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007336 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007337 occ--;
7338 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007339 *vptr = ctx->line + ctx->val;
7340 *vlen = ctx->vlen;
7341 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007342 }
7343 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007344 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007345 }
7346
7347 /* negative occurrence, we scan all the list then walk back */
7348 if (-occ > MAX_HDR_HISTORY)
7349 return 0;
7350
Willy Tarreau294c4732011-12-16 21:35:50 +01007351 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007352 while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007353 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7354 len_hist[hist_ptr] = ctx->vlen;
7355 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007356 hist_ptr = 0;
7357 found++;
7358 }
7359 if (-occ > found)
7360 return 0;
7361 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007362 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7363 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7364 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007365 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007366 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007367 if (hist_ptr >= MAX_HDR_HISTORY)
7368 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007369 *vptr = ptr_hist[hist_ptr];
7370 *vlen = len_hist[hist_ptr];
7371 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007372}
7373
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007374/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7375 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7376 * performed over the whole headers. Otherwise it must contain a valid header
7377 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7378 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7379 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7380 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7381 * -1. This function differs from http_get_hdr() in that it only returns full
7382 * line header values and does not stop at commas.
7383 * The return value is 0 if nothing was found, or non-zero otherwise.
7384 */
7385unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7386 struct hdr_idx *idx, int occ,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007387 struct hdr_ctx *ctx, char **vptr, size_t *vlen)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007388{
7389 struct hdr_ctx local_ctx;
7390 char *ptr_hist[MAX_HDR_HISTORY];
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007391 unsigned int len_hist[MAX_HDR_HISTORY];
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007392 unsigned int hist_ptr;
7393 int found;
7394
7395 if (!ctx) {
7396 local_ctx.idx = 0;
7397 ctx = &local_ctx;
7398 }
7399
7400 if (occ >= 0) {
7401 /* search from the beginning */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007402 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007403 occ--;
7404 if (occ <= 0) {
7405 *vptr = ctx->line + ctx->val;
7406 *vlen = ctx->vlen;
7407 return 1;
7408 }
7409 }
7410 return 0;
7411 }
7412
7413 /* negative occurrence, we scan all the list then walk back */
7414 if (-occ > MAX_HDR_HISTORY)
7415 return 0;
7416
7417 found = hist_ptr = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007418 while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007419 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7420 len_hist[hist_ptr] = ctx->vlen;
7421 if (++hist_ptr >= MAX_HDR_HISTORY)
7422 hist_ptr = 0;
7423 found++;
7424 }
7425 if (-occ > found)
7426 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007427
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007428 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007429 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7430 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7431 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007432 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007433 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007434 if (hist_ptr >= MAX_HDR_HISTORY)
7435 hist_ptr -= MAX_HDR_HISTORY;
7436 *vptr = ptr_hist[hist_ptr];
7437 *vlen = len_hist[hist_ptr];
7438 return 1;
7439}
7440
Willy Tarreaubaaee002006-06-26 02:48:02 +02007441/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007442 * Print a debug line with a header. Always stop at the first CR or LF char,
7443 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7444 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007445 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007446void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007447{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007448 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007449 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007450
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007451 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007452 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007453 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007454 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 +02007455
7456 for (max = 0; start + max < end; max++)
7457 if (start[max] == '\r' || start[max] == '\n')
7458 break;
7459
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007460 UBOUND(max, trash.size - trash.data - 3);
7461 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7462 trash.area[trash.data++] = '\n';
7463 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007464}
7465
Willy Tarreaueee5b512015-04-03 23:46:31 +02007466
7467/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7468 * The hdr_idx is allocated as well. In case of allocation failure, everything
7469 * allocated is freed and NULL is returned. Otherwise the new transaction is
7470 * assigned to the stream and returned.
7471 */
7472struct http_txn *http_alloc_txn(struct stream *s)
7473{
7474 struct http_txn *txn = s->txn;
7475
7476 if (txn)
7477 return txn;
7478
Willy Tarreaubafbe012017-11-24 17:34:44 +01007479 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007480 if (!txn)
7481 return txn;
7482
7483 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007484 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007485 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007486 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007487 return NULL;
7488 }
7489
7490 s->txn = txn;
7491 return txn;
7492}
7493
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007494void http_txn_reset_req(struct http_txn *txn)
7495{
7496 txn->req.flags = 0;
7497 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7498 txn->req.next = 0;
7499 txn->req.chunk_len = 0LL;
7500 txn->req.body_len = 0LL;
7501 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7502}
7503
7504void http_txn_reset_res(struct http_txn *txn)
7505{
7506 txn->rsp.flags = 0;
7507 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7508 txn->rsp.next = 0;
7509 txn->rsp.chunk_len = 0LL;
7510 txn->rsp.body_len = 0LL;
7511 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7512}
7513
Willy Tarreau0937bc42009-12-22 15:03:09 +01007514/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007515 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007516 * the required fields are properly allocated and that we only need to (re)init
7517 * them. This should be used before processing any new request.
7518 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007519void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007520{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007521 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007522 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007523
7524 txn->flags = 0;
7525 txn->status = -1;
7526
Willy Tarreauf64d1412010-10-07 20:06:11 +02007527 txn->cookie_first_date = 0;
7528 txn->cookie_last_date = 0;
7529
Willy Tarreaueee5b512015-04-03 23:46:31 +02007530 txn->srv_cookie = NULL;
7531 txn->cli_cookie = NULL;
7532 txn->uri = NULL;
7533
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007534 http_txn_reset_req(txn);
7535 http_txn_reset_res(txn);
7536
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007537 txn->req.chn = &s->req;
7538 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007539
7540 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007541
7542 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7543 if (fe->options2 & PR_O2_REQBUG_OK)
7544 txn->req.err_pos = -1; /* let buggy requests pass */
7545
Willy Tarreau0937bc42009-12-22 15:03:09 +01007546 if (txn->hdr_idx.v)
7547 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007548
7549 vars_init(&s->vars_txn, SCOPE_TXN);
7550 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007551}
7552
7553/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007554void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007555{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007556 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007557 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007558
7559 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007560 pool_free(pool_head_requri, txn->uri);
7561 pool_free(pool_head_capture, txn->cli_cookie);
7562 pool_free(pool_head_capture, txn->srv_cookie);
7563 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007564
William Lallemanda73203e2012-03-12 12:48:57 +01007565 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007566 txn->uri = NULL;
7567 txn->srv_cookie = NULL;
7568 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007569
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007570 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007571 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007572 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007573 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007574 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007575 }
7576
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007577 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007578 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007579 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007580 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007581 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007582 }
7583
Willy Tarreau6204cd92016-03-10 16:33:04 +01007584 vars_prune(&s->vars_txn, s->sess, s);
7585 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007586}
7587
7588/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007589void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007590{
7591 http_end_txn(s);
7592 http_init_txn(s);
7593
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007594 /* reinitialise the current rule list pointer to NULL. We are sure that
7595 * any rulelist match the NULL pointer.
7596 */
7597 s->current_rule_list = NULL;
7598
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007599 s->be = strm_fe(s);
7600 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007601 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007602 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007603 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007604 /* re-init store persistence */
7605 s->store_count = 0;
Willy Tarreau90a7c032018-09-05 16:21:29 +02007606 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007607
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007608 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007609
Willy Tarreau739cfba2010-01-25 23:11:14 +01007610 /* We must trim any excess data from the response buffer, because we
7611 * may have blocked an invalid response from a server that we don't
7612 * want to accidentely forward once we disable the analysers, nor do
7613 * we want those data to come along with next response. A typical
7614 * example of such data would be from a buggy server responding to
7615 * a HEAD with some data, or sending more than the advertised
7616 * content-length.
7617 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007618 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007619 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007620
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007621 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007622 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007623
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007624 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007625 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007626
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007627 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007628 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007629
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007630 s->req.rex = TICK_ETERNITY;
7631 s->req.wex = TICK_ETERNITY;
7632 s->req.analyse_exp = TICK_ETERNITY;
7633 s->res.rex = TICK_ETERNITY;
7634 s->res.wex = TICK_ETERNITY;
7635 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007636 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007637}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007638
Willy Tarreau79e57332018-10-02 16:01:16 +02007639/* This function executes one of the set-{method,path,query,uri} actions. It
7640 * takes the string from the variable 'replace' with length 'len', then modifies
7641 * the relevant part of the request line accordingly. Then it updates various
7642 * pointers to the next elements which were moved, and the total buffer length.
7643 * It finds the action to be performed in p[2], previously filled by function
7644 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7645 * error, though this can be revisited when this code is finally exploited.
7646 *
7647 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7648 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007649 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007650 * In query string case, the mark question '?' must be set at the start of the
7651 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007652 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007653int http_replace_req_line(int action, const char *replace, int len,
7654 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007655{
Willy Tarreau79e57332018-10-02 16:01:16 +02007656 struct http_txn *txn = s->txn;
7657 char *cur_ptr, *cur_end;
7658 int offset = 0;
7659 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007660
Willy Tarreau79e57332018-10-02 16:01:16 +02007661 switch (action) {
7662 case 0: // method
7663 cur_ptr = ci_head(&s->req);
7664 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007665
Willy Tarreau79e57332018-10-02 16:01:16 +02007666 /* adjust req line offsets and lengths */
7667 delta = len - offset - (cur_end - cur_ptr);
7668 txn->req.sl.rq.m_l += delta;
7669 txn->req.sl.rq.u += delta;
7670 txn->req.sl.rq.v += delta;
7671 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007672
Willy Tarreau79e57332018-10-02 16:01:16 +02007673 case 1: // path
7674 cur_ptr = http_txn_get_path(txn);
7675 if (!cur_ptr)
7676 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007677
Willy Tarreau79e57332018-10-02 16:01:16 +02007678 cur_end = cur_ptr;
7679 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7680 cur_end++;
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 case 2: // query
7689 offset = 1;
7690 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7691 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7692 while (cur_ptr < cur_end && *cur_ptr != '?')
7693 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007694
Willy Tarreau79e57332018-10-02 16:01:16 +02007695 /* skip the question mark or indicate that we must insert it
7696 * (but only if the format string is not empty then).
7697 */
7698 if (cur_ptr < cur_end)
7699 cur_ptr++;
7700 else if (len > 1)
7701 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007702
Willy Tarreau79e57332018-10-02 16:01:16 +02007703 /* adjust req line offsets and lengths */
7704 delta = len - offset - (cur_end - cur_ptr);
7705 txn->req.sl.rq.u_l += delta;
7706 txn->req.sl.rq.v += delta;
7707 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007708
Willy Tarreau79e57332018-10-02 16:01:16 +02007709 case 3: // uri
7710 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7711 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007712
Willy Tarreau79e57332018-10-02 16:01:16 +02007713 /* adjust req line offsets and lengths */
7714 delta = len - offset - (cur_end - cur_ptr);
7715 txn->req.sl.rq.u_l += delta;
7716 txn->req.sl.rq.v += delta;
7717 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007718
Willy Tarreau79e57332018-10-02 16:01:16 +02007719 default:
7720 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007721 }
7722
Willy Tarreau79e57332018-10-02 16:01:16 +02007723 /* commit changes and adjust end of message */
7724 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7725 txn->req.sl.rq.l += delta;
7726 txn->hdr_idx.v[0].len += delta;
7727 http_msg_move_end(&txn->req, delta);
7728 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007729}
7730
Willy Tarreau79e57332018-10-02 16:01:16 +02007731/* This function replace the HTTP status code and the associated message. The
7732 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007733 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007734void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007735{
Willy Tarreau79e57332018-10-02 16:01:16 +02007736 struct http_txn *txn = s->txn;
7737 char *cur_ptr, *cur_end;
7738 int delta;
7739 char *res;
7740 int c_l;
7741 const char *msg = reason;
7742 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007743
Willy Tarreau79e57332018-10-02 16:01:16 +02007744 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007745
Willy Tarreau79e57332018-10-02 16:01:16 +02007746 res = ultoa_o(status, trash.area, trash.size);
7747 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007748
Willy Tarreau79e57332018-10-02 16:01:16 +02007749 trash.area[c_l] = ' ';
7750 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007751
Willy Tarreau79e57332018-10-02 16:01:16 +02007752 /* Do we have a custom reason format string? */
7753 if (msg == NULL)
7754 msg = http_get_reason(status);
7755 msg_len = strlen(msg);
7756 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7757 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007758
Willy Tarreau79e57332018-10-02 16:01:16 +02007759 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7760 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007761
Willy Tarreau79e57332018-10-02 16:01:16 +02007762 /* commit changes and adjust message */
7763 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7764 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007765
Willy Tarreau79e57332018-10-02 16:01:16 +02007766 /* adjust res line offsets and lengths */
7767 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7768 txn->rsp.sl.st.c_l = c_l;
7769 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007770
Willy Tarreau79e57332018-10-02 16:01:16 +02007771 delta = trash.data - (cur_end - cur_ptr);
7772 txn->rsp.sl.st.l += delta;
7773 txn->hdr_idx.v[0].len += delta;
7774 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007775}
7776
Willy Tarreau8797c062007-05-07 00:55:35 +02007777__attribute__((constructor))
7778static void __http_protocol_init(void)
7779{
Willy Tarreau8797c062007-05-07 00:55:35 +02007780}
7781
7782
Willy Tarreau58f10d72006-12-04 02:26:12 +01007783/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007784 * Local variables:
7785 * c-indent-level: 8
7786 * c-basic-offset: 8
7787 * End:
7788 */