blob: 4361d6db564a8cc324d630576ed0c99c33eccd4e [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010026#include <common/base64.h>
Frédéric Lécaillea41d5312018-01-29 12:05:07 +010027#include <common/cfgparse.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020028#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020029#include <common/compat.h>
30#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010031#include <common/debug.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010032#include <common/h1.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010042#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020043#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <types/global.h>
William Lallemand71bd11a2017-11-20 19:13:14 +010045#include <types/cache.h>
William Lallemand9ed62032016-11-21 17:49:11 +010046#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Willy Tarreau8797c062007-05-07 00:55:35 +020048#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020049#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020050#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010051#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020053#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010054#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010055#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020056#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010057#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020059#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020060#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010062#include <proto/hdr_idx.h>
Patrick Hemmere3faf022018-08-22 10:02:00 -040063#include <proto/hlua.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010064#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020065#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010067#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020069#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010070#include <proto/server.h>
Olivier Houchard985f1392018-11-30 17:31:52 +010071#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020072#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010073#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020074#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020075#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020076#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020077
Cyril Bonté19979e12012-04-04 12:57:21 +020078/* status codes available for the stats admin page (strictly 4 chars length) */
79const char *stat_status_codes[STAT_STATUS_SIZE] = {
80 [STAT_STATUS_DENY] = "DENY",
81 [STAT_STATUS_DONE] = "DONE",
82 [STAT_STATUS_ERRP] = "ERRP",
83 [STAT_STATUS_EXCD] = "EXCD",
84 [STAT_STATUS_NONE] = "NONE",
85 [STAT_STATUS_PART] = "PART",
86 [STAT_STATUS_UNKN] = "UNKN",
87};
88
Willy Tarreau2d3d94c2008-11-30 20:20:08 +010089/* This function handles a server error at the stream interface level. The
90 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +010091 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +010092 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +010093 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +020094 */
Willy Tarreau87b09662015-04-03 00:22:06 +020095static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau83061a82018-07-13 11:56:34 +020096 int err, int finst, const struct buffer *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +020097{
Christopher Faulet9768c262018-10-22 09:34:31 +020098 if (IS_HTX_STRM(s))
99 return htx_server_error(s, si, err, finst, msg);
100
Willy Tarreau2019f952017-03-14 11:07:31 +0100101 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100102 channel_auto_read(si_oc(si));
103 channel_abort(si_oc(si));
104 channel_auto_close(si_oc(si));
105 channel_erase(si_oc(si));
106 channel_auto_close(si_ic(si));
107 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200108 if (msg)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200109 co_inject(si_ic(si), msg->area, msg->data);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200110 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200111 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200112 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200113 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114}
115
Willy Tarreau87b09662015-04-03 00:22:06 +0200116/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100117 * and message.
118 */
119
Willy Tarreau83061a82018-07-13 11:56:34 +0200120struct buffer *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100121{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200122 const int msgnum = http_get_status_idx(s->txn->status);
123
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100124 if (IS_HTX_STRM(s))
125 return htx_error_message(s);
126
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200127 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200128 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200129 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200130 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100131 else
132 return &http_err_chunks[msgnum];
133}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200134
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100135void
Willy Tarreau83061a82018-07-13 11:56:34 +0200136http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100137{
Christopher Faulet9768c262018-10-22 09:34:31 +0200138 if (IS_HTX_STRM(s))
139 return htx_reply_and_close(s, status, msg);
140
Christopher Fauletd7c91962015-04-30 11:48:27 +0200141 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100142 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100143 si_retnclose(&s->si[0], msg);
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100144}
145
Willy Tarreau21d2af32008-02-14 20:25:24 +0100146/* Parse the URI from the given transaction (which is assumed to be in request
147 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
148 * It is returned otherwise.
149 */
Willy Tarreau6b952c82018-09-10 17:45:34 +0200150char *http_txn_get_path(const struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100151{
Willy Tarreau6b952c82018-09-10 17:45:34 +0200152 struct ist ret;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100153
Willy Tarreau9d9ccdb2018-10-28 20:13:12 +0100154 if (!txn->req.chn->buf.size)
155 return NULL;
156
Willy Tarreau6b952c82018-09-10 17:45:34 +0200157 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 +0100158
Willy Tarreau6b952c82018-09-10 17:45:34 +0200159 return ret.ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +0100160}
161
Willy Tarreau71241ab2012-12-27 11:30:54 +0100162/* Returns a 302 for a redirectable request that reaches a server working in
163 * in redirect mode. This may only be called just after the stream interface
164 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
165 * follow normal proxy processing. NOTE: this function is designed to support
166 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100167 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200168void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100169{
170 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100171 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100172 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200173 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100174
Christopher Fauletfefc73d2018-10-24 21:18:04 +0200175 if (IS_HTX_STRM(s))
176 return htx_perform_server_redirect(s, si);
177
Willy Tarreauefb453c2008-10-26 20:49:47 +0100178 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200179 trash.data = strlen(HTTP_302);
180 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100181
Willy Tarreaub05e48a2018-09-20 11:12:58 +0200182 srv = __objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100183
Willy Tarreauefb453c2008-10-26 20:49:47 +0100184 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200185 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100186 return;
187
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100188 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100189 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200190 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
191 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100192 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100193
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200194 /* 3: add the request URI. Since it was already forwarded, we need
195 * to temporarily rewind the buffer.
196 */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200197 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200198 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200199
Willy Tarreau6b952c82018-09-10 17:45:34 +0200200 path = http_txn_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200201 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 +0200202
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200203 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200204
Willy Tarreauefb453c2008-10-26 20:49:47 +0100205 if (!path)
206 return;
207
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200208 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100209 return;
210
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200211 memcpy(trash.area + trash.data, path, len);
212 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100213
214 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200215 memcpy(trash.area + trash.data,
216 "\r\nProxy-Connection: close\r\n\r\n", 29);
217 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100218 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200219 memcpy(trash.area + trash.data,
220 "\r\nConnection: close\r\n\r\n", 23);
221 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100222 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100223
224 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200225 si_shutr(si);
226 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100227 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100228 si->state = SI_ST_CLO;
229
230 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +0100231 txn->status = 302;
232 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100233
234 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100235 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500236 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100237}
238
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100239/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100240 * that the server side is closed. Note that err_type is actually a
241 * bitmask, where almost only aborts may be cumulated with other
242 * values. We consider that aborted operations are more important
243 * than timeouts or errors due to the fact that nobody else in the
244 * logs might explain incomplete retries. All others should avoid
245 * being cumulated. It should normally not be possible to have multiple
246 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100247 * Note that connection errors appearing on the second request of a keep-alive
248 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100249 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200250void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100251{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100252 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100253
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200254 /* set s->txn->status for http_error_message(s) */
255 s->txn->status = 503;
256
Willy Tarreauefb453c2008-10-26 20:49:47 +0100257 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200258 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100259 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100260 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200261 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100262 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200263 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100264 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200265 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100266 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100267 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200268 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100269 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100270 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200271 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100272 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200273 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100274 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200275 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100276 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200277 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200278 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200279 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100280 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200281 http_error_message(s));
282 else { /* SI_ET_CONN_OTHER and others */
283 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200284 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100285 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200286 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100287}
288
Willy Tarreau42250582007-04-01 01:30:43 +0200289extern const char sess_term_cond[8];
290extern const char sess_fin_state[8];
291extern const char *monthname[12];
Willy Tarreau8ceae722018-11-26 11:58:30 +0100292
293DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
294DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
295
296struct pool_head *pool_head_requri = NULL;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100297struct pool_head *pool_head_capture = NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100298
Willy Tarreau117f59e2007-03-04 18:17:17 +0100299/*
300 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +0200301 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +0100302 */
Christopher Faulet10079f52018-10-03 15:17:28 +0200303void http_capture_headers(char *som, struct hdr_idx *idx,
304 char **cap, struct cap_hdr *cap_hdr)
Willy Tarreau117f59e2007-03-04 18:17:17 +0100305{
306 char *eol, *sol, *col, *sov;
307 int cur_idx;
308 struct cap_hdr *h;
309 int len;
310
311 sol = som + hdr_idx_first_pos(idx);
312 cur_idx = hdr_idx_first_idx(idx);
313
314 while (cur_idx) {
315 eol = sol + idx->v[cur_idx].len;
316
317 col = sol;
318 while (col < eol && *col != ':')
319 col++;
320
321 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100322 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +0100323 sov++;
324
325 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +0200326 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +0100327 (strncasecmp(sol, h->name, h->namelen) == 0)) {
328 if (cap[h->index] == NULL)
329 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +0100330 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100331
332 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100333 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +0100334 continue;
335 }
336
337 len = eol - sov;
338 if (len > h->len)
339 len = h->len;
340
341 memcpy(cap[h->index], sov, len);
342 cap[h->index][len]=0;
343 }
344 }
345 sol = eol + idx->v[cur_idx].cr + 1;
346 cur_idx = idx->v[cur_idx].next;
347 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100348}
349
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200350/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
351 * conversion succeeded, 0 in case of error. If the request was already 1.X,
352 * nothing is done and 1 is returned.
353 */
Willy Tarreau79e57332018-10-02 16:01:16 +0200354int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200355{
356 int delta;
357 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100358 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200359
360 if (msg->sl.rq.v_l != 0)
361 return 1;
362
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300363 /* RFC 1945 allows only GET for HTTP/0.9 requests */
364 if (txn->meth != HTTP_METH_GET)
365 return 0;
366
Willy Tarreauf37954d2018-06-15 18:31:02 +0200367 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200368
369 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300370 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
371 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200372 }
373 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200374 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +0100375 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200376 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200377 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200378 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +0200379 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200380 NULL, NULL);
381 if (unlikely(!cur_end))
382 return 0;
383
384 /* we have a full HTTP/1.0 request now and we know that
385 * we have either a CR or an LF at <ptr>.
386 */
387 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
388 return 1;
389}
390
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100391/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100392 * and "keep-alive" values. If we already know that some headers may safely
393 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100394 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
395 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +0100396 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100397 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
398 * found, and TX_CON_*_SET is adjusted depending on what is left so only
399 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100400 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +0100401 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100402void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +0100403{
Willy Tarreau5b154472009-12-21 20:11:07 +0100404 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100405 const char *hdr_val = "Connection";
406 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +0100407
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100408 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +0100409 return;
410
Willy Tarreau88d349d2010-01-25 12:15:43 +0100411 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
412 hdr_val = "Proxy-Connection";
413 hdr_len = 16;
414 }
415
Willy Tarreau5b154472009-12-21 20:11:07 +0100416 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100417 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200418 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100419 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
420 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100421 if (to_del & 2)
422 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100423 else
424 txn->flags |= TX_CON_KAL_SET;
425 }
426 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
427 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100428 if (to_del & 1)
429 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100430 else
431 txn->flags |= TX_CON_CLO_SET;
432 }
Willy Tarreau50fc7772012-11-11 22:19:57 +0100433 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
434 txn->flags |= TX_HDR_CONN_UPG;
435 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100436 }
437
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100438 txn->flags |= TX_HDR_CONN_PRS;
439 return;
440}
Willy Tarreau5b154472009-12-21 20:11:07 +0100441
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100442/* Apply desired changes on the Connection: header. Values may be removed and/or
443 * added depending on the <wanted> flags, which are exclusively composed of
444 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
445 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
446 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100447void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100448{
449 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100450 const char *hdr_val = "Connection";
451 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100452
453 ctx.idx = 0;
454
Willy Tarreau88d349d2010-01-25 12:15:43 +0100455
456 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
457 hdr_val = "Proxy-Connection";
458 hdr_len = 16;
459 }
460
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100461 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200462 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100463 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
464 if (wanted & TX_CON_KAL_SET)
465 txn->flags |= TX_CON_KAL_SET;
466 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100467 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +0100468 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100469 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
470 if (wanted & TX_CON_CLO_SET)
471 txn->flags |= TX_CON_CLO_SET;
472 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100473 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +0100474 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100475 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100476
477 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
478 return;
479
480 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
481 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100482 hdr_val = "Connection: close";
483 hdr_len = 17;
484 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
485 hdr_val = "Proxy-Connection: close";
486 hdr_len = 23;
487 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100488 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100489 }
490
491 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
492 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100493 hdr_val = "Connection: keep-alive";
494 hdr_len = 22;
495 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
496 hdr_val = "Proxy-Connection: keep-alive";
497 hdr_len = 28;
498 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100499 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100500 }
501 return;
Willy Tarreau5b154472009-12-21 20:11:07 +0100502}
503
Willy Tarreau87b09662015-04-03 00:22:06 +0200504void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200505{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200506 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200507 int tmp = TX_CON_WANT_KAL;
508
Christopher Fauletf2824e62018-10-01 12:12:37 +0200509 if (IS_HTX_STRM(s))
Christopher Faulet0f226952018-10-22 09:29:56 +0200510 return htx_adjust_conn_mode(s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200511
Christopher Faulet315b39c2018-09-21 16:26:19 +0200512 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
513 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
514 tmp = TX_CON_WANT_TUN;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200515
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200516 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Faulet315b39c2018-09-21 16:26:19 +0200517 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200518 tmp = TX_CON_WANT_SCL;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200519
Christopher Faulet315b39c2018-09-21 16:26:19 +0200520 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
521 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200522 tmp = TX_CON_WANT_CLO;
523
524 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
525 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
526
527 if (!(txn->flags & TX_HDR_CONN_PRS) &&
528 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
529 /* parse the Connection header and possibly clean it */
530 int to_del = 0;
531 if ((msg->flags & HTTP_MSGF_VER_11) ||
532 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200533 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200534 to_del |= 2; /* remove "keep-alive" */
535 if (!(msg->flags & HTTP_MSGF_VER_11))
536 to_del |= 1; /* remove "close" */
537 http_parse_connection_header(txn, msg, to_del);
538 }
539
540 /* check if client or config asks for explicit close in KAL/SCL */
541 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
542 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
543 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
544 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
545 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200546 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200547 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
548}
William Lallemand82fe75c2012-10-23 10:25:10 +0200549
Willy Tarreaud787e662009-07-07 10:14:51 +0200550/* This stream analyser waits for a complete HTTP request. It returns 1 if the
551 * processing can continue on next analysers, or zero if it either needs more
552 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100553 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200554 * when it has nothing left to do, and may remove any analyser when it wants to
555 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100556 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200557int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100558{
Willy Tarreau59234e92008-11-30 23:51:27 +0100559 /*
560 * We will parse the partial (or complete) lines.
561 * We will check the request syntax, and also join multi-line
562 * headers. An index of all the lines will be elaborated while
563 * parsing.
564 *
565 * For the parsing, we use a 28 states FSM.
566 *
567 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200568 * ci_head(req) = beginning of request
569 * ci_head(req) + msg->eoh = end of processed headers / start of current one
570 * ci_tail(req) = end of input data
571 * msg->eol = end of current header or line (LF or CRLF)
572 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200573 *
574 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200575 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200576 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
577 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100578 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100579
Willy Tarreau59234e92008-11-30 23:51:27 +0100580 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200581 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200582 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100583 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200584 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100585
Christopher Faulete0768eb2018-10-03 16:38:02 +0200586 if (IS_HTX_STRM(s))
587 return htx_wait_for_request(s, req, an_bit);
588
Christopher Faulet45073512018-07-20 10:16:29 +0200589 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 +0100590 now_ms, __FUNCTION__,
591 s,
592 req,
593 req->rex, req->wex,
594 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200595 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100596 req->analysers);
597
Willy Tarreau52a0c602009-08-16 22:45:38 +0200598 /* we're speaking HTTP here, so let's speak HTTP to the client */
599 s->srv_error = http_return_srv_error;
600
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300601 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200602 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300603 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
604
Willy Tarreau83e3af02009-12-28 17:39:57 +0100605 /* There's a protected area at the end of the buffer for rewriting
606 * purposes. We don't want to start to parse the request if the
607 * protected area is affected, because we may have to move processed
608 * data later, which is much more complicated.
609 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200610 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200611 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100612 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200613 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100614 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100615 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200616 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200617 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100618 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100619 return 0;
620 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200621 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200622 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200623 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100624 }
625
Willy Tarreauf37954d2018-06-15 18:31:02 +0200626 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100627 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100628 }
629
Willy Tarreau59234e92008-11-30 23:51:27 +0100630 /* 1: we might have to print this header in debug mode */
631 if (unlikely((global.mode & MODE_DEBUG) &&
632 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200633 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100634 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100635
Willy Tarreauf37954d2018-06-15 18:31:02 +0200636 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200637 /* this is a bit complex : in case of error on the request line,
638 * we know that rq.l is still zero, so we display only the part
639 * up to the end of the line (truncated by debug_hdr).
640 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200641 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100642 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100643
Willy Tarreau59234e92008-11-30 23:51:27 +0100644 sol += hdr_idx_first_pos(&txn->hdr_idx);
645 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100646
Willy Tarreau59234e92008-11-30 23:51:27 +0100647 while (cur_idx) {
648 eol = sol + txn->hdr_idx.v[cur_idx].len;
649 debug_hdr("clihdr", s, sol, eol);
650 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
651 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100652 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100653 }
654
Willy Tarreau58f10d72006-12-04 02:26:12 +0100655
Willy Tarreau59234e92008-11-30 23:51:27 +0100656 /*
657 * Now we quickly check if we have found a full valid request.
658 * If not so, we check the FD and buffer states before leaving.
659 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100660 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100661 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200662 * on a keep-alive stream, if we encounter and error, close, t/o,
663 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100664 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200665 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200666 * Last, we may increase some tracked counters' http request errors on
667 * the cases that are deliberately the client's fault. For instance,
668 * a timeout or connection reset is not counted as an error. However
669 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100670 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100671
Willy Tarreau655dce92009-11-08 13:10:58 +0100672 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100673 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100674 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100675 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100676 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200677 stream_inc_http_req_ctr(s);
678 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200679 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100680 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100681 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100682
Willy Tarreau59234e92008-11-30 23:51:27 +0100683 /* 1: Since we are in header mode, if there's no space
684 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200685 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100686 * must terminate it now.
687 */
Willy Tarreau23752332018-06-15 14:54:53 +0200688 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100689 /* FIXME: check if URI is set and return Status
690 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100691 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200692 stream_inc_http_req_ctr(s);
693 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200694 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +0200695 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +0200696 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +0100697 goto return_bad_req;
698 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100699
Willy Tarreau59234e92008-11-30 23:51:27 +0100700 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200701 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200702 if (!(s->flags & SF_ERR_MASK))
703 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100704
Willy Tarreaufcffa692010-01-10 14:21:19 +0100705 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100706 goto failed_keep_alive;
707
Willy Tarreau0f228a02015-05-01 15:37:53 +0200708 if (sess->fe->options & PR_O_IGNORE_PRB)
709 goto failed_keep_alive;
710
Willy Tarreau59234e92008-11-30 23:51:27 +0100711 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +0200712 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200713 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +0200714 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200715 }
716
Willy Tarreaudc979f22012-12-04 10:39:01 +0100717 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +0100718 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +0100719 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100720 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +0100721 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +0200722 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200723 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100724 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200725 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100726 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200727
Willy Tarreaue7dff022015-04-03 01:14:29 +0200728 if (!(s->flags & SF_FINST_MASK))
729 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +0100730 return 0;
731 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200732
Willy Tarreau59234e92008-11-30 23:51:27 +0100733 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200734 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200735 if (!(s->flags & SF_ERR_MASK))
736 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100737
Willy Tarreaufcffa692010-01-10 14:21:19 +0100738 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100739 goto failed_keep_alive;
740
Willy Tarreau0f228a02015-05-01 15:37:53 +0200741 if (sess->fe->options & PR_O_IGNORE_PRB)
742 goto failed_keep_alive;
743
Willy Tarreau59234e92008-11-30 23:51:27 +0100744 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +0200745 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200746 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +0200747 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200748 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100749 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +0100750 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +0100751 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200752 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +0100753 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200754
Willy Tarreau87b09662015-04-03 00:22:06 +0200755 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200756 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100757 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200758 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100759 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200760
Willy Tarreaue7dff022015-04-03 01:14:29 +0200761 if (!(s->flags & SF_FINST_MASK))
762 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +0100763 return 0;
764 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200765
Willy Tarreau59234e92008-11-30 23:51:27 +0100766 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200767 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200768 if (!(s->flags & SF_ERR_MASK))
769 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100770
Willy Tarreaufcffa692010-01-10 14:21:19 +0100771 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100772 goto failed_keep_alive;
773
Willy Tarreau0f228a02015-05-01 15:37:53 +0200774 if (sess->fe->options & PR_O_IGNORE_PRB)
775 goto failed_keep_alive;
776
Willy Tarreau4076a152009-04-02 15:18:36 +0200777 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200778 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100779 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +0100780 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +0100781 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200782 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +0100783 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +0200784 stream_inc_http_err_ctr(s);
785 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200786 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100787 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200788 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100789 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200790
Willy Tarreaue7dff022015-04-03 01:14:29 +0200791 if (!(s->flags & SF_FINST_MASK))
792 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +0200793 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100794 }
795
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200796 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200797 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100798 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100799
800 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req)) {
Willy Tarreau5e205522011-12-17 16:34:27 +0100801 /* We need more data, we have to re-enable quick-ack in case we
802 * previously disabled it, otherwise we might cause the client
803 * to delay next data.
804 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100805 conn_set_quickack(objt_conn(sess->origin), 1);
Willy Tarreau5e205522011-12-17 16:34:27 +0100806 }
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100807
Willy Tarreaufcffa692010-01-10 14:21:19 +0100808 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
809 /* If the client starts to talk, let's fall back to
810 * request timeout processing.
811 */
812 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +0100813 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +0100814 }
815
Willy Tarreau59234e92008-11-30 23:51:27 +0100816 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +0100817 if (!tick_isset(req->analyse_exp)) {
818 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
819 (txn->flags & TX_WAIT_NEXT_RQ) &&
820 tick_isset(s->be->timeout.httpka))
821 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
822 else
823 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
824 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100825
Willy Tarreau59234e92008-11-30 23:51:27 +0100826 /* we're not ready yet */
827 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +0100828
829 failed_keep_alive:
830 /* Here we process low-level errors for keep-alive requests. In
831 * short, if the request is not the first one and it experiences
832 * a timeout, read error or shutdown, we just silently close so
833 * that the client can try again.
834 */
835 txn->status = 0;
836 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +0100837 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +0100838 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +0200839 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100840 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100841 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +0100842 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +0100843 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100844
Willy Tarreaud787e662009-07-07 10:14:51 +0200845 /* OK now we have a complete HTTP request with indexed headers. Let's
846 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200847 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +0100848 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +0100849 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +0100850 * byte after the last LF. msg->sov points to the first byte of data.
851 * msg->eol cannot be trusted because it may have been left uninitialized
852 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +0200853 */
Willy Tarreau9cdde232007-05-02 20:58:19 +0200854
Willy Tarreau87b09662015-04-03 00:22:06 +0200855 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200856 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +0100857
Willy Tarreaub16a5742010-01-10 14:46:16 +0100858 if (txn->flags & TX_WAIT_NEXT_RQ) {
859 /* kill the pending keep-alive timeout */
860 txn->flags &= ~TX_WAIT_NEXT_RQ;
861 req->analyse_exp = TICK_ETERNITY;
862 }
863
864
Willy Tarreaud787e662009-07-07 10:14:51 +0200865 /* Maybe we found in invalid header name while we were configured not
866 * to block on that, so we have to capture it now.
867 */
868 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200869 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +0200870
Willy Tarreau59234e92008-11-30 23:51:27 +0100871 /*
872 * 1: identify the method
873 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200874 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +0100875
876 /* we can make use of server redirect on GET and HEAD */
877 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200878 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +0100879 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +0200880 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +0100881 /* PRI is reserved for the HTTP/2 preface */
882 msg->err_pos = 0;
883 goto return_bad_req;
884 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200885
Willy Tarreau59234e92008-11-30 23:51:27 +0100886 /*
887 * 2: check if the URI matches the monitor_uri.
888 * We have to do this for every request which gets in, because
889 * the monitor-uri is defined by the frontend.
890 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200891 if (unlikely((sess->fe->monitor_uri_len != 0) &&
892 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +0200893 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200894 sess->fe->monitor_uri,
895 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100896 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100897 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +0100898 */
Willy Tarreau59234e92008-11-30 23:51:27 +0100899 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +0100900
Willy Tarreaue7dff022015-04-03 01:14:29 +0200901 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100902 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +0100903
Willy Tarreau59234e92008-11-30 23:51:27 +0100904 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200905 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +0200906 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +0200907
Willy Tarreau59234e92008-11-30 23:51:27 +0100908 ret = acl_pass(ret);
909 if (cond->pol == ACL_COND_UNLESS)
910 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +0100911
Willy Tarreau59234e92008-11-30 23:51:27 +0100912 if (ret) {
913 /* we fail this request, let's return 503 service unavail */
914 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200915 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +0200916 if (!(s->flags & SF_ERR_MASK))
917 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +0100918 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +0100919 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100920 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100921
Joseph Herlant5ba80252018-11-15 09:25:36 -0800922 /* nothing to fail, let's reply normally */
Willy Tarreau59234e92008-11-30 23:51:27 +0100923 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200924 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +0200925 if (!(s->flags & SF_ERR_MASK))
926 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +0100927 goto return_prx_cond;
928 }
929
930 /*
931 * 3: Maybe we have to copy the original REQURI for the logs ?
932 * Note: we cannot log anymore if the request has been
933 * classified as invalid.
934 */
935 if (unlikely(s->logs.logwait & LW_REQ)) {
936 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100937 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100938 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100939
Stéphane Cottin23e9e932017-05-18 08:58:41 +0200940 if (urilen >= global.tune.requri_len )
941 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +0200942 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +0100943 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100944
Willy Tarreaud79a3b22012-12-28 09:40:16 +0100945 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +0100946 s->do_log(s);
947 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100948 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100949 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100950 }
Willy Tarreau06619262006-12-17 08:37:22 +0100951
Willy Tarreau91852eb2015-05-01 13:26:00 +0200952 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
953 * exactly one digit "." one digit. This check may be disabled using
954 * option accept-invalid-http-request.
955 */
956 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
957 if (msg->sl.rq.v_l != 8) {
958 msg->err_pos = msg->sl.rq.v;
959 goto return_bad_req;
960 }
961
Willy Tarreauf37954d2018-06-15 18:31:02 +0200962 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
963 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
964 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
965 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +0200966 msg->err_pos = msg->sl.rq.v + 4;
967 goto return_bad_req;
968 }
969 }
Willy Tarreau13317662015-05-01 13:47:08 +0200970 else {
971 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
972 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
973 goto return_bad_req;
974 }
Willy Tarreau91852eb2015-05-01 13:26:00 +0200975
Willy Tarreau5b154472009-12-21 20:11:07 +0100976 /* ... and check if the request is HTTP/1.1 or above */
977 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +0200978 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
979 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
980 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +0100981 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +0100982
983 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +0100984 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 +0100985
Willy Tarreau88d349d2010-01-25 12:15:43 +0100986 /* if the frontend has "option http-use-proxy-header", we'll check if
987 * we have what looks like a proxied connection instead of a connection,
988 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
989 * Note that this is *not* RFC-compliant, however browsers and proxies
990 * happen to do that despite being non-standard :-(
991 * We consider that a request not beginning with either '/' or '*' is
992 * a proxied connection, which covers both "scheme://location" and
993 * CONNECT ip:port.
994 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200995 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +0200996 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +0100997 txn->flags |= TX_USE_PX_CONN;
998
Willy Tarreaue8e785b2009-12-26 15:34:26 +0100999 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001000 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001001
Willy Tarreau59234e92008-11-30 23:51:27 +01001002 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001003 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02001004 http_capture_headers(ci_head(req), &txn->hdr_idx,
1005 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001006
Willy Tarreau557f1992015-05-01 10:05:17 +02001007 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1008 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001009 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001010 * The length of a message body is determined by one of the following
1011 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001012 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001013 * 1. Any response to a HEAD request and any response with a 1xx
1014 * (Informational), 204 (No Content), or 304 (Not Modified) status
1015 * code is always terminated by the first empty line after the
1016 * header fields, regardless of the header fields present in the
1017 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001018 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001019 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1020 * the connection will become a tunnel immediately after the empty
1021 * line that concludes the header fields. A client MUST ignore any
1022 * Content-Length or Transfer-Encoding header fields received in
1023 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001024 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001025 * 3. If a Transfer-Encoding header field is present and the chunked
1026 * transfer coding (Section 4.1) is the final encoding, the message
1027 * body length is determined by reading and decoding the chunked
1028 * data until the transfer coding indicates the data is complete.
1029 *
1030 * If a Transfer-Encoding header field is present in a response and
1031 * the chunked transfer coding is not the final encoding, the
1032 * message body length is determined by reading the connection until
1033 * it is closed by the server. If a Transfer-Encoding header field
1034 * is present in a request and the chunked transfer coding is not
1035 * the final encoding, the message body length cannot be determined
1036 * reliably; the server MUST respond with the 400 (Bad Request)
1037 * status code and then close the connection.
1038 *
1039 * If a message is received with both a Transfer-Encoding and a
1040 * Content-Length header field, the Transfer-Encoding overrides the
1041 * Content-Length. Such a message might indicate an attempt to
1042 * perform request smuggling (Section 9.5) or response splitting
1043 * (Section 9.4) and ought to be handled as an error. A sender MUST
1044 * remove the received Content-Length field prior to forwarding such
1045 * a message downstream.
1046 *
1047 * 4. If a message is received without Transfer-Encoding and with
1048 * either multiple Content-Length header fields having differing
1049 * field-values or a single Content-Length header field having an
1050 * invalid value, then the message framing is invalid and the
1051 * recipient MUST treat it as an unrecoverable error. If this is a
1052 * request message, the server MUST respond with a 400 (Bad Request)
1053 * status code and then close the connection. If this is a response
1054 * message received by a proxy, the proxy MUST close the connection
1055 * to the server, discard the received response, and send a 502 (Bad
1056 * Gateway) response to the client. If this is a response message
1057 * received by a user agent, the user agent MUST close the
1058 * connection to the server and discard the received response.
1059 *
1060 * 5. If a valid Content-Length header field is present without
1061 * Transfer-Encoding, its decimal value defines the expected message
1062 * body length in octets. If the sender closes the connection or
1063 * the recipient times out before the indicated number of octets are
1064 * received, the recipient MUST consider the message to be
1065 * incomplete and close the connection.
1066 *
1067 * 6. If this is a request message and none of the above are true, then
1068 * the message body length is zero (no message body is present).
1069 *
1070 * 7. Otherwise, this is a response message without a declared message
1071 * body length, so the message body length is determined by the
1072 * number of octets received prior to the server closing the
1073 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001074 */
1075
Willy Tarreau32b47f42009-10-18 20:55:02 +02001076 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001077 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001078 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001079 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001080 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001081 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001082 /* chunked not last, return badreq */
1083 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001084 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001085 }
1086
Willy Tarreau1c913912015-04-30 10:57:51 +02001087 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001088 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001089 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001090 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001091 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1092 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001093 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001094 signed long long cl;
1095
Willy Tarreauad14f752011-09-02 20:33:27 +02001096 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001097 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001098 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001099 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001100
Willy Tarreauad14f752011-09-02 20:33:27 +02001101 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001102 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001103 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001104 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001105
Willy Tarreauad14f752011-09-02 20:33:27 +02001106 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001107 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001108 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001109 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001110
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001111 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001112 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001113 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001114 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001115
Christopher Fauletbe821b92017-03-30 11:21:53 +02001116 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001117 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001118 }
1119
Willy Tarreau34dfc602015-05-01 10:09:49 +02001120 /* even bodyless requests have a known length */
1121 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001122
Willy Tarreau179085c2014-04-28 16:48:56 +02001123 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1124 * only change if both the request and the config reference something else.
1125 * Option httpclose by itself sets tunnel mode where headers are mangled.
1126 * However, if another mode is set, it will affect it (eg: server-close/
1127 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1128 * if FE and BE have the same settings (common). The method consists in
1129 * checking if options changed between the two calls (implying that either
1130 * one is non-null, or one of them is non-null and we are there for the first
1131 * time.
1132 */
1133 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001134 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001135 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001136
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001137 /* we may have to wait for the request's body */
1138 if ((s->be->options & PR_O_WREQ_BODY) &&
1139 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1140 req->analysers |= AN_REQ_HTTP_BODY;
1141
Willy Tarreau83ece462017-12-21 15:13:09 +01001142 /*
1143 * RFC7234#4:
1144 * A cache MUST write through requests with methods
1145 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1146 * the origin server; i.e., a cache is not allowed
1147 * to generate a reply to such a request before
1148 * having forwarded the request and having received
1149 * a corresponding response.
1150 *
1151 * RFC7231#4.2.1:
1152 * Of the request methods defined by this
1153 * specification, the GET, HEAD, OPTIONS, and TRACE
1154 * methods are defined to be safe.
1155 */
1156 if (likely(txn->meth == HTTP_METH_GET ||
1157 txn->meth == HTTP_METH_HEAD ||
1158 txn->meth == HTTP_METH_OPTIONS ||
1159 txn->meth == HTTP_METH_TRACE))
1160 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1161
Willy Tarreaud787e662009-07-07 10:14:51 +02001162 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001163 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001164 req->analyse_exp = TICK_ETERNITY;
1165 return 1;
1166
1167 return_bad_req:
1168 /* We centralize bad requests processing here */
1169 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1170 /* we detected a parsing error. We want to archive this request
1171 * in the dedicated proxy area for later troubleshooting.
1172 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001173 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001174 }
1175
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001176 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001177 txn->req.msg_state = HTTP_MSG_ERROR;
1178 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001179 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001180
Olivier Houcharda798bf52019-03-08 18:52:00 +01001181 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001182 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001183 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001184
1185 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001186 if (!(s->flags & SF_ERR_MASK))
1187 s->flags |= SF_ERR_PRXCOND;
1188 if (!(s->flags & SF_FINST_MASK))
1189 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001190
Christopher Faulet0184ea72017-01-05 14:06:34 +01001191 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001192 req->analyse_exp = TICK_ETERNITY;
1193 return 0;
1194}
1195
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001196
Willy Tarreau347a35d2013-11-22 17:51:09 +01001197/* This function prepares an applet to handle the stats. It can deal with the
1198 * "100-continue" expectation, check that admin rules are met for POST requests,
1199 * and program a response message if something was unexpected. It cannot fail
1200 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001201 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001202 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001203 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001204 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001205int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001206{
1207 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001208 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001209 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001210 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001211 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001212 struct uri_auth *uri_auth = s->be->uri_auth;
1213 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001214 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001215
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001216 appctx = si_appctx(si);
1217 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1218 appctx->st1 = appctx->st2 = 0;
1219 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1220 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001221 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001222 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001223
Willy Tarreauf37954d2018-06-15 18:31:02 +02001224 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001225 lookup = uri + uri_auth->uri_len;
1226
1227 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1228 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001229 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001230 break;
1231 }
1232 }
1233
1234 if (uri_auth->refresh) {
1235 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1236 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001237 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001238 break;
1239 }
1240 }
1241 }
1242
1243 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1244 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001245 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001246 break;
1247 }
1248 }
1249
Willy Tarreau1e62df92016-01-11 18:57:53 +01001250 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1251 if (memcmp(h, ";typed", 6) == 0) {
1252 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1253 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1254 break;
1255 }
1256 }
1257
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001258 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1259 if (memcmp(h, ";st=", 4) == 0) {
1260 int i;
1261 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001262 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001263 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1264 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001265 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001266 break;
1267 }
1268 }
1269 break;
1270 }
1271 }
1272
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001273 appctx->ctx.stats.scope_str = 0;
1274 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001275 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1276 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1277 int itx = 0;
1278 const char *h2;
1279 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1280 const char *err;
1281
1282 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1283 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001284 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001285 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1286 itx++;
1287 h++;
1288 }
1289
1290 if (itx > STAT_SCOPE_TXT_MAXLEN)
1291 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001292 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001293
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001294 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001295 memcpy(scope_txt, h2, itx);
1296 scope_txt[itx] = '\0';
1297 err = invalid_char(scope_txt);
1298 if (err) {
1299 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001300 appctx->ctx.stats.scope_str = 0;
1301 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001302 }
1303 break;
1304 }
1305 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001306
1307 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001308 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001309 int ret = 1;
1310
1311 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001312 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 +01001313 ret = acl_pass(ret);
1314 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1315 ret = !ret;
1316 }
1317
1318 if (ret) {
1319 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001320 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001321 break;
1322 }
1323 }
1324
1325 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001326 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001327 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001328 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001329 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1330 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001331 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001332 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001333 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001334 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1335 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001336 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001337 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001338 else {
1339 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001340 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001341 }
1342
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001343 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001344 return 1;
1345}
1346
Willy Tarreau87b09662015-04-03 00:22:06 +02001347int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001348 const char* name, unsigned int name_len,
1349 const char *str, struct my_regex *re,
1350 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001351{
Willy Tarreaueee5b512015-04-03 23:46:31 +02001352 struct hdr_idx *idx = &s->txn->hdr_idx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001353 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001354
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001355 /* Choose the header browsing function. */
1356 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001357 case ACT_HTTP_REPLACE_VAL:
Willy Tarreauc5a4fd52018-12-11 11:42:27 +01001358 return http_legacy_replace_header(idx, msg, name, name_len, str, re, output);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001359 case ACT_HTTP_REPLACE_HDR:
Willy Tarreauc5a4fd52018-12-11 11:42:27 +01001360 return http_legacy_replace_full_header(idx, msg, name, name_len, str, re, output);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001361 default: /* impossible */
1362 return -1;
1363 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001364}
1365
Willy Tarreau87b09662015-04-03 00:22:06 +02001366static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001367 const char* name, unsigned int name_len,
1368 struct list *fmt, struct my_regex *re,
1369 int action)
1370{
Willy Tarreau83061a82018-07-13 11:56:34 +02001371 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001372 int ret = -1;
1373
1374 replace = alloc_trash_chunk();
1375 if (!replace)
1376 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001377
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001378 replace->data = build_logline(s, replace->area, replace->size, fmt);
1379 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001380 goto leave;
1381
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001382 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1383 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001384
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001385 leave:
1386 free_trash_chunk(replace);
1387 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001388}
1389
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001390/*
1391 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
1392 * built according to <fmt> log line format.
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001393 * If <early_hints> is NULL, it is allocated and the HTTP 103 response first
1394 * line is inserted before the header. If an error occurred <early_hints> is
1395 * released and NULL is returned. On success the updated buffer is returned.
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001396 */
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001397static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints,
1398 const char* name, unsigned int name_len,
1399 struct list *fmt)
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001400{
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001401 if (!early_hints) {
1402 early_hints = alloc_trash_chunk();
1403 if (!early_hints)
1404 goto fail;
1405 if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len))
1406 goto fail;
1407 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001408
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001409 if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2))
1410 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001411
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001412 early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt);
1413 if (!chunk_memcat(early_hints, "\r\n", 2))
1414 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001415
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001416 return early_hints;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001417
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001418 fail:
1419 free_trash_chunk(early_hints);
1420 return NULL;
1421}
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001422
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001423/* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the
1424 * response is added. If an error occurred or if another response was already
1425 * sent, this function does nothing.
1426 */
1427static void http_send_early_hints(struct stream *s, struct buffer *early_hints)
1428{
1429 struct channel *chn = s->txn->rsp.chn;
1430 char *cur_ptr = ci_head(chn);
1431 int ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001432
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001433 /* If a response was already sent, skip early hints */
1434 if (s->txn->status > 0)
1435 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001436
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001437 if (!chunk_memcat(early_hints, "\r\n", 2))
1438 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001439
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001440 ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints));
1441 c_adv(chn, ret);
1442 chn->total += ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001443}
1444
Willy Tarreau87b09662015-04-03 00:22:06 +02001445/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001446 * transaction <txn>. Returns the verdict of the first rule that prevents
1447 * further processing of the request (auth, deny, ...), and defaults to
1448 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1449 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001450 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1451 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1452 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001453 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001454enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001455http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001456{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001457 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001458 struct http_txn *txn = s->txn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001459 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001460 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001461 const char *auth_realm;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001462 struct buffer *early_hints = NULL;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001463 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001464 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001465 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001466
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001467 /* If "the current_rule_list" match the executed rule list, we are in
1468 * resume condition. If a resume is needed it is always in the action
1469 * and never in the ACL or converters. In this case, we initialise the
1470 * current rule, and go to the action execution point.
1471 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001472 if (s->current_rule) {
1473 rule = s->current_rule;
1474 s->current_rule = NULL;
1475 if (s->current_rule_list == rules)
1476 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001477 }
1478 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001479
Willy Tarreauff011f22011-01-06 17:51:27 +01001480 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001481
Willy Tarreau96257ec2012-12-27 10:46:37 +01001482 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001483 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001484 int ret;
1485
Willy Tarreau192252e2015-04-04 01:47:55 +02001486 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001487 ret = acl_pass(ret);
1488
Willy Tarreauff011f22011-01-06 17:51:27 +01001489 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001490 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001491
1492 if (!ret) /* condition not matched */
1493 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001494 }
1495
Willy Tarreauacc98002015-09-27 23:34:39 +02001496 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001497resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001498 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001499 case ACT_ACTION_ALLOW:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001500 rule_ret = HTTP_RULE_RES_STOP;
1501 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001502
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001503 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001504 if (deny_status)
1505 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001506 rule_ret = HTTP_RULE_RES_DENY;
1507 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001508
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001509 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001510 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001511 if (deny_status)
1512 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001513 rule_ret = HTTP_RULE_RES_DENY;
1514 goto end;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001515
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001516 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001517 /* Be sure to send any pending HTTP 103 response first */
1518 if (early_hints) {
1519 http_send_early_hints(s, early_hints);
1520 free_trash_chunk(early_hints);
1521 early_hints = NULL;
1522 }
Willy Tarreauae3c0102014-04-28 23:22:08 +02001523 /* Auth might be performed on regular http-req rules as well as on stats */
1524 auth_realm = rule->arg.auth.realm;
1525 if (!auth_realm) {
1526 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1527 auth_realm = STATS_DEFAULT_REALM;
1528 else
1529 auth_realm = px->id;
1530 }
1531 /* send 401/407 depending on whether we use a proxy or not. We still
1532 * count one error, because normal browsing won't significantly
1533 * increase the counter but brute force attempts will.
1534 */
1535 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1536 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001537 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001538 stream_inc_http_err_ctr(s);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001539 rule_ret = HTTP_RULE_RES_ABRT;
1540 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001541
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001542 case ACT_HTTP_REDIR:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001543 /* Be sure to send any pending HTTP 103 response first */
1544 if (early_hints) {
1545 http_send_early_hints(s, early_hints);
1546 free_trash_chunk(early_hints);
1547 early_hints = NULL;
1548 }
Christopher Fauletea827bd2018-11-15 15:34:11 +01001549 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau0b748332014-04-29 00:13:29 +02001550 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Fauletea827bd2018-11-15 15:34:11 +01001551 rule_ret = HTTP_RULE_RES_BADREQ;
1552 goto end;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001553
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001554 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001555 s->task->nice = rule->arg.nice;
1556 break;
1557
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001558 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001559 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001560 break;
1561
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001562 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001563 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Willy Tarreau51347ed2013-06-11 19:34:13 +02001564 break;
1565
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001566 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001567 s->logs.level = rule->arg.loglevel;
1568 break;
1569
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001570 case ACT_HTTP_REPLACE_HDR:
1571 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001572 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1573 rule->arg.hdr_add.name_len,
1574 &rule->arg.hdr_add.fmt,
Christopher Fauletea827bd2018-11-15 15:34:11 +01001575 &rule->arg.hdr_add.re, rule->action)) {
1576 rule_ret = HTTP_RULE_RES_BADREQ;
1577 goto end;
1578 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001579 break;
1580
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001581 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001582 ctx.idx = 0;
1583 /* remove all occurrences of the header */
1584 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001585 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001586 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001587 }
Willy Tarreau85603282015-01-21 20:39:27 +01001588 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001589
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001590 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001591 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001592 /* The scope of the trash buffer must be limited to this function. The
1593 * build_logline() function can execute a lot of other function which
1594 * can use the trash buffer. So for limiting the scope of this global
1595 * buffer, we build first the header value using build_logline, and
1596 * after we store the header name.
1597 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001598 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001599
1600 replace = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001601 if (!replace) {
1602 rule_ret = HTTP_RULE_RES_BADREQ;
1603 goto end;
1604 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001605
Thierry Fournier4b788f72016-06-01 13:35:36 +02001606 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001607 len += build_logline(s, replace->area + len,
1608 replace->size - len,
1609 &rule->arg.hdr_add.fmt);
1610 memcpy(replace->area, rule->arg.hdr_add.name,
1611 rule->arg.hdr_add.name_len);
1612 replace->area[rule->arg.hdr_add.name_len] = ':';
1613 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1614 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001615
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001616 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001617 /* remove all occurrences of the header */
1618 ctx.idx = 0;
1619 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001620 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001621 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1622 }
1623 }
1624
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001625 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001626 static unsigned char rate_limit = 0;
1627
1628 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001629 replace->area[rule->arg.hdr_add.name_len] = 0;
1630 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,
1631 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001632 }
1633
Olivier Houcharda798bf52019-03-08 18:52:00 +01001634 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001635 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001636 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001637 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001638 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001639 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001640
1641 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001642 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001643 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001644
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001645 case ACT_HTTP_DEL_ACL:
1646 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001647 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001648 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001649
1650 /* collect reference */
1651 ref = pat_ref_lookup(rule->arg.map.ref);
1652 if (!ref)
1653 continue;
1654
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001655 /* allocate key */
1656 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001657 if (!key) {
1658 rule_ret = HTTP_RULE_RES_BADREQ;
1659 goto end;
1660 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001661
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001662 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001663 key->data = build_logline(s, key->area, key->size,
1664 &rule->arg.map.key);
1665 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001666
1667 /* perform update */
1668 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001669 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001670 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001671 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001672
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001673 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001674 break;
1675 }
1676
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001677 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001678 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001679 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001680
1681 /* collect reference */
1682 ref = pat_ref_lookup(rule->arg.map.ref);
1683 if (!ref)
1684 continue;
1685
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001686 /* allocate key */
1687 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001688 if (!key) {
1689 rule_ret = HTTP_RULE_RES_BADREQ;
1690 goto end;
1691 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001692
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001693 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001694 key->data = build_logline(s, key->area, key->size,
1695 &rule->arg.map.key);
1696 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001697
1698 /* perform update */
1699 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001700 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001701 if (pat_ref_find_elt(ref, key->area) == NULL)
1702 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001703 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001704
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001705 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001706 break;
1707 }
1708
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001709 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001710 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001711 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001712
1713 /* collect reference */
1714 ref = pat_ref_lookup(rule->arg.map.ref);
1715 if (!ref)
1716 continue;
1717
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001718 /* allocate key */
1719 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001720 if (!key) {
1721 rule_ret = HTTP_RULE_RES_BADREQ;
1722 goto end;
1723 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001724
1725 /* allocate value */
1726 value = alloc_trash_chunk();
1727 if (!value) {
1728 free_trash_chunk(key);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001729 rule_ret = HTTP_RULE_RES_BADREQ;
1730 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001731 }
1732
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001733 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001734 key->data = build_logline(s, key->area, key->size,
1735 &rule->arg.map.key);
1736 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001737
1738 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001739 value->data = build_logline(s, value->area,
1740 value->size,
1741 &rule->arg.map.value);
1742 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001743
1744 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001745 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001746 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001747 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001748 else
1749 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001750 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001751
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001752 free_trash_chunk(key);
1753 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001754 break;
1755 }
William Lallemand73025dd2014-04-24 14:38:37 +02001756
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001757 case ACT_HTTP_EARLY_HINT:
1758 if (!(txn->req.flags & HTTP_MSGF_VER_11))
1759 break;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001760 early_hints = http_apply_early_hint_rule(s, early_hints,
1761 rule->arg.early_hint.name,
1762 rule->arg.early_hint.name_len,
1763 &rule->arg.early_hint.fmt);
1764 if (!early_hints) {
Christopher Fauletea827bd2018-11-15 15:34:11 +01001765 rule_ret = HTTP_RULE_RES_DONE;
1766 goto end;
1767 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001768 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02001769 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01001770 if ((s->req.flags & CF_READ_ERROR) ||
1771 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
1772 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
1773 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02001774 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02001775
Willy Tarreauacc98002015-09-27 23:34:39 +02001776 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02001777 case ACT_RET_ERR:
1778 case ACT_RET_CONT:
1779 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02001780 case ACT_RET_STOP:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001781 rule_ret = HTTP_RULE_RES_DONE;
1782 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02001783 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02001784 s->current_rule = rule;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001785 rule_ret = HTTP_RULE_RES_YIELD;
1786 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001787 }
William Lallemand73025dd2014-04-24 14:38:37 +02001788 break;
1789
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001790 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02001791 /* Note: only the first valid tracking parameter of each
1792 * applies.
1793 */
1794
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001795 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02001796 struct stktable *t;
1797 struct stksess *ts;
1798 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001799 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02001800
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02001801 t = rule->arg.trk_ctr.table.t;
1802 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 +02001803
1804 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001805 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02001806
1807 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001808 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1809 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1810 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001811 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02001812
Emeric Brun819fc6f2017-06-13 19:37:32 +02001813 if (ptr1)
1814 stktable_data_cast(ptr1, http_req_cnt)++;
1815
1816 if (ptr2)
1817 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
1818 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
1819
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001820 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01001821
1822 /* If data was modified, we need to touch to re-schedule sync */
1823 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001824 }
Willy Tarreau09448f72014-06-25 18:12:15 +02001825
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001826 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001827 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001828 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02001829 }
1830 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02001831 break;
1832
Joseph Herlant5ba80252018-11-15 09:25:36 -08001833 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02001834 default:
1835 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001836 }
1837 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01001838
Christopher Fauletea827bd2018-11-15 15:34:11 +01001839 end:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001840 if (early_hints) {
1841 http_send_early_hints(s, early_hints);
1842 free_trash_chunk(early_hints);
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001843 }
1844
Willy Tarreau96257ec2012-12-27 10:46:37 +01001845 /* we reached the end of the rules, nothing to report */
Christopher Fauletea827bd2018-11-15 15:34:11 +01001846 return rule_ret;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001847}
1848
Willy Tarreau71241ab2012-12-27 11:30:54 +01001849
Willy Tarreau51d861a2015-05-22 17:30:48 +02001850/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
1851 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
1852 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
1853 * is returned, the process can continue the evaluation of next rule list. If
1854 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
1855 * is returned, it means the operation could not be processed and a server error
1856 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
1857 * deny rule. If *YIELD is returned, the caller must call again the function
1858 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001859 */
Christopher Faulet10079f52018-10-03 15:17:28 +02001860enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001861http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001862{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001863 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001864 struct http_txn *txn = s->txn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001865 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001866 struct hdr_ctx ctx;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001867 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001868 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001869
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001870 /* If "the current_rule_list" match the executed rule list, we are in
1871 * resume condition. If a resume is needed it is always in the action
1872 * and never in the ACL or converters. In this case, we initialise the
1873 * current rule, and go to the action execution point.
1874 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001875 if (s->current_rule) {
1876 rule = s->current_rule;
1877 s->current_rule = NULL;
1878 if (s->current_rule_list == rules)
1879 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001880 }
1881 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001882
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001883 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001884
1885 /* check optional condition */
1886 if (rule->cond) {
1887 int ret;
1888
Willy Tarreau192252e2015-04-04 01:47:55 +02001889 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001890 ret = acl_pass(ret);
1891
1892 if (rule->cond->pol == ACL_COND_UNLESS)
1893 ret = !ret;
1894
1895 if (!ret) /* condition not matched */
1896 continue;
1897 }
1898
Willy Tarreauacc98002015-09-27 23:34:39 +02001899 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001900resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001901 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001902 case ACT_ACTION_ALLOW:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001903 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
1904 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001905
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001906 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001907 txn->flags |= TX_SVDENY;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001908 rule_ret = HTTP_RULE_RES_STOP;
1909 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001910
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001911 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001912 s->task->nice = rule->arg.nice;
1913 break;
1914
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001915 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001916 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001917 break;
1918
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001919 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001920 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Willy Tarreau51347ed2013-06-11 19:34:13 +02001921 break;
1922
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001923 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001924 s->logs.level = rule->arg.loglevel;
1925 break;
1926
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001927 case ACT_HTTP_REPLACE_HDR:
1928 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001929 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
1930 rule->arg.hdr_add.name_len,
1931 &rule->arg.hdr_add.fmt,
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001932 &rule->arg.hdr_add.re, rule->action)) {
1933 rule_ret = HTTP_RULE_RES_BADREQ;
1934 goto end;
1935 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001936 break;
1937
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001938 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001939 ctx.idx = 0;
1940 /* remove all occurrences of the header */
1941 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001942 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001943 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
1944 }
Willy Tarreau85603282015-01-21 20:39:27 +01001945 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001946
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001947 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001948 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02001949 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001950
1951 replace = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001952 if (!replace) {
1953 rule_ret = HTTP_RULE_RES_BADREQ;
1954 goto end;
1955 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001956
1957 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001958 memcpy(replace->area, rule->arg.hdr_add.name,
1959 rule->arg.hdr_add.name_len);
1960 replace->data = rule->arg.hdr_add.name_len;
1961 replace->area[replace->data++] = ':';
1962 replace->area[replace->data++] = ' ';
1963 replace->data += build_logline(s,
1964 replace->area + replace->data,
1965 replace->size - replace->data,
1966 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01001967
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001968 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001969 /* remove all occurrences of the header */
1970 ctx.idx = 0;
1971 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001972 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001973 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
1974 }
1975 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001976
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001977 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001978 static unsigned char rate_limit = 0;
1979
1980 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001981 replace->area[rule->arg.hdr_add.name_len] = 0;
1982 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,
1983 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001984 }
1985
Olivier Houcharda798bf52019-03-08 18:52:00 +01001986 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001987 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001988 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001989 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001990 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001991 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001992 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001993 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001994
1995 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001996 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001997 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001998
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001999 case ACT_HTTP_DEL_ACL:
2000 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002001 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002002 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002003
2004 /* collect reference */
2005 ref = pat_ref_lookup(rule->arg.map.ref);
2006 if (!ref)
2007 continue;
2008
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002009 /* allocate key */
2010 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002011 if (!key) {
2012 rule_ret = HTTP_RULE_RES_BADREQ;
2013 goto end;
2014 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002015
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002016 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002017 key->data = build_logline(s, key->area, key->size,
2018 &rule->arg.map.key);
2019 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002020
2021 /* perform update */
2022 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002023 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002024 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002025 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002026
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002027 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002028 break;
2029 }
2030
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002031 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002032 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002033 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002034
2035 /* collect reference */
2036 ref = pat_ref_lookup(rule->arg.map.ref);
2037 if (!ref)
2038 continue;
2039
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002040 /* allocate key */
2041 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002042 if (!key) {
2043 rule_ret = HTTP_RULE_RES_BADREQ;
2044 goto end;
2045 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002046
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002047 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002048 key->data = build_logline(s, key->area, key->size,
2049 &rule->arg.map.key);
2050 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002051
2052 /* perform update */
2053 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002054 if (pat_ref_find_elt(ref, key->area) == NULL)
2055 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002056
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002057 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002058 break;
2059 }
2060
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002061 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002062 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002063 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002064
2065 /* collect reference */
2066 ref = pat_ref_lookup(rule->arg.map.ref);
2067 if (!ref)
2068 continue;
2069
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002070 /* allocate key */
2071 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002072 if (!key) {
2073 rule_ret = HTTP_RULE_RES_BADREQ;
2074 goto end;
2075 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002076
2077 /* allocate value */
2078 value = alloc_trash_chunk();
2079 if (!value) {
2080 free_trash_chunk(key);
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002081 rule_ret = HTTP_RULE_RES_BADREQ;
2082 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002083 }
2084
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002085 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002086 key->data = build_logline(s, key->area, key->size,
2087 &rule->arg.map.key);
2088 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002089
2090 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002091 value->data = build_logline(s, value->area,
2092 value->size,
2093 &rule->arg.map.value);
2094 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002095
2096 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002097 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002098 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002099 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002100 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002101 else
2102 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002103 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002104 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002105 free_trash_chunk(key);
2106 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002107 break;
2108 }
William Lallemand73025dd2014-04-24 14:38:37 +02002109
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002110 case ACT_HTTP_REDIR:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002111 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002112 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002113 rule_ret = HTTP_RULE_RES_BADREQ;
2114 goto end;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002115
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002116 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2117 /* Note: only the first valid tracking parameter of each
2118 * applies.
2119 */
2120
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002121 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002122 struct stktable *t;
2123 struct stksess *ts;
2124 struct stktable_key *key;
2125 void *ptr;
2126
2127 t = rule->arg.trk_ctr.table.t;
2128 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2129
2130 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002131 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002132
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002133 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002134
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002135 /* let's count a new HTTP request as it's the first time we do it */
2136 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2137 if (ptr)
2138 stktable_data_cast(ptr, http_req_cnt)++;
2139
2140 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2141 if (ptr)
2142 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2143 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2144
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002145 /* When the client triggers a 4xx from the server, it's most often due
2146 * to a missing object or permission. These events should be tracked
2147 * because if they happen often, it may indicate a brute force or a
2148 * vulnerability scan. Normally this is done when receiving the response
2149 * but here we're tracking after this ought to have been done so we have
2150 * to do it on purpose.
2151 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002152 if ((unsigned)(txn->status - 400) < 100) {
2153 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2154 if (ptr)
2155 stktable_data_cast(ptr, http_err_cnt)++;
2156
2157 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2158 if (ptr)
2159 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2160 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2161 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002162
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002163 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002164
Emeric Brun0fed0b02017-11-29 16:15:07 +01002165 /* If data was modified, we need to touch to re-schedule sync */
2166 stktable_touch_local(t, ts, 0);
2167
Emeric Brun819fc6f2017-06-13 19:37:32 +02002168 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2169 if (sess->fe != s->be)
2170 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2171
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002172 }
2173 }
2174 break;
2175
Thierry FOURNIER42148732015-09-02 17:17:33 +02002176 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002177 if ((s->req.flags & CF_READ_ERROR) ||
2178 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2179 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2180 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002181 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002182
Willy Tarreauacc98002015-09-27 23:34:39 +02002183 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002184 case ACT_RET_ERR:
2185 case ACT_RET_CONT:
2186 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002187 case ACT_RET_STOP:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002188 rule_ret = HTTP_RULE_RES_STOP;
2189 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002190 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002191 s->current_rule = rule;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002192 rule_ret = HTTP_RULE_RES_YIELD;
2193 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002194 }
William Lallemand73025dd2014-04-24 14:38:37 +02002195 break;
2196
Joseph Herlant5ba80252018-11-15 09:25:36 -08002197 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002198 default:
2199 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002200 }
2201 }
2202
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002203 end:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002204 /* we reached the end of the rules, nothing to report */
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002205 return rule_ret;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002206}
2207
2208
Willy Tarreau71241ab2012-12-27 11:30:54 +01002209/* Perform an HTTP redirect based on the information in <rule>. The function
2210 * returns non-zero on success, or zero in case of a, irrecoverable error such
2211 * as too large a request to build a valid response.
2212 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002213int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002214{
Willy Tarreaub329a312015-05-22 16:27:37 +02002215 struct http_msg *req = &txn->req;
2216 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002217 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002218 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002219 int ret = 0;
2220
Christopher Fauletf2824e62018-10-01 12:12:37 +02002221 if (IS_HTX_STRM(s))
2222 return htx_apply_redirect_rule(rule, s, txn);
2223
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002224 chunk = alloc_trash_chunk();
2225 if (!chunk)
2226 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002227
2228 /* build redirect message */
2229 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002230 case 308:
2231 msg_fmt = HTTP_308;
2232 break;
2233 case 307:
2234 msg_fmt = HTTP_307;
2235 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002236 case 303:
2237 msg_fmt = HTTP_303;
2238 break;
2239 case 301:
2240 msg_fmt = HTTP_301;
2241 break;
2242 case 302:
2243 default:
2244 msg_fmt = HTTP_302;
2245 break;
2246 }
2247
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002248 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2249 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002250
2251 switch(rule->type) {
2252 case REDIRECT_TYPE_SCHEME: {
2253 const char *path;
2254 const char *host;
2255 struct hdr_ctx ctx;
2256 int pathlen;
2257 int hostlen;
2258
2259 host = "";
2260 hostlen = 0;
2261 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002262 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002263 host = ctx.line + ctx.val;
2264 hostlen = ctx.vlen;
2265 }
2266
Willy Tarreau6b952c82018-09-10 17:45:34 +02002267 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002268 /* build message using path */
2269 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002270 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002271 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2272 int qs = 0;
2273 while (qs < pathlen) {
2274 if (path[qs] == '?') {
2275 pathlen = qs;
2276 break;
2277 }
2278 qs++;
2279 }
2280 }
2281 } else {
2282 path = "/";
2283 pathlen = 1;
2284 }
2285
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002286 if (rule->rdr_str) { /* this is an old "redirect" rule */
2287 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002288 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002289 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002290
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002291 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002292 memcpy(chunk->area + chunk->data, rule->rdr_str,
2293 rule->rdr_len);
2294 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002295 }
2296 else {
2297 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002298 chunk->data += build_logline(s,
2299 chunk->area + chunk->data,
2300 chunk->size - chunk->data,
2301 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002302
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002303 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002304 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002305 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002306 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002307 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002308 memcpy(chunk->area + chunk->data, "://", 3);
2309 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002310
2311 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002312 memcpy(chunk->area + chunk->data, host, hostlen);
2313 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002314
2315 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002316 memcpy(chunk->area + chunk->data, path, pathlen);
2317 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002318
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002319 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002320 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002321 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002322 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002323 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002324 chunk->area[chunk->data] = '/';
2325 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002326 }
2327
2328 break;
2329 }
2330 case REDIRECT_TYPE_PREFIX: {
2331 const char *path;
2332 int pathlen;
2333
Willy Tarreau6b952c82018-09-10 17:45:34 +02002334 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002335 /* build message using path */
2336 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002337 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002338 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2339 int qs = 0;
2340 while (qs < pathlen) {
2341 if (path[qs] == '?') {
2342 pathlen = qs;
2343 break;
2344 }
2345 qs++;
2346 }
2347 }
2348 } else {
2349 path = "/";
2350 pathlen = 1;
2351 }
2352
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002353 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002354 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002355 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002356
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002357 /* add prefix. Note that if prefix == "/", we don't want to
2358 * add anything, otherwise it makes it hard for the user to
2359 * configure a self-redirection.
2360 */
2361 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002362 memcpy(chunk->area + chunk->data,
2363 rule->rdr_str, rule->rdr_len);
2364 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002365 }
2366 }
2367 else {
2368 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002369 chunk->data += build_logline(s,
2370 chunk->area + chunk->data,
2371 chunk->size - chunk->data,
2372 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002373
2374 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002375 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002376 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002377 }
2378
2379 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002380 memcpy(chunk->area + chunk->data, path, pathlen);
2381 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002382
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002383 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002384 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002385 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002386 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002387 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002388 chunk->area[chunk->data] = '/';
2389 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002390 }
2391
2392 break;
2393 }
2394 case REDIRECT_TYPE_LOCATION:
2395 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002396 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002397 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002398 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002399
2400 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002401 memcpy(chunk->area + chunk->data, rule->rdr_str,
2402 rule->rdr_len);
2403 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002404 }
2405 else {
2406 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002407 chunk->data += build_logline(s,
2408 chunk->area + chunk->data,
2409 chunk->size - chunk->data,
2410 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002411
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002412 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002413 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002414 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002415 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002416 break;
2417 }
2418
2419 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002420 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2421 chunk->data += 14;
2422 memcpy(chunk->area + chunk->data, rule->cookie_str,
2423 rule->cookie_len);
2424 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002425 }
2426
Willy Tarreau19b14122017-02-28 09:48:11 +01002427 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002428 txn->status = rule->code;
2429 /* let's log the request time */
2430 s->logs.tv_request = now;
2431
Christopher Fauletbe821b92017-03-30 11:21:53 +02002432 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002433 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2434 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2435 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002436 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002437 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002438 memcpy(chunk->area + chunk->data,
2439 "\r\nProxy-Connection: keep-alive", 30);
2440 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002441 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002442 memcpy(chunk->area + chunk->data,
2443 "\r\nConnection: keep-alive", 24);
2444 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002445 }
2446 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002447 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2448 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002449 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002450 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002451 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002452 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002453 req->next -= req->sov;
2454 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002455 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002456 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002457 req->msg_state = HTTP_MSG_CLOSED;
2458 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002459 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002460 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002461 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002462 /* let the server side turn to SI_ST_CLO */
2463 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002464 } else {
2465 /* keep-alive not possible */
2466 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002467 memcpy(chunk->area + chunk->data,
2468 "\r\nProxy-Connection: close\r\n\r\n", 29);
2469 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002470 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002471 memcpy(chunk->area + chunk->data,
2472 "\r\nConnection: close\r\n\r\n", 23);
2473 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002474 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002475 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002476 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002477 }
2478
Willy Tarreaue7dff022015-04-03 01:14:29 +02002479 if (!(s->flags & SF_ERR_MASK))
2480 s->flags |= SF_ERR_LOCAL;
2481 if (!(s->flags & SF_FINST_MASK))
2482 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002483
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002484 ret = 1;
2485 leave:
2486 free_trash_chunk(chunk);
2487 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002488}
2489
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002490/* This stream analyser runs all HTTP request processing which is common to
2491 * frontends and backends, which means blocking ACLs, filters, connection-close,
2492 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002493 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002494 * either needs more data or wants to immediately abort the request (eg: deny,
2495 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002496 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002497int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002498{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002499 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002500 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002501 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002502 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002503 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002504 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002505 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002506 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002507
Christopher Faulete0768eb2018-10-03 16:38:02 +02002508 if (IS_HTX_STRM(s))
2509 return htx_process_req_common(s, req, an_bit, px);
2510
Willy Tarreau655dce92009-11-08 13:10:58 +01002511 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002512 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002513 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002514 }
2515
Christopher Faulet45073512018-07-20 10:16:29 +02002516 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 +02002517 now_ms, __FUNCTION__,
2518 s,
2519 req,
2520 req->rex, req->wex,
2521 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002522 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002523 req->analysers);
2524
Willy Tarreau65410832014-04-28 21:25:43 +02002525 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002526 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002527
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002528 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002529 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002530 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002531
Willy Tarreau0b748332014-04-29 00:13:29 +02002532 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002533 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2534 goto return_prx_yield;
2535
Willy Tarreau0b748332014-04-29 00:13:29 +02002536 case HTTP_RULE_RES_CONT:
2537 case HTTP_RULE_RES_STOP: /* nothing to do */
2538 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002539
Willy Tarreau0b748332014-04-29 00:13:29 +02002540 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2541 if (txn->flags & TX_CLTARPIT)
2542 goto tarpit;
2543 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002544
Willy Tarreau0b748332014-04-29 00:13:29 +02002545 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2546 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002547
Willy Tarreau0b748332014-04-29 00:13:29 +02002548 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002549 goto done;
2550
Willy Tarreau0b748332014-04-29 00:13:29 +02002551 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2552 goto return_bad_req;
2553 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002554 }
2555
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002556 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2557 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002558 struct hdr_ctx ctx;
2559
2560 ctx.idx = 0;
2561 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002562 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002563 if (unlikely(http_header_add_tail2(&txn->req,
2564 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002565 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002566 goto return_bad_req;
2567 }
2568 }
2569
2570 }
2571
Willy Tarreau52542592014-04-28 18:33:26 +02002572 /* OK at this stage, we know that the request was accepted according to
2573 * the http-request rules, we can check for the stats. Note that the
2574 * URI is detected *before* the req* rules in order not to be affected
2575 * by a possible reqrep, while they are processed *after* so that a
2576 * reqdeny can still block them. This clearly needs to change in 1.6!
2577 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002578 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002579 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01002580 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002581 txn->status = 500;
2582 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002583 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002584
Willy Tarreaue7dff022015-04-03 01:14:29 +02002585 if (!(s->flags & SF_ERR_MASK))
2586 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002587 goto return_prx_cond;
2588 }
2589
2590 /* parse the whole stats request and extract the relevant information */
2591 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002592 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002593 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002594
Willy Tarreau0b748332014-04-29 00:13:29 +02002595 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2596 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002597
Willy Tarreau0b748332014-04-29 00:13:29 +02002598 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2599 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002600 }
2601
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002602 /* evaluate the req* rules except reqadd */
2603 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002604 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002605 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002606
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002607 if (txn->flags & TX_CLDENY)
2608 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002609
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002610 if (txn->flags & TX_CLTARPIT) {
2611 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002612 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002613 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002614 }
Willy Tarreau06619262006-12-17 08:37:22 +01002615
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002616 /* add request headers from the rule sets in the same order */
2617 list_for_each_entry(wl, &px->req_add, list) {
2618 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002619 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002620 ret = acl_pass(ret);
2621 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2622 ret = !ret;
2623 if (!ret)
2624 continue;
2625 }
2626
Christopher Faulet10079f52018-10-03 15:17:28 +02002627 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002628 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002629 }
2630
Willy Tarreau52542592014-04-28 18:33:26 +02002631
2632 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002633 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2634 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002635 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002636 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +01002637 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002638
Willy Tarreaue7dff022015-04-03 01:14:29 +02002639 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2640 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2641 if (!(s->flags & SF_FINST_MASK))
2642 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002643
Willy Tarreau70730dd2014-04-24 18:06:27 +02002644 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002645 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2646 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002647 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002648 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002649 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002650
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002651 /* check whether we have some ACLs set to redirect this request */
2652 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002653 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002654 int ret;
2655
Willy Tarreau192252e2015-04-04 01:47:55 +02002656 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002657 ret = acl_pass(ret);
2658 if (rule->cond->pol == ACL_COND_UNLESS)
2659 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002660 if (!ret)
2661 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01002662 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002663 if (!http_apply_redirect_rule(rule, s, txn))
2664 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002665 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002666 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002667
Willy Tarreau2be39392010-01-03 17:24:51 +01002668 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2669 * If this happens, then the data will not come immediately, so we must
2670 * send all what we have without waiting. Note that due to the small gain
2671 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002672 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01002673 * itself once used.
2674 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002675 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01002676
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002677 done: /* done with this analyser, continue with next ones that the calling
2678 * points will have set, if any.
2679 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002680 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002681 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
2682 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002683 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002684
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002685 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02002686 /* Allow cookie logging
2687 */
2688 if (s->be->cookie_name || sess->fe->capture_name)
2689 manage_client_side_cookies(s, req);
2690
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002691 /* When a connection is tarpitted, we use the tarpit timeout,
2692 * which may be the same as the connect timeout if unspecified.
2693 * If unset, then set it to zero because we really want it to
2694 * eventually expire. We build the tarpit as an analyser.
2695 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002696 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002697
2698 /* wipe the request out so that we can drop the connection early
2699 * if the client closes first.
2700 */
2701 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002702
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002703 txn->status = http_err_codes[deny_status];
2704
Christopher Faulet0184ea72017-01-05 14:06:34 +01002705 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002706 req->analysers |= AN_REQ_HTTP_TARPIT;
2707 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2708 if (!req->analyse_exp)
2709 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02002710 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +01002711 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002712 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002713 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002714 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002715 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002716 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002717
2718 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002719
2720 /* Allow cookie logging
2721 */
2722 if (s->be->cookie_name || sess->fe->capture_name)
2723 manage_client_side_cookies(s, req);
2724
Willy Tarreau0b748332014-04-29 00:13:29 +02002725 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002726 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002727 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002728 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02002729 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +01002730 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002731 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002732 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002733 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002734 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002735 goto return_prx_cond;
2736
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002737 return_bad_req:
2738 /* We centralize bad requests processing here */
2739 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2740 /* we detected a parsing error. We want to archive this request
2741 * in the dedicated proxy area for later troubleshooting.
2742 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02002743 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002744 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002745
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002746 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002747 txn->req.msg_state = HTTP_MSG_ERROR;
2748 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002749 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002750
Olivier Houcharda798bf52019-03-08 18:52:00 +01002751 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002752 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002753 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002754
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002755 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002756 if (!(s->flags & SF_ERR_MASK))
2757 s->flags |= SF_ERR_PRXCOND;
2758 if (!(s->flags & SF_FINST_MASK))
2759 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002760
Christopher Faulet0184ea72017-01-05 14:06:34 +01002761 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002762 req->analyse_exp = TICK_ETERNITY;
2763 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002764
2765 return_prx_yield:
2766 channel_dont_connect(req);
2767 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002768}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002769
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002770/* This function performs all the processing enabled for the current request.
2771 * It returns 1 if the processing can continue on next analysers, or zero if it
2772 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002773 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002774 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002775int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002776{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002777 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002778 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002779 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02002780 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002781
Christopher Faulete0768eb2018-10-03 16:38:02 +02002782 if (IS_HTX_STRM(s))
2783 return htx_process_request(s, req, an_bit);
2784
Willy Tarreau655dce92009-11-08 13:10:58 +01002785 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002786 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002787 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002788 return 0;
2789 }
2790
Christopher Faulet45073512018-07-20 10:16:29 +02002791 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 +02002792 now_ms, __FUNCTION__,
2793 s,
2794 req,
2795 req->rex, req->wex,
2796 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002797 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002798 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002799
Willy Tarreau59234e92008-11-30 23:51:27 +01002800 /*
2801 * Right now, we know that we have processed the entire headers
2802 * and that unwanted requests have been filtered out. We can do
2803 * whatever we want with the remaining request. Also, now we
2804 * may have separate values for ->fe, ->be.
2805 */
Willy Tarreau06619262006-12-17 08:37:22 +01002806
Willy Tarreau59234e92008-11-30 23:51:27 +01002807 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002808 * If HTTP PROXY is set we simply get remote server address parsing
2809 * incoming request. Note that this requires that a connection is
2810 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01002811 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002812 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002813 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002814 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002815
Willy Tarreau9471b8c2013-12-15 13:31:35 +01002816 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002817 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002818 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002819 txn->req.msg_state = HTTP_MSG_ERROR;
2820 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002821 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002822 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002823
Willy Tarreaue7dff022015-04-03 01:14:29 +02002824 if (!(s->flags & SF_ERR_MASK))
2825 s->flags |= SF_ERR_RESOURCE;
2826 if (!(s->flags & SF_FINST_MASK))
2827 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002828
2829 return 0;
2830 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002831
Willy Tarreau6b952c82018-09-10 17:45:34 +02002832 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02002833 if (url2sa(ci_head(req) + msg->sl.rq.u,
2834 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02002835 &conn->addr.to, NULL) == -1)
2836 goto return_bad_req;
2837
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002838 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02002839 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
2840 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002841 * u_l characters by a single "/".
2842 */
2843 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002844 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002845 char *cur_end = cur_ptr + txn->req.sl.rq.l;
2846 int delta;
2847
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002848 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002849 http_msg_move_end(&txn->req, delta);
2850 cur_end += delta;
2851 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
2852 goto return_bad_req;
2853 }
2854 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002855 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002856 char *cur_end = cur_ptr + txn->req.sl.rq.l;
2857 int delta;
2858
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002859 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002860 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002861 http_msg_move_end(&txn->req, delta);
2862 cur_end += delta;
2863 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
2864 goto return_bad_req;
2865 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002866 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002867
Willy Tarreau59234e92008-11-30 23:51:27 +01002868 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002869 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002870 * Note that doing so might move headers in the request, but
2871 * the fields will stay coherent and the URI will not move.
2872 * This should only be performed in the backend.
2873 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002874 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002875 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002876
William Lallemanda73203e2012-03-12 12:48:57 +01002877 /* add unique-id if "header-unique-id" is specified */
2878
Thierry Fournierf4011dd2016-03-29 17:23:51 +02002879 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01002880 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002881 goto return_bad_req;
2882 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002883 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002884 }
William Lallemanda73203e2012-03-12 12:48:57 +01002885
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002886 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02002887 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01002888 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002889 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01002890 goto return_bad_req;
2891 }
2892
Cyril Bontéb21570a2009-11-29 20:04:48 +01002893 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002894 * 9: add X-Forwarded-For if either the frontend or the backend
2895 * asks for it.
2896 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002897 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02002898 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002899 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
2900 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
2901 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002902 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02002903 /* The header is set to be added only if none is present
2904 * and we found it, so don't do anything.
2905 */
2906 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002907 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002908 /* Add an X-Forwarded-For header unless the source IP is
2909 * in the 'except' network range.
2910 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002911 if ((!sess->fe->except_mask.s_addr ||
2912 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
2913 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01002914 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002915 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01002916 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002917 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002918 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002919 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002920
2921 /* Note: we rely on the backend to get the header name to be used for
2922 * x-forwarded-for, because the header is really meant for the backends.
2923 * However, if the backend did not specify any option, we have to rely
2924 * on the frontend's header name.
2925 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002926 if (s->be->fwdfor_hdr_len) {
2927 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002928 memcpy(trash.area,
2929 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002930 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002931 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002932 memcpy(trash.area,
2933 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002934 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002935 len += snprintf(trash.area + len,
2936 trash.size - len,
2937 ": %d.%d.%d.%d", pn[0], pn[1],
2938 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002939
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002940 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01002941 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002942 }
2943 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002944 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002945 /* FIXME: for the sake of completeness, we should also support
2946 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002947 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002948 int len;
2949 char pn[INET6_ADDRSTRLEN];
2950 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002951 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01002952 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002953
Willy Tarreau59234e92008-11-30 23:51:27 +01002954 /* Note: we rely on the backend to get the header name to be used for
2955 * x-forwarded-for, because the header is really meant for the backends.
2956 * However, if the backend did not specify any option, we have to rely
2957 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002958 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002959 if (s->be->fwdfor_hdr_len) {
2960 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002961 memcpy(trash.area, s->be->fwdfor_hdr_name,
2962 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01002963 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002964 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002965 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
2966 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002967 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002968 len += snprintf(trash.area + len, trash.size - len,
2969 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002970
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002971 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01002972 goto return_bad_req;
2973 }
2974 }
2975
2976 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02002977 * 10: add X-Original-To if either the frontend or the backend
2978 * asks for it.
2979 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002980 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02002981
2982 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002983 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02002984 /* Add an X-Original-To header unless the destination IP is
2985 * in the 'except' network range.
2986 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002987 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02002988
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002989 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002990 ((!sess->fe->except_mask_to.s_addr ||
2991 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
2992 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02002993 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002994 (((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 +02002995 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02002996 int len;
2997 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002998 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02002999
3000 /* Note: we rely on the backend to get the header name to be used for
3001 * x-original-to, because the header is really meant for the backends.
3002 * However, if the backend did not specify any option, we have to rely
3003 * on the frontend's header name.
3004 */
3005 if (s->be->orgto_hdr_len) {
3006 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003007 memcpy(trash.area,
3008 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003009 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003010 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003011 memcpy(trash.area,
3012 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003013 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003014 len += snprintf(trash.area + len,
3015 trash.size - len,
3016 ": %d.%d.%d.%d", pn[0], pn[1],
3017 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003018
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003019 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003020 goto return_bad_req;
3021 }
3022 }
3023 }
3024
Willy Tarreau50fc7772012-11-11 22:19:57 +01003025 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3026 * If an "Upgrade" token is found, the header is left untouched in order not to have
3027 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3028 * "Upgrade" is present in the Connection header.
3029 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003030 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003031 unsigned int want_flags = 0;
3032
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003033 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003034 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003035 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003036 want_flags |= TX_CON_CLO_SET;
3037 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003038 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003039 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003040 want_flags |= TX_CON_KAL_SET;
3041 }
3042
3043 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003044 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003045 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003046
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003047
Willy Tarreau522d6c02009-12-06 18:49:18 +01003048 /* If we have no server assigned yet and we're balancing on url_param
3049 * with a POST request, we may be interested in checking the body for
3050 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003051 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003052 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +01003053 s->txn->meth == HTTP_METH_POST &&
3054 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003055 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003056 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003057 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003058 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003059
Christopher Fauletbe821b92017-03-30 11:21:53 +02003060 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3061 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +01003062
Christopher Fauletbe821b92017-03-30 11:21:53 +02003063 /* We expect some data from the client. Unless we know for sure
3064 * we already have a full request, we have to re-enable quick-ack
3065 * in case we previously disabled it, otherwise we might cause
3066 * the client to delay further data.
3067 */
3068 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletbe821b92017-03-30 11:21:53 +02003069 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003070 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau1a18b542018-12-11 16:37:42 +01003071 conn_set_quickack(cli_conn, 1);
Willy Tarreau03945942009-12-22 16:50:27 +01003072
Willy Tarreau59234e92008-11-30 23:51:27 +01003073 /*************************************************************
3074 * OK, that's finished for the headers. We have done what we *
3075 * could. Let's switch to the DATA state. *
3076 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003077 req->analyse_exp = TICK_ETERNITY;
3078 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003079
Willy Tarreau59234e92008-11-30 23:51:27 +01003080 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003081 /* OK let's go on with the BODY now */
3082 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003083
Willy Tarreau59234e92008-11-30 23:51:27 +01003084 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003085 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003086 /* we detected a parsing error. We want to archive this request
3087 * in the dedicated proxy area for later troubleshooting.
3088 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003089 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003090 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003091
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003092 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003093 txn->req.msg_state = HTTP_MSG_ERROR;
3094 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003095 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003096 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003097
Olivier Houcharda798bf52019-03-08 18:52:00 +01003098 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003099 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003100 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003101
Willy Tarreaue7dff022015-04-03 01:14:29 +02003102 if (!(s->flags & SF_ERR_MASK))
3103 s->flags |= SF_ERR_PRXCOND;
3104 if (!(s->flags & SF_FINST_MASK))
3105 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003106 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003107}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003108
Willy Tarreau60b85b02008-11-30 23:28:40 +01003109/* This function is an analyser which processes the HTTP tarpit. It always
3110 * returns zero, at the beginning because it prevents any other processing
3111 * from occurring, and at the end because it terminates the request.
3112 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003113int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003114{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003115 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003116
Christopher Faulete0768eb2018-10-03 16:38:02 +02003117 if (IS_HTX_STRM(s))
3118 return htx_process_tarpit(s, req, an_bit);
3119
Willy Tarreau60b85b02008-11-30 23:28:40 +01003120 /* This connection is being tarpitted. The CLIENT side has
3121 * already set the connect expiration date to the right
3122 * timeout. We just have to check that the client is still
3123 * there and that the timeout has not expired.
3124 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003125 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003126 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003127 !tick_is_expired(req->analyse_exp, now_ms))
3128 return 0;
3129
3130 /* We will set the queue timer to the time spent, just for
3131 * logging purposes. We fake a 500 server error, so that the
3132 * attacker will not suspect his connection has been tarpitted.
3133 * It will not cause trouble to the logs because we can exclude
3134 * the tarpitted connections by filtering on the 'PT' status flags.
3135 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003136 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3137
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003138 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003139 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003140
Christopher Faulet0184ea72017-01-05 14:06:34 +01003141 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003142 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003143
Willy Tarreaue7dff022015-04-03 01:14:29 +02003144 if (!(s->flags & SF_ERR_MASK))
3145 s->flags |= SF_ERR_PRXCOND;
3146 if (!(s->flags & SF_FINST_MASK))
3147 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003148 return 0;
3149}
3150
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003151/* This function is an analyser which waits for the HTTP request body. It waits
3152 * for either the buffer to be full, or the full advertised contents to have
3153 * reached the buffer. It must only be called after the standard HTTP request
3154 * processing has occurred, because it expects the request to be parsed and will
3155 * look for the Expect header. It may send a 100-Continue interim response. It
3156 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3157 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3158 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003159 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003160int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003161{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003162 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003163 struct http_txn *txn = s->txn;
3164 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003165
Christopher Faulete0768eb2018-10-03 16:38:02 +02003166 if (IS_HTX_STRM(s))
3167 return htx_wait_for_request_body(s, req, an_bit);
3168
Willy Tarreaud34af782008-11-30 23:36:37 +01003169 /* We have to parse the HTTP request body to find any required data.
3170 * "balance url_param check_post" should have been the only way to get
3171 * into this. We were brought here after HTTP header analysis, so all
3172 * related structures are ready.
3173 */
3174
Willy Tarreau890988f2014-04-10 11:59:33 +02003175 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3176 /* This is the first call */
3177 if (msg->msg_state < HTTP_MSG_BODY)
3178 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003179
Willy Tarreau890988f2014-04-10 11:59:33 +02003180 if (msg->msg_state < HTTP_MSG_100_SENT) {
3181 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3182 * send an HTTP/1.1 100 Continue intermediate response.
3183 */
3184 if (msg->flags & HTTP_MSGF_VER_11) {
3185 struct hdr_ctx ctx;
3186 ctx.idx = 0;
3187 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003188 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003189 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003190 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003191 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003192 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003193 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003194 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003195 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003196
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003197 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003198 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003199 * must save the body in msg->next because it survives buffer
3200 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003201 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003202 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003203
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003204 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003205 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3206 else
3207 msg->msg_state = HTTP_MSG_DATA;
3208 }
3209
Willy Tarreau890988f2014-04-10 11:59:33 +02003210 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3211 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003212 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003213 goto missing_data;
3214
3215 /* OK we have everything we need now */
3216 goto http_end;
3217 }
3218
3219 /* OK here we're parsing a chunked-encoded message */
3220
Willy Tarreau522d6c02009-12-06 18:49:18 +01003221 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003222 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003223 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003224 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003225 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003226 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003227 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003228
Willy Tarreau115acb92009-12-26 13:56:06 +01003229 if (!ret)
3230 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003231 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003232 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003233 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003234 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003235 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003236 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003237 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003238
3239 msg->chunk_len = chunk;
3240 msg->body_len += chunk;
3241
3242 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003243 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003244 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003245 }
3246
Willy Tarreaud98cf932009-12-27 22:54:55 +01003247 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003248 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3249 * for at least a whole chunk or the whole content length bytes after
3250 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003251 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003252 if (msg->msg_state == HTTP_MSG_TRAILERS)
3253 goto http_end;
3254
Willy Tarreaue115b492015-05-01 23:05:14 +02003255 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003256 goto http_end;
3257
3258 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003259 /* we get here if we need to wait for more data. If the buffer is full,
3260 * we have the maximum we can expect.
3261 */
Willy Tarreau23752332018-06-15 14:54:53 +02003262 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003263 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003264
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003265 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003266 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003267 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003268
Willy Tarreaue7dff022015-04-03 01:14:29 +02003269 if (!(s->flags & SF_ERR_MASK))
3270 s->flags |= SF_ERR_CLITO;
3271 if (!(s->flags & SF_FINST_MASK))
3272 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003273 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003274 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003275
3276 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003277 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003278 /* Not enough data. We'll re-use the http-request
3279 * timeout here. Ideally, we should set the timeout
3280 * relative to the accept() date. We just set the
3281 * request timeout once at the beginning of the
3282 * request.
3283 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003284 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003285 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003286 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003287 return 0;
3288 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003289
3290 http_end:
3291 /* The situation will not evolve, so let's give up on the analysis. */
3292 s->logs.tv_request = now; /* update the request timer to reflect full request */
3293 req->analysers &= ~an_bit;
3294 req->analyse_exp = TICK_ETERNITY;
3295 return 1;
3296
3297 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003298 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003299 txn->req.msg_state = HTTP_MSG_ERROR;
3300 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003301 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003302
Willy Tarreaue7dff022015-04-03 01:14:29 +02003303 if (!(s->flags & SF_ERR_MASK))
3304 s->flags |= SF_ERR_PRXCOND;
3305 if (!(s->flags & SF_FINST_MASK))
3306 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003307
Willy Tarreau522d6c02009-12-06 18:49:18 +01003308 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003309 req->analysers &= AN_REQ_FLT_END;
Olivier Houcharda798bf52019-03-08 18:52:00 +01003310 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003311 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003312 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003313 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003314}
3315
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003316/* send a server's name with an outgoing request over an established connection.
3317 * Note: this function is designed to be called once the request has been scheduled
3318 * for being forwarded. This is the reason why it rewinds the buffer before
3319 * proceeding.
3320 */
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003321int http_send_name_header(struct stream *s, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003322
3323 struct hdr_ctx ctx;
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003324 struct http_txn *txn = s->txn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003325 char *hdr_name = be->server_id_hdr_name;
3326 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003327 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003328 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003329 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003330
Christopher Faulet64159df2018-10-24 21:15:35 +02003331 if (IS_HTX_STRM(s))
3332 return htx_send_name_header(s, be, srv_name);
William Lallemandd9e90662012-01-30 17:27:17 +01003333 ctx.idx = 0;
3334
Willy Tarreau211cdec2014-04-17 20:18:08 +02003335 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003336 if (old_o) {
3337 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003338 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003339 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003340 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003341 }
3342
Willy Tarreauf37954d2018-06-15 18:31:02 +02003343 old_i = ci_data(chn);
3344 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 -05003345 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003346 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003347 }
3348
3349 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003350 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003351 memcpy(hdr_val, hdr_name, hdr_name_len);
3352 hdr_val += hdr_name_len;
3353 *hdr_val++ = ':';
3354 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003355 hdr_val += strlcpy2(hdr_val, srv_name,
3356 trash.area + trash.size - hdr_val);
3357 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3358 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003359
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003360 if (old_o) {
3361 /* If this was a forwarded request, we must readjust the amount of
3362 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003363 * variations. Note that the current state is >= HTTP_MSG_BODY,
3364 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003365 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003366 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003367 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003368 txn->req.next -= old_o;
3369 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003370 }
3371
Mark Lamourinec2247f02012-01-04 13:02:01 -05003372 return 0;
3373}
3374
Willy Tarreau610ecce2010-01-04 21:15:02 +01003375/* Terminate current transaction and prepare a new one. This is very tricky
3376 * right now but it works.
3377 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003378void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003379{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003380 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003381 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003382 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003383 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003384 struct connection *srv_conn;
3385 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003386 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003387
Willy Tarreau610ecce2010-01-04 21:15:02 +01003388 /* FIXME: We need a more portable way of releasing a backend's and a
3389 * server's connections. We need a safer way to reinitialize buffer
3390 * flags. We also need a more accurate method for computing per-request
3391 * data.
3392 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003393 cs = objt_cs(s->si[1].end);
3394 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003395
Willy Tarreau4213a112013-12-15 10:25:42 +01003396 /* unless we're doing keep-alive, we want to quickly close the connection
3397 * to the server.
3398 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003399 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003400 !si_conn_ready(&s->si[1]) || !srv_conn->owner) {
Willy Tarreau350f4872014-11-28 14:42:25 +01003401 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3402 si_shutr(&s->si[1]);
3403 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003404 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003405
Willy Tarreaue7dff022015-04-03 01:14:29 +02003406 if (s->flags & SF_BE_ASSIGNED) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003407 _HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003408 if (unlikely(s->srv_conn))
3409 sess_change_server(s, NULL);
3410 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003411
3412 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003413 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003414
Willy Tarreaueee5b512015-04-03 23:46:31 +02003415 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003416 int n;
3417
Willy Tarreaueee5b512015-04-03 23:46:31 +02003418 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003419 if (n < 1 || n > 5)
3420 n = 0;
3421
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003422 if (fe->mode == PR_MODE_HTTP) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003423 _HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003424 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003425 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003426 (be->mode == PR_MODE_HTTP)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003427 _HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3428 _HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003429 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003430 }
3431
3432 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003433 s->logs.bytes_in -= ci_data(&s->req);
3434 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003435
Willy Tarreau66425e32018-07-25 06:55:12 +02003436 /* we may need to know the position in the queue */
3437 pendconn_free(s);
3438
Willy Tarreau610ecce2010-01-04 21:15:02 +01003439 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003440 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003441 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003442 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003443 s->do_log(s);
3444 }
3445
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003446 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003447 stream_stop_content_counters(s);
3448 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003449
Willy Tarreau9efd7452018-05-31 14:48:54 +02003450 /* reset the profiling counter */
3451 s->task->calls = 0;
3452 s->task->cpu_time = 0;
3453 s->task->lat_time = 0;
3454 s->task->call_date = (profiling & HA_PROF_TASKS) ? now_mono_time() : 0;
3455
Willy Tarreau610ecce2010-01-04 21:15:02 +01003456 s->logs.accept_date = date; /* user-visible date for logging */
3457 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003458 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3459 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003460 tv_zero(&s->logs.tv_request);
3461 s->logs.t_queue = -1;
3462 s->logs.t_connect = -1;
3463 s->logs.t_data = -1;
3464 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003465 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3466 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003467
Willy Tarreauf37954d2018-06-15 18:31:02 +02003468 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3469 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003470
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003471 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003472 if (s->flags & SF_CURR_SESS) {
3473 s->flags &= ~SF_CURR_SESS;
Olivier Houcharda798bf52019-03-08 18:52:00 +01003474 _HA_ATOMIC_SUB(&__objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003475 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003476 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003477 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003478 }
3479
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003480 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003481
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003482
3483 /* If we're doing keepalive, first call the mux detach() method
3484 * to let it know we want to detach without freing the connection.
3485 * We then can call si_release_endpoint() to destroy the conn_stream
Willy Tarreau4213a112013-12-15 10:25:42 +01003486 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003487 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houcharda70e1762018-12-13 18:01:00 +01003488 !si_conn_ready(&s->si[1]) ||
3489 (srv_conn && srv_conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)))
Willy Tarreau323a2d92015-08-04 19:00:17 +02003490 srv_conn = NULL;
Olivier Houchard985f1392018-11-30 17:31:52 +01003491 else if (!srv_conn->owner) {
3492 srv_conn->owner = s->sess;
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003493 /* Add it unconditionally to the session list, it'll be removed
3494 * later if needed by session_check_idle_conn(), once we'll
3495 * have released the endpoint and know if it no longer has
3496 * attached streams, and so an idling connection
3497 */
Olivier Houchard351411f2018-12-27 17:20:54 +01003498 if (!session_add_conn(s->sess, srv_conn, s->target)) {
3499 srv_conn->owner = NULL;
3500 /* Try to add the connection to the server idle list.
3501 * If it fails, as the connection no longer has an
3502 * owner, it will be destroy later by
3503 * si_release_endpoint(), anyway
3504 */
3505 srv_add_to_idle_list(objt_server(srv_conn->target), srv_conn);
3506 srv_conn = NULL;
3507
3508 }
Olivier Houchard985f1392018-11-30 17:31:52 +01003509 }
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003510 si_release_endpoint(&s->si[1]);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003511 if (srv_conn && srv_conn->owner == s->sess) {
3512 if (session_check_idle_conn(s->sess, srv_conn) != 0)
3513 srv_conn = NULL;
3514 }
3515
Willy Tarreau4213a112013-12-15 10:25:42 +01003516
Willy Tarreau350f4872014-11-28 14:42:25 +01003517 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3518 s->si[1].err_type = SI_ET_NONE;
3519 s->si[1].conn_retries = 0; /* used for logging too */
3520 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003521 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 +01003522 s->req.flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WAKE_CONNECT|CF_WROTE_DATA);
Willy Tarreauede3d882018-10-24 17:17:56 +02003523 s->res.flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA);
Willy Tarreaue7dff022015-04-03 01:14:29 +02003524 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3525 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3526 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003527
Patrick Hemmere3faf022018-08-22 10:02:00 -04003528 hlua_ctx_destroy(s->hlua);
3529 s->hlua = NULL;
3530
Willy Tarreaueee5b512015-04-03 23:46:31 +02003531 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003532 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003533 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003534
3535 if (prev_status == 401 || prev_status == 407) {
3536 /* In HTTP keep-alive mode, if we receive a 401, we still have
3537 * a chance of being able to send the visitor again to the same
3538 * server over the same connection. This is required by some
3539 * broken protocols such as NTLM, and anyway whenever there is
3540 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003541 * it's better to do it (at least it helps with debugging), at
3542 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003543 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003544 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003545 }
3546
Willy Tarreau53f96852016-02-02 18:50:47 +01003547 /* Never ever allow to reuse a connection from a non-reuse backend */
3548 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3549 srv_conn->flags |= CO_FL_PRIVATE;
3550
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003551 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003552 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003553
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003554 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003555 s->req.flags |= CF_NEVER_WAIT;
3556 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003557 }
3558
Willy Tarreau714ea782015-11-25 20:11:11 +01003559 /* we're removing the analysers, we MUST re-enable events detection.
3560 * We don't enable close on the response channel since it's either
3561 * already closed, or in keep-alive with an idle connection handler.
3562 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003563 channel_auto_read(&s->req);
3564 channel_auto_close(&s->req);
3565 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003566
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003567 /* we're in keep-alive with an idle connection, monitor it if not already done */
3568 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003569 srv = objt_server(srv_conn->target);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003570 if (srv) {
3571 if (srv_conn->flags & CO_FL_PRIVATE)
3572 LIST_ADD(&srv->priv_conns[tid], &srv_conn->list);
3573 else if (prev_flags & TX_NOT_FIRST)
3574 /* note: we check the request, not the connection, but
3575 * this is valid for strategies SAFE and AGGR, and in
3576 * case of ALWS, we don't care anyway.
3577 */
3578 LIST_ADD(&srv->safe_conns[tid], &srv_conn->list);
3579 else
3580 LIST_ADD(&srv->idle_conns[tid], &srv_conn->list);
3581 }
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003582 }
Christopher Faulete6006242017-03-10 11:52:44 +01003583 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3584 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003585}
3586
3587
3588/* This function updates the request state machine according to the response
3589 * state machine and buffer flags. It returns 1 if it changes anything (flag
3590 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3591 * it is only used to find when a request/response couple is complete. Both
3592 * this function and its equivalent should loop until both return zero. It
3593 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3594 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003595int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003596{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003597 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003598 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003599 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003600 unsigned int old_state = txn->req.msg_state;
3601
Christopher Faulet4be98032017-07-18 10:48:24 +02003602 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003603 return 0;
3604
3605 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003606 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003607 * We can shut the read side unless we want to abort_on_close,
3608 * or we have a POST request. The issue with POST requests is
3609 * that some browsers still send a CRLF after the request, and
3610 * this CRLF must be read so that it does not remain in the kernel
3611 * buffers, otherwise a close could cause an RST on some systems
3612 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003613 * Note that if we're using keep-alive on the client side, we'd
3614 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003615 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003616 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003617 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003618 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3619 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003620 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3621 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003622 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003623 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003624
Willy Tarreau40f151a2012-12-20 12:10:09 +01003625 /* if the server closes the connection, we want to immediately react
3626 * and close the socket to save packets and syscalls.
3627 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003628 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003629
Willy Tarreau7f876a12015-11-18 11:59:55 +01003630 /* In any case we've finished parsing the request so we must
3631 * disable Nagle when sending data because 1) we're not going
3632 * to shut this side, and 2) the server is waiting for us to
3633 * send pending data.
3634 */
3635 chn->flags |= CF_NEVER_WAIT;
3636
Willy Tarreau610ecce2010-01-04 21:15:02 +01003637 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3638 goto wait_other_side;
3639
3640 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3641 /* The server has not finished to respond, so we
3642 * don't want to move in order not to upset it.
3643 */
3644 goto wait_other_side;
3645 }
3646
Willy Tarreau610ecce2010-01-04 21:15:02 +01003647 /* When we get here, it means that both the request and the
3648 * response have finished receiving. Depending on the connection
3649 * mode, we'll have to wait for the last bytes to leave in either
3650 * direction, and sometimes for a close to be effective.
3651 */
3652
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003653 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3654 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003655 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3656 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003657 }
3658 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3659 /* Option forceclose is set, or either side wants to close,
3660 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003661 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003662 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003663 *
3664 * However, there is an exception if the response
3665 * length is undefined. In this case, we need to wait
3666 * the close from the server. The response will be
3667 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003668 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003669 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3670 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3671 goto check_channel_flags;
3672
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003673 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3674 channel_shutr_now(chn);
3675 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003676 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003677 }
3678 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003679 /* The last possible modes are keep-alive and tunnel. Tunnel mode
3680 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003681 */
Willy Tarreau4213a112013-12-15 10:25:42 +01003682 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
3683 channel_auto_read(chn);
3684 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003685 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003686 }
3687
Christopher Faulet4be98032017-07-18 10:48:24 +02003688 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003689 }
3690
3691 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3692 http_msg_closing:
3693 /* nothing else to forward, just waiting for the output buffer
3694 * to be empty and for the shutw_now to take effect.
3695 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003696 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003697 txn->req.msg_state = HTTP_MSG_CLOSED;
3698 goto http_msg_closed;
3699 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003700 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003701 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003702 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003703 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003704 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003705 }
3706
3707 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3708 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01003709 /* if we don't know whether the server will close, we need to hard close */
3710 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
3711 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3712
Willy Tarreau3988d932013-12-27 23:03:08 +01003713 /* see above in MSG_DONE why we only do this in these states */
3714 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3715 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003716 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3717 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01003718 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003719 goto wait_other_side;
3720 }
3721
Christopher Faulet4be98032017-07-18 10:48:24 +02003722 check_channel_flags:
3723 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
3724 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
3725 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02003726 txn->req.msg_state = HTTP_MSG_CLOSING;
3727 goto http_msg_closing;
3728 }
3729
3730
Willy Tarreau610ecce2010-01-04 21:15:02 +01003731 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003732 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003733}
3734
3735
3736/* This function updates the response state machine according to the request
3737 * state machine and buffer flags. It returns 1 if it changes anything (flag
3738 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3739 * it is only used to find when a request/response couple is complete. Both
3740 * this function and its equivalent should loop until both return zero. It
3741 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3742 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003743int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003744{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003745 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003746 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003747 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003748 unsigned int old_state = txn->rsp.msg_state;
3749
Christopher Faulet4be98032017-07-18 10:48:24 +02003750 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003751 return 0;
3752
3753 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3754 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003755 * still monitor the server connection for a possible close
3756 * while the request is being uploaded, so we don't disable
3757 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003758 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003759 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003760
3761 if (txn->req.msg_state == HTTP_MSG_ERROR)
3762 goto wait_other_side;
3763
3764 if (txn->req.msg_state < HTTP_MSG_DONE) {
3765 /* The client seems to still be sending data, probably
3766 * because we got an error response during an upload.
3767 * We have the choice of either breaking the connection
3768 * or letting it pass through. Let's do the later.
3769 */
3770 goto wait_other_side;
3771 }
3772
Willy Tarreau610ecce2010-01-04 21:15:02 +01003773 /* When we get here, it means that both the request and the
3774 * response have finished receiving. Depending on the connection
3775 * mode, we'll have to wait for the last bytes to leave in either
3776 * direction, and sometimes for a close to be effective.
3777 */
3778
3779 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3780 /* Server-close mode : shut read and wait for the request
3781 * side to close its output buffer. The caller will detect
3782 * when we're in DONE and the other is in CLOSED and will
3783 * catch that for the final cleanup.
3784 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003785 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
3786 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003787 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003788 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3789 /* Option forceclose is set, or either side wants to close,
3790 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003791 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003792 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003793 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01003794 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003795 channel_shutr_now(chn);
3796 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003797 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003798 }
3799 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003800 /* The last possible modes are keep-alive and tunnel. Tunnel will
3801 * need to forward remaining data. Keep-alive will need to monitor
3802 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003803 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003804 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02003805 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01003806 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
3807 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003808 }
3809
Christopher Faulet4be98032017-07-18 10:48:24 +02003810 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003811 }
3812
3813 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3814 http_msg_closing:
3815 /* nothing else to forward, just waiting for the output buffer
3816 * to be empty and for the shutw_now to take effect.
3817 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003818 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003819 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3820 goto http_msg_closed;
3821 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003822 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02003823 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003824 txn->rsp.msg_state = HTTP_MSG_ERROR;
Olivier Houcharda798bf52019-03-08 18:52:00 +01003825 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003826 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01003827 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003828 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003829 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003830 }
3831
3832 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3833 http_msg_closed:
3834 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01003835 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003836 channel_auto_close(chn);
3837 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003838 goto wait_other_side;
3839 }
3840
Christopher Faulet4be98032017-07-18 10:48:24 +02003841 check_channel_flags:
3842 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
3843 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
3844 /* if we've just closed an output, let's switch */
3845 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3846 goto http_msg_closing;
3847 }
3848
Willy Tarreau610ecce2010-01-04 21:15:02 +01003849 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02003850 /* We force the response to leave immediately if we're waiting for the
3851 * other side, since there is no pending shutdown to push it out.
3852 */
3853 if (!channel_is_empty(chn))
3854 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003855 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003856}
3857
3858
Christopher Faulet894da4c2017-07-18 11:29:07 +02003859/* Resync the request and response state machines. */
3860void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003861{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003862 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02003863#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01003864 int old_req_state = txn->req.msg_state;
3865 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02003866#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01003867
Willy Tarreau610ecce2010-01-04 21:15:02 +01003868 http_sync_req_state(s);
3869 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003870 if (!http_sync_res_state(s))
3871 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003872 if (!http_sync_req_state(s))
3873 break;
3874 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02003875
Christopher Faulet894da4c2017-07-18 11:29:07 +02003876 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
3877 "req->analysers=0x%08x res->analysers=0x%08x\n",
3878 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02003879 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
3880 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02003881 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02003882
3883
Willy Tarreau610ecce2010-01-04 21:15:02 +01003884 /* OK, both state machines agree on a compatible state.
3885 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01003886 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3887 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02003888 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
3889 * means we must abort the request.
3890 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
3891 * corresponding channel.
3892 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
3893 * on the response with server-close mode means we've completed one
3894 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003895 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02003896 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3897 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01003898 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003899 channel_auto_close(&s->req);
3900 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003901 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003902 channel_auto_close(&s->res);
3903 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003904 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02003905 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3906 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01003907 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01003908 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003909 channel_auto_close(&s->res);
3910 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003911 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003912 channel_abort(&s->req);
3913 channel_auto_close(&s->req);
3914 channel_auto_read(&s->req);
3915 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003916 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02003917 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3918 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3919 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3920 s->req.analysers &= AN_REQ_FLT_END;
3921 if (HAS_REQ_DATA_FILTERS(s))
3922 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
3923 }
3924 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3925 s->res.analysers &= AN_RES_FLT_END;
3926 if (HAS_RSP_DATA_FILTERS(s))
3927 s->res.analysers |= AN_RES_FLT_XFER_DATA;
3928 }
3929 channel_auto_close(&s->req);
3930 channel_auto_read(&s->req);
3931 channel_auto_close(&s->res);
3932 channel_auto_read(&s->res);
3933 }
Willy Tarreau4213a112013-12-15 10:25:42 +01003934 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
3935 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003936 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01003937 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3938 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3939 /* server-close/keep-alive: terminate this transaction,
3940 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02003941 * a fresh-new transaction, but only once we're sure there's
3942 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02003943 * another request. They must not hold any pending output data
3944 * and the response buffer must realigned
3945 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01003946 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003947 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02003948 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02003949 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02003950 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02003951 else {
3952 s->req.analysers = AN_REQ_FLT_END;
3953 s->res.analysers = AN_RES_FLT_END;
3954 txn->flags |= TX_WAIT_CLEANUP;
3955 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003956 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003957}
3958
Willy Tarreaud98cf932009-12-27 22:54:55 +01003959/* This function is an analyser which forwards request body (including chunk
3960 * sizes if any). It is called as soon as we must forward, even if we forward
3961 * zero byte. The only situation where it must not be called is when we're in
3962 * tunnel mode and we want to forward till the close. It's used both to forward
3963 * remaining data and to resync after end of body. It expects the msg_state to
3964 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02003965 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01003966 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02003967 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003968 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003969int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01003970{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003971 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003972 struct http_txn *txn = s->txn;
3973 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01003974 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003975
Christopher Faulete0768eb2018-10-03 16:38:02 +02003976 if (IS_HTX_STRM(s))
3977 return htx_request_forward_body(s, req, an_bit);
3978
Christopher Faulet45073512018-07-20 10:16:29 +02003979 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 +02003980 now_ms, __FUNCTION__,
3981 s,
3982 req,
3983 req->rex, req->wex,
3984 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003985 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02003986 req->analysers);
3987
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003988 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3989 return 0;
3990
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003991 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003992 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02003993 /* Output closed while we were sending data. We must abort and
3994 * wake the other side up.
3995 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003996 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02003997 msg->msg_state = HTTP_MSG_ERROR;
3998 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003999 return 1;
4000 }
4001
Willy Tarreaud98cf932009-12-27 22:54:55 +01004002 /* Note that we don't have to send 100-continue back because we don't
4003 * need the data to complete our job, and it's up to the server to
4004 * decide whether to return 100, 417 or anything else in return of
4005 * an "Expect: 100-continue" header.
4006 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004007 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004008 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4009 ? HTTP_MSG_CHUNK_SIZE
4010 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004011
4012 /* TODO/filters: when http-buffer-request option is set or if a
4013 * rule on url_param exists, the first chunk size could be
4014 * already parsed. In that case, msg->next is after the chunk
4015 * size (including the CRLF after the size). So this case should
4016 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004017 }
4018
Willy Tarreau7ba23542014-04-17 21:50:00 +02004019 /* Some post-connect processing might want us to refrain from starting to
4020 * forward data. Currently, the only reason for this is "balance url_param"
4021 * whichs need to parse/process the request after we've enabled forwarding.
4022 */
4023 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004024 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004025 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004026 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004027 channel_dont_close(req); /* don't fail on early shutr */
4028 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004029 }
4030 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4031 }
4032
Willy Tarreau80a92c02014-03-12 10:41:13 +01004033 /* in most states, we should abort in case of early close */
4034 channel_auto_close(req);
4035
Willy Tarreauefdf0942014-04-24 20:08:57 +02004036 if (req->to_forward) {
4037 /* We can't process the buffer's contents yet */
4038 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004039 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004040 }
4041
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004042 if (msg->msg_state < HTTP_MSG_DONE) {
4043 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4044 ? http_msg_forward_chunked_body(s, msg)
4045 : http_msg_forward_body(s, msg));
4046 if (!ret)
4047 goto missing_data_or_waiting;
4048 if (ret < 0)
4049 goto return_bad_req;
4050 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004051
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004052 /* other states, DONE...TUNNEL */
4053 /* we don't want to forward closes on DONE except in tunnel mode. */
4054 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4055 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004056
Christopher Faulet894da4c2017-07-18 11:29:07 +02004057 http_resync_states(s);
4058 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004059 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4060 if (req->flags & CF_SHUTW) {
4061 /* request errors are most likely due to the
4062 * server aborting the transfer. */
4063 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004064 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004065 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004066 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004067 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004068 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004069 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004070 }
4071
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004072 /* If "option abortonclose" is set on the backend, we want to monitor
4073 * the client's connection and forward any shutdown notification to the
4074 * server, which will decide whether to close or to go on processing the
4075 * request. We only do that in tunnel mode, and not in other modes since
4076 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004077 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004078 channel_auto_read(req);
4079 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4080 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4081 s->si[1].flags |= SI_FL_NOLINGER;
4082 channel_auto_close(req);
4083 }
4084 else if (s->txn->meth == HTTP_METH_POST) {
4085 /* POST requests may require to read extra CRLF sent by broken
4086 * browsers and which could cause an RST to be sent upon close
4087 * on some systems (eg: Linux). */
4088 channel_auto_read(req);
4089 }
4090 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004091
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004092 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004093 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004094 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004095 if (!(s->flags & SF_ERR_MASK))
4096 s->flags |= SF_ERR_CLICL;
4097 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004098 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004099 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004100 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004101 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004102 }
4103
Olivier Houcharda798bf52019-03-08 18:52:00 +01004104 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4105 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004106 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004107 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004108
4109 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004110 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004111
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004112 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004113 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004114 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004115 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004116
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004117 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004118 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004119 * And when content-length is used, we never want to let the possible
4120 * shutdown be forwarded to the other side, as the state machine will
4121 * take care of it once the client responds. It's also important to
4122 * prevent TIME_WAITs from accumulating on the backend side, and for
4123 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004124 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004125 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004126 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004127
Willy Tarreau5c620922011-05-11 19:56:11 +02004128 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004129 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004130 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004131 * modes are already handled by the stream sock layer. We must not do
4132 * this in content-length mode because it could present the MSG_MORE
4133 * flag with the last block of forwarded data, which would cause an
4134 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004135 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004136 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004137 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004138
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004139 return 0;
4140
Willy Tarreaud98cf932009-12-27 22:54:55 +01004141 return_bad_req: /* let's centralize all bad requests */
Olivier Houcharda798bf52019-03-08 18:52:00 +01004142 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004143 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01004144 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004145
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004146 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004147 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004148 txn->req.msg_state = HTTP_MSG_ERROR;
4149 if (txn->status) {
4150 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004151 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004152 } else {
4153 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004154 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004155 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004156 req->analysers &= AN_REQ_FLT_END;
4157 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 +01004158
Willy Tarreaue7dff022015-04-03 01:14:29 +02004159 if (!(s->flags & SF_ERR_MASK))
4160 s->flags |= SF_ERR_PRXCOND;
4161 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004162 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004163 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004164 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004165 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004166 }
4167 return 0;
4168
4169 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004170 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004171 txn->req.msg_state = HTTP_MSG_ERROR;
4172 if (txn->status) {
4173 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004174 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004175 } else {
4176 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004177 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004178 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004179 req->analysers &= AN_REQ_FLT_END;
4180 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 +01004181
Olivier Houcharda798bf52019-03-08 18:52:00 +01004182 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4183 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004184 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004185 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004186
Willy Tarreaue7dff022015-04-03 01:14:29 +02004187 if (!(s->flags & SF_ERR_MASK))
4188 s->flags |= SF_ERR_SRVCL;
4189 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004190 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004191 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004192 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004193 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004194 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004195 return 0;
4196}
4197
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004198/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4199 * processing can continue on next analysers, or zero if it either needs more
4200 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004201 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004202 * when it has nothing left to do, and may remove any analyser when it wants to
4203 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004204 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004205int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004206{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004207 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004208 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004209 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004210 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004211 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004212 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004213 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004214 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004215
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004216 srv_conn = cs_conn(objt_cs(s->si[1].end));
4217
Christopher Faulete0768eb2018-10-03 16:38:02 +02004218 if (IS_HTX_STRM(s))
4219 return htx_wait_for_response(s, rep, an_bit);
4220
Christopher Faulet45073512018-07-20 10:16:29 +02004221 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 +02004222 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004223 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004224 rep,
4225 rep->rex, rep->wex,
4226 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004227 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004228 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004229
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004230 /*
4231 * Now parse the partial (or complete) lines.
4232 * We will check the response syntax, and also join multi-line
4233 * headers. An index of all the lines will be elaborated while
4234 * parsing.
4235 *
4236 * For the parsing, we use a 28 states FSM.
4237 *
4238 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004239 * ci_head(rep) = beginning of response
4240 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4241 * ci_tail(rep) = end of input data
4242 * msg->eol = end of current header or line (LF or CRLF)
4243 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004244 */
4245
Willy Tarreau628c40c2014-04-24 19:11:26 +02004246 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004247 /* There's a protected area at the end of the buffer for rewriting
4248 * purposes. We don't want to start to parse the request if the
4249 * protected area is affected, because we may have to move processed
4250 * data later, which is much more complicated.
4251 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004252 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004253 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004254 /* some data has still not left the buffer, wake us once that's done */
4255 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4256 goto abort_response;
4257 channel_dont_close(rep);
4258 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004259 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004260 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004261 }
4262
Willy Tarreau188e2302018-06-15 11:11:53 +02004263 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004264 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004265 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004266
Willy Tarreauf37954d2018-06-15 18:31:02 +02004267 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004268 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004269 }
4270
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004271 /* 1: we might have to print this header in debug mode */
4272 if (unlikely((global.mode & MODE_DEBUG) &&
4273 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004274 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004275 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004276
Willy Tarreauf37954d2018-06-15 18:31:02 +02004277 sol = ci_head(rep);
4278 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004279 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004280
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004281 sol += hdr_idx_first_pos(&txn->hdr_idx);
4282 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004283
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004284 while (cur_idx) {
4285 eol = sol + txn->hdr_idx.v[cur_idx].len;
4286 debug_hdr("srvhdr", s, sol, eol);
4287 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4288 cur_idx = txn->hdr_idx.v[cur_idx].next;
4289 }
4290 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004291
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004292 /*
4293 * Now we quickly check if we have found a full valid response.
4294 * If not so, we check the FD and buffer states before leaving.
4295 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004296 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004297 * responses are checked first.
4298 *
4299 * Depending on whether the client is still there or not, we
4300 * may send an error response back or not. Note that normally
4301 * we should only check for HTTP status there, and check I/O
4302 * errors somewhere else.
4303 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004304
Willy Tarreau655dce92009-11-08 13:10:58 +01004305 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004306 /* Invalid response */
4307 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4308 /* we detected a parsing error. We want to archive this response
4309 * in the dedicated proxy area for later troubleshooting.
4310 */
4311 hdr_response_bad:
4312 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004313 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004314
Olivier Houcharda798bf52019-03-08 18:52:00 +01004315 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004316 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004317 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004318 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004319 }
Willy Tarreau64648412010-03-05 10:41:54 +01004320 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004321 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004322 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004323 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004324 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004325 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004326 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004327
Willy Tarreaue7dff022015-04-03 01:14:29 +02004328 if (!(s->flags & SF_ERR_MASK))
4329 s->flags |= SF_ERR_PRXCOND;
4330 if (!(s->flags & SF_FINST_MASK))
4331 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004332
4333 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004334 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004335
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004336 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004337 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004338 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004339 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004340 goto hdr_response_bad;
4341 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004342
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004343 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004344 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004345 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004346 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004347 else if (txn->flags & TX_NOT_FIRST)
4348 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004349
Olivier Houcharda798bf52019-03-08 18:52:00 +01004350 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004351 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004352 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004353 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004354 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004355
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004356 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004357 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004358 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004359
4360 /* Check to see if the server refused the early data.
4361 * If so, just send a 425
4362 */
4363 if (objt_cs(s->si[1].end)) {
4364 struct connection *conn = objt_cs(s->si[1].end)->conn;
4365
4366 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4367 txn->status = 425;
4368 }
4369
Willy Tarreau350f4872014-11-28 14:42:25 +01004370 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004371 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004372 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004373
Willy Tarreaue7dff022015-04-03 01:14:29 +02004374 if (!(s->flags & SF_ERR_MASK))
4375 s->flags |= SF_ERR_SRVCL;
4376 if (!(s->flags & SF_FINST_MASK))
4377 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004378 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004379 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004380
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004381 /* read timeout : return a 504 to the client. */
4382 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004383 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004384 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004385
Olivier Houcharda798bf52019-03-08 18:52:00 +01004386 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004387 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004388 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004389 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004390 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004391
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004392 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004393 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004394 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004395 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004396 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004397 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004398
Willy Tarreaue7dff022015-04-03 01:14:29 +02004399 if (!(s->flags & SF_ERR_MASK))
4400 s->flags |= SF_ERR_SRVTO;
4401 if (!(s->flags & SF_FINST_MASK))
4402 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004403 return 0;
4404 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004405
Willy Tarreauf003d372012-11-26 13:35:37 +01004406 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004407 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004408 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4409 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004410 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004411 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004412
Christopher Faulet0184ea72017-01-05 14:06:34 +01004413 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004414 channel_auto_close(rep);
4415
4416 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004417 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004418 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004419
Willy Tarreaue7dff022015-04-03 01:14:29 +02004420 if (!(s->flags & SF_ERR_MASK))
4421 s->flags |= SF_ERR_CLICL;
4422 if (!(s->flags & SF_FINST_MASK))
4423 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004424
Willy Tarreau87b09662015-04-03 00:22:06 +02004425 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004426 return 0;
4427 }
4428
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004429 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004430 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004431 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004432 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004433 else if (txn->flags & TX_NOT_FIRST)
4434 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004435
Olivier Houcharda798bf52019-03-08 18:52:00 +01004436 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004437 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004438 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004439 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004440 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004441
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004442 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004443 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004444 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004445 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004446 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004447 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004448
Willy Tarreaue7dff022015-04-03 01:14:29 +02004449 if (!(s->flags & SF_ERR_MASK))
4450 s->flags |= SF_ERR_SRVCL;
4451 if (!(s->flags & SF_FINST_MASK))
4452 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004453 return 0;
4454 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004455
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004456 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004457 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004458 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004459 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004460 else if (txn->flags & TX_NOT_FIRST)
4461 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004462
Olivier Houcharda798bf52019-03-08 18:52:00 +01004463 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004464 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004465 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004466
Willy Tarreaue7dff022015-04-03 01:14:29 +02004467 if (!(s->flags & SF_ERR_MASK))
4468 s->flags |= SF_ERR_CLICL;
4469 if (!(s->flags & SF_FINST_MASK))
4470 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004471
Willy Tarreau87b09662015-04-03 00:22:06 +02004472 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004473 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004474 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004475
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004476 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004477 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004478 return 0;
4479 }
4480
4481 /* More interesting part now : we know that we have a complete
4482 * response which at least looks like HTTP. We have an indicator
4483 * of each header's length, so we can parse them quickly.
4484 */
4485
4486 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004487 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004488
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004489 /*
4490 * 1: get the status code
4491 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004492 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004493 if (n < 1 || n > 5)
4494 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004495 /* when the client triggers a 4xx from the server, it's most often due
4496 * to a missing object or permission. These events should be tracked
4497 * because if they happen often, it may indicate a brute force or a
4498 * vulnerability scan.
4499 */
4500 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004501 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004502
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004503 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004504 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004505
Willy Tarreau91852eb2015-05-01 13:26:00 +02004506 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4507 * exactly one digit "." one digit. This check may be disabled using
4508 * option accept-invalid-http-response.
4509 */
4510 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4511 if (msg->sl.st.v_l != 8) {
4512 msg->err_pos = 0;
4513 goto hdr_response_bad;
4514 }
4515
Willy Tarreauf37954d2018-06-15 18:31:02 +02004516 if (ci_head(rep)[4] != '/' ||
4517 !isdigit((unsigned char)ci_head(rep)[5]) ||
4518 ci_head(rep)[6] != '.' ||
4519 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004520 msg->err_pos = 4;
4521 goto hdr_response_bad;
4522 }
4523 }
4524
Willy Tarreau5b154472009-12-21 20:11:07 +01004525 /* check if the response is HTTP/1.1 or above */
4526 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004527 ((ci_head(rep)[5] > '1') ||
4528 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004529 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004530
4531 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004532 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 +01004533
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004534 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004535 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004536
Willy Tarreauf37954d2018-06-15 18:31:02 +02004537 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004538
Willy Tarreau39650402010-03-15 19:44:39 +01004539 /* Adjust server's health based on status code. Note: status codes 501
4540 * and 505 are triggered on demand by client request, so we must not
4541 * count them as server failures.
4542 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004543 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004544 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004545 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004546 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004547 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004548 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004549
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004550 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004551 * We may be facing a 100-continue response, or any other informational
4552 * 1xx response which is non-final, in which case this is not the right
4553 * response, and we're waiting for the next one. Let's allow this response
4554 * to go to the client and wait for the next one. There's an exception for
4555 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004556 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004557 if (txn->status < 200 &&
4558 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004559 hdr_idx_init(&txn->hdr_idx);
4560 msg->next -= channel_forward(rep, msg->next);
4561 msg->msg_state = HTTP_MSG_RPBEFORE;
4562 txn->status = 0;
4563 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004564 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004565 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004566 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004567
Willy Tarreaua14ad722017-07-07 11:36:32 +02004568 /*
4569 * 2: check for cacheability.
4570 */
4571
4572 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004573 case 200:
4574 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004575 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004576 case 206:
4577 case 300:
4578 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004579 case 404:
4580 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004581 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004582 case 414:
4583 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004584 break;
4585 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004586 /* RFC7231#6.1:
4587 * Responses with status codes that are defined as
4588 * cacheable by default (e.g., 200, 203, 204, 206,
4589 * 300, 301, 404, 405, 410, 414, and 501 in this
4590 * specification) can be reused by a cache with
4591 * heuristic expiration unless otherwise indicated
4592 * by the method definition or explicit cache
4593 * controls [RFC7234]; all other status codes are
4594 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004595 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004596 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004597 break;
4598 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004599
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004600 /*
4601 * 3: we may need to capture headers
4602 */
4603 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004604 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004605 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4606 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004607
Willy Tarreau557f1992015-05-01 10:05:17 +02004608 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4609 * by RFC7230#3.3.3 :
4610 *
4611 * The length of a message body is determined by one of the following
4612 * (in order of precedence):
4613 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004614 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4615 * the connection will become a tunnel immediately after the empty
4616 * line that concludes the header fields. A client MUST ignore
4617 * any Content-Length or Transfer-Encoding header fields received
4618 * in such a message. Any 101 response (Switching Protocols) is
4619 * managed in the same manner.
4620 *
4621 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004622 * (Informational), 204 (No Content), or 304 (Not Modified) status
4623 * code is always terminated by the first empty line after the
4624 * header fields, regardless of the header fields present in the
4625 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004626 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004627 * 3. If a Transfer-Encoding header field is present and the chunked
4628 * transfer coding (Section 4.1) is the final encoding, the message
4629 * body length is determined by reading and decoding the chunked
4630 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004631 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004632 * If a Transfer-Encoding header field is present in a response and
4633 * the chunked transfer coding is not the final encoding, the
4634 * message body length is determined by reading the connection until
4635 * it is closed by the server. If a Transfer-Encoding header field
4636 * is present in a request and the chunked transfer coding is not
4637 * the final encoding, the message body length cannot be determined
4638 * reliably; the server MUST respond with the 400 (Bad Request)
4639 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004640 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004641 * If a message is received with both a Transfer-Encoding and a
4642 * Content-Length header field, the Transfer-Encoding overrides the
4643 * Content-Length. Such a message might indicate an attempt to
4644 * perform request smuggling (Section 9.5) or response splitting
4645 * (Section 9.4) and ought to be handled as an error. A sender MUST
4646 * remove the received Content-Length field prior to forwarding such
4647 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004648 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004649 * 4. If a message is received without Transfer-Encoding and with
4650 * either multiple Content-Length header fields having differing
4651 * field-values or a single Content-Length header field having an
4652 * invalid value, then the message framing is invalid and the
4653 * recipient MUST treat it as an unrecoverable error. If this is a
4654 * request message, the server MUST respond with a 400 (Bad Request)
4655 * status code and then close the connection. If this is a response
4656 * message received by a proxy, the proxy MUST close the connection
4657 * to the server, discard the received response, and send a 502 (Bad
4658 * Gateway) response to the client. If this is a response message
4659 * received by a user agent, the user agent MUST close the
4660 * connection to the server and discard the received response.
4661 *
4662 * 5. If a valid Content-Length header field is present without
4663 * Transfer-Encoding, its decimal value defines the expected message
4664 * body length in octets. If the sender closes the connection or
4665 * the recipient times out before the indicated number of octets are
4666 * received, the recipient MUST consider the message to be
4667 * incomplete and close the connection.
4668 *
4669 * 6. If this is a request message and none of the above are true, then
4670 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004671 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004672 * 7. Otherwise, this is a response message without a declared message
4673 * body length, so the message body length is determined by the
4674 * number of octets received prior to the server closing the
4675 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004676 */
4677
4678 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004679 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004680 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004681 * FIXME: should we parse anyway and return an error on chunked encoding ?
4682 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004683 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4684 txn->status == 101)) {
4685 /* Either we've established an explicit tunnel, or we're
4686 * switching the protocol. In both cases, we're very unlikely
4687 * to understand the next protocols. We have to switch to tunnel
4688 * mode, so that we transfer the request and responses then let
4689 * this protocol pass unmodified. When we later implement specific
4690 * parsers for such protocols, we'll want to check the Upgrade
4691 * header which contains information about that protocol for
4692 * responses with status 101 (eg: see RFC2817 about TLS).
4693 */
4694 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4695 msg->flags |= HTTP_MSGF_XFER_LEN;
4696 goto end;
4697 }
4698
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004699 if (txn->meth == HTTP_METH_HEAD ||
4700 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004701 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004702 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004703 goto skip_content_length;
4704 }
4705
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004706 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004707 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004708 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004709 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004710 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4711 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004712 /* bad transfer-encoding (chunked followed by something else) */
4713 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004714 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004715 break;
4716 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004717 }
4718
Willy Tarreau1c913912015-04-30 10:57:51 +02004719 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004720 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02004721 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004722 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02004723 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4724 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02004725 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004726 signed long long cl;
4727
Willy Tarreauad14f752011-09-02 20:33:27 +02004728 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004729 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004730 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004731 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004732
Willy Tarreauad14f752011-09-02 20:33:27 +02004733 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004734 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004735 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004736 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004737
Willy Tarreauad14f752011-09-02 20:33:27 +02004738 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004739 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004740 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004741 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004742
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004743 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004744 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004745 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004746 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004747
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004748 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004749 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004750 }
4751
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004752 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
4753 * 407 (Proxy-Authenticate) responses and set the connection to private
4754 */
4755 if (srv_conn && txn->status == 401) {
4756 /* check for Negotiate/NTLM WWW-Authenticate headers */
4757 ctx.idx = 0;
4758 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
4759 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
4760 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
4761 srv_conn->flags |= CO_FL_PRIVATE;
4762 }
4763 } else if (srv_conn && txn->status == 407) {
4764 /* check for Negotiate/NTLM Proxy-Authenticate headers */
4765 ctx.idx = 0;
4766 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
4767 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
4768 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
4769 srv_conn->flags |= CO_FL_PRIVATE;
4770 }
4771 }
4772
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004773 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01004774 /* Now we have to check if we need to modify the Connection header.
4775 * This is more difficult on the response than it is on the request,
4776 * because we can have two different HTTP versions and we don't know
4777 * how the client will interprete a response. For instance, let's say
4778 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4779 * HTTP/1.1 response without any header. Maybe it will bound itself to
4780 * HTTP/1.0 because it only knows about it, and will consider the lack
4781 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4782 * the lack of header as a keep-alive. Thus we will use two flags
4783 * indicating how a request MAY be understood by the client. In case
4784 * of multiple possibilities, we'll fix the header to be explicit. If
4785 * ambiguous cases such as both close and keepalive are seen, then we
4786 * will fall back to explicit close. Note that we won't take risks with
4787 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004788 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004789 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004790 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02004791 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01004792 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004793
Willy Tarreau60466522010-01-18 19:08:45 +01004794 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02004795 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01004796 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004797
Willy Tarreau60466522010-01-18 19:08:45 +01004798 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02004799 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01004800 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004801 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004802 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004803 }
Willy Tarreau60466522010-01-18 19:08:45 +01004804 else { /* SCL / KAL */
4805 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004806 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004807 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004808 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004809
Willy Tarreau60466522010-01-18 19:08:45 +01004810 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004811 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004812
Willy Tarreau60466522010-01-18 19:08:45 +01004813 /* Some keep-alive responses are converted to Server-close if
4814 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004815 */
Willy Tarreau60466522010-01-18 19:08:45 +01004816 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4817 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004818 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004819 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004820 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004821 }
4822
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004823 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02004824 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02004825 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02004826
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004827 /* end of job, return OK */
4828 rep->analysers &= ~an_bit;
4829 rep->analyse_exp = TICK_ETERNITY;
4830 channel_auto_close(rep);
4831 return 1;
4832
4833 abort_keep_alive:
4834 /* A keep-alive request to the server failed on a network error.
4835 * The client is required to retry. We need to close without returning
4836 * any other information so that the client retries.
4837 */
4838 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004839 rep->analysers &= AN_RES_FLT_END;
4840 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004841 channel_auto_close(rep);
4842 s->logs.logwait = 0;
4843 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004844 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01004845 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004846 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004847 return 0;
4848}
4849
4850/* This function performs all the processing enabled for the current response.
4851 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004852 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004853 * other functions. It works like process_request (see indications above).
4854 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004855int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004856{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004857 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004858 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004859 struct http_msg *msg = &txn->rsp;
4860 struct proxy *cur_proxy;
4861 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004862 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004863
Christopher Faulete0768eb2018-10-03 16:38:02 +02004864 if (IS_HTX_STRM(s))
4865 return htx_process_res_common(s, rep, an_bit, px);
4866
Christopher Faulet45073512018-07-20 10:16:29 +02004867 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 +02004868 now_ms, __FUNCTION__,
4869 s,
4870 rep,
4871 rep->rex, rep->wex,
4872 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004873 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004874 rep->analysers);
4875
4876 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
4877 return 0;
4878
Willy Tarreau70730dd2014-04-24 18:06:27 +02004879 /* The stats applet needs to adjust the Connection header but we don't
4880 * apply any filter there.
4881 */
Willy Tarreau612adb82015-03-10 15:25:54 +01004882 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
4883 rep->analysers &= ~an_bit;
4884 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02004885 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01004886 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02004887
Willy Tarreau58975672014-04-24 21:13:57 +02004888 /*
4889 * We will have to evaluate the filters.
4890 * As opposed to version 1.2, now they will be evaluated in the
4891 * filters order and not in the header order. This means that
4892 * each filter has to be validated among all headers.
4893 *
4894 * Filters are tried with ->be first, then with ->fe if it is
4895 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004896 *
4897 * Maybe we are in resume condiion. In this case I choose the
4898 * "struct proxy" which contains the rule list matching the resume
4899 * pointer. If none of theses "struct proxy" match, I initialise
4900 * the process with the first one.
4901 *
4902 * In fact, I check only correspondance betwwen the current list
4903 * pointer and the ->fe rule list. If it doesn't match, I initialize
4904 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02004905 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004906 if (s->current_rule_list == &sess->fe->http_res_rules)
4907 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004908 else
4909 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02004910 while (1) {
4911 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004912
Willy Tarreau58975672014-04-24 21:13:57 +02004913 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02004914 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004915 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004916
Willy Tarreau51d861a2015-05-22 17:30:48 +02004917 if (ret == HTTP_RULE_RES_BADREQ)
4918 goto return_srv_prx_502;
4919
4920 if (ret == HTTP_RULE_RES_DONE) {
4921 rep->analysers &= ~an_bit;
4922 rep->analyse_exp = TICK_ETERNITY;
4923 return 1;
4924 }
4925 }
4926
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004927 /* we need to be called again. */
4928 if (ret == HTTP_RULE_RES_YIELD) {
4929 channel_dont_close(rep);
4930 return 0;
4931 }
4932
Willy Tarreau58975672014-04-24 21:13:57 +02004933 /* try headers filters */
4934 if (rule_set->rsp_exp != NULL) {
4935 if (apply_filters_to_response(s, rep, rule_set) < 0) {
4936 return_bad_resp:
4937 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004938 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004939 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004940 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01004941 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02004942 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004943 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02004944 txn->status = 502;
4945 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01004946 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004947 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004948 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02004949 if (!(s->flags & SF_ERR_MASK))
4950 s->flags |= SF_ERR_PRXCOND;
4951 if (!(s->flags & SF_FINST_MASK))
4952 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02004953 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004954 }
Willy Tarreau58975672014-04-24 21:13:57 +02004955 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004956
Willy Tarreau58975672014-04-24 21:13:57 +02004957 /* has the response been denied ? */
4958 if (txn->flags & TX_SVDENY) {
4959 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004960 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004961
Olivier Houcharda798bf52019-03-08 18:52:00 +01004962 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
4963 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004964 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01004965 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004966
Willy Tarreau58975672014-04-24 21:13:57 +02004967 goto return_srv_prx_502;
4968 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004969
Willy Tarreau58975672014-04-24 21:13:57 +02004970 /* add response headers from the rule sets in the same order */
4971 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02004972 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004973 break;
Willy Tarreau58975672014-04-24 21:13:57 +02004974 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004975 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02004976 ret = acl_pass(ret);
4977 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4978 ret = !ret;
4979 if (!ret)
4980 continue;
4981 }
Christopher Faulet10079f52018-10-03 15:17:28 +02004982 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02004983 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004984 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004985
Willy Tarreau58975672014-04-24 21:13:57 +02004986 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004987 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02004988 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004989 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02004990 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004991
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004992 /* After this point, this anayzer can't return yield, so we can
4993 * remove the bit corresponding to this analyzer from the list.
4994 *
4995 * Note that the intermediate returns and goto found previously
4996 * reset the analyzers.
4997 */
4998 rep->analysers &= ~an_bit;
4999 rep->analyse_exp = TICK_ETERNITY;
5000
Willy Tarreau58975672014-04-24 21:13:57 +02005001 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005002 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005003 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005004
Willy Tarreau58975672014-04-24 21:13:57 +02005005 /*
5006 * Now check for a server cookie.
5007 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005008 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005009 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005010
Willy Tarreau58975672014-04-24 21:13:57 +02005011 /*
5012 * Check for cache-control or pragma headers if required.
5013 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005014 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005015 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005016
Willy Tarreau58975672014-04-24 21:13:57 +02005017 /*
5018 * Add server cookie in the response if needed
5019 */
5020 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5021 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005022 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005023 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5024 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5025 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5026 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5027 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005028 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005029 /* the server is known, it's not the one the client requested, or the
5030 * cookie's last seen date needs to be refreshed. We have to
5031 * insert a set-cookie here, except if we want to insert only on POST
5032 * requests and this one isn't. Note that servers which don't have cookies
5033 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005034 */
Willy Tarreau58975672014-04-24 21:13:57 +02005035 if (!objt_server(s->target)->cookie) {
5036 chunk_printf(&trash,
5037 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5038 s->be->cookie_name);
5039 }
5040 else {
5041 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005042
Willy Tarreau58975672014-04-24 21:13:57 +02005043 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5044 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005045 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5046 s30tob64((date.tv_sec+3) >> 2,
5047 trash.area + trash.data);
5048 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005049
Willy Tarreau58975672014-04-24 21:13:57 +02005050 if (s->be->cookie_maxlife) {
5051 /* emit first_date, which is either the original one or
5052 * the current date.
5053 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005054 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005055 s30tob64(txn->cookie_first_date ?
5056 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005057 (date.tv_sec+3) >> 2,
5058 trash.area + trash.data);
5059 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005060 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005061 }
Willy Tarreau58975672014-04-24 21:13:57 +02005062 chunk_appendf(&trash, "; path=/");
5063 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005064
Willy Tarreau58975672014-04-24 21:13:57 +02005065 if (s->be->cookie_domain)
5066 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005067
Willy Tarreau58975672014-04-24 21:13:57 +02005068 if (s->be->ck_opts & PR_CK_HTTPONLY)
5069 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005070
Willy Tarreau58975672014-04-24 21:13:57 +02005071 if (s->be->ck_opts & PR_CK_SECURE)
5072 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005073
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005074 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005075 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005076
Willy Tarreau58975672014-04-24 21:13:57 +02005077 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005078 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005079 /* the server did not change, only the date was updated */
5080 txn->flags |= TX_SCK_UPDATED;
5081 else
5082 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005083
Willy Tarreau58975672014-04-24 21:13:57 +02005084 /* Here, we will tell an eventual cache on the client side that we don't
5085 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5086 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5087 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005088 */
Willy Tarreau58975672014-04-24 21:13:57 +02005089 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005090
Willy Tarreau58975672014-04-24 21:13:57 +02005091 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005092
Willy Tarreau58975672014-04-24 21:13:57 +02005093 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5094 "Cache-control: private", 22) < 0))
5095 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005096 }
Willy Tarreau58975672014-04-24 21:13:57 +02005097 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005098
Willy Tarreau58975672014-04-24 21:13:57 +02005099 /*
5100 * Check if result will be cacheable with a cookie.
5101 * We'll block the response if security checks have caught
5102 * nasty things such as a cacheable cookie.
5103 */
5104 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5105 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5106 (s->be->options & PR_O_CHK_CACHE)) {
5107 /* we're in presence of a cacheable response containing
5108 * a set-cookie header. We'll block it as requested by
5109 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005110 */
Willy Tarreau58975672014-04-24 21:13:57 +02005111 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01005112 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005113
Olivier Houcharda798bf52019-03-08 18:52:00 +01005114 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5115 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005116 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01005117 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005118
Christopher Faulet767a84b2017-11-24 16:50:31 +01005119 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5120 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005121 send_log(s->be, LOG_ALERT,
5122 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5123 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5124 goto return_srv_prx_502;
5125 }
Willy Tarreau03945942009-12-22 16:50:27 +01005126
Willy Tarreau70730dd2014-04-24 18:06:27 +02005127 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005128 /*
5129 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5130 * If an "Upgrade" token is found, the header is left untouched in order
5131 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005132 * if anything but "Upgrade" is present in the Connection header. We don't
5133 * want to touch any 101 response either since it's switching to another
5134 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005135 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005136 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005137 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005138 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005139
Willy Tarreau58975672014-04-24 21:13:57 +02005140 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5141 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5142 /* we want a keep-alive response here. Keep-alive header
5143 * required if either side is not 1.1.
5144 */
5145 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5146 want_flags |= TX_CON_KAL_SET;
5147 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005148 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005149 /* we want a close response here. Close header required if
5150 * the server is 1.1, regardless of the client.
5151 */
5152 if (msg->flags & HTTP_MSGF_VER_11)
5153 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005154 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005155
Willy Tarreau58975672014-04-24 21:13:57 +02005156 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5157 http_change_connection_header(txn, msg, want_flags);
5158 }
5159
5160 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005161 /* Always enter in the body analyzer */
5162 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5163 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005164
Willy Tarreau58975672014-04-24 21:13:57 +02005165 /* if the user wants to log as soon as possible, without counting
5166 * bytes from the server, then this is the right moment. We have
5167 * to temporarily assign bytes_out to log what we currently have.
5168 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005169 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005170 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5171 s->logs.bytes_out = txn->rsp.eoh;
5172 s->do_log(s);
5173 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005174 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005175 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005176}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005177
Willy Tarreaud98cf932009-12-27 22:54:55 +01005178/* This function is an analyser which forwards response body (including chunk
5179 * sizes if any). It is called as soon as we must forward, even if we forward
5180 * zero byte. The only situation where it must not be called is when we're in
5181 * tunnel mode and we want to forward till the close. It's used both to forward
5182 * remaining data and to resync after end of body. It expects the msg_state to
5183 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005184 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005185 *
5186 * It is capable of compressing response data both in content-length mode and
5187 * in chunked mode. The state machines follows different flows depending on
5188 * whether content-length and chunked modes are used, since there are no
5189 * trailers in content-length :
5190 *
5191 * chk-mode cl-mode
5192 * ,----- BODY -----.
5193 * / \
5194 * V size > 0 V chk-mode
5195 * .--> SIZE -------------> DATA -------------> CRLF
5196 * | | size == 0 | last byte |
5197 * | v final crlf v inspected |
5198 * | TRAILERS -----------> DONE |
5199 * | |
5200 * `----------------------------------------------'
5201 *
5202 * Compression only happens in the DATA state, and must be flushed in final
5203 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5204 * is performed at once on final states for all bytes parsed, or when leaving
5205 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005206 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005207int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005208{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005209 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005210 struct http_txn *txn = s->txn;
5211 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005212 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005213
Christopher Faulete0768eb2018-10-03 16:38:02 +02005214 if (IS_HTX_STRM(s))
5215 return htx_response_forward_body(s, res, an_bit);
5216
Christopher Faulet45073512018-07-20 10:16:29 +02005217 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 +02005218 now_ms, __FUNCTION__,
5219 s,
5220 res,
5221 res->rex, res->wex,
5222 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005223 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005224 res->analysers);
5225
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005226 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5227 return 0;
5228
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005229 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005230 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005231 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005232 /* Output closed while we were sending data. We must abort and
5233 * wake the other side up.
5234 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005235 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005236 msg->msg_state = HTTP_MSG_ERROR;
5237 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005238 return 1;
5239 }
5240
Willy Tarreau4fe41902010-06-07 22:27:41 +02005241 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005242 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005243
Christopher Fauletd7c91962015-04-30 11:48:27 +02005244 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005245 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5246 ? HTTP_MSG_CHUNK_SIZE
5247 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005248 }
5249
Willy Tarreauefdf0942014-04-24 20:08:57 +02005250 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005251 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005252 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005253 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005254 }
5255
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005256 if (msg->msg_state < HTTP_MSG_DONE) {
5257 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5258 ? http_msg_forward_chunked_body(s, msg)
5259 : http_msg_forward_body(s, msg));
5260 if (!ret)
5261 goto missing_data_or_waiting;
5262 if (ret < 0)
5263 goto return_bad_res;
5264 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005265
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005266 /* other states, DONE...TUNNEL */
5267 /* for keep-alive we don't want to forward closes on DONE */
5268 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5269 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5270 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005271
Christopher Faulet894da4c2017-07-18 11:29:07 +02005272 http_resync_states(s);
5273 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005274 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5275 if (res->flags & CF_SHUTW) {
5276 /* response errors are most likely due to the
5277 * client aborting the transfer. */
5278 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005279 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005280 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005281 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005282 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005283 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005284 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005285 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005286 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005287
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005288 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005289 if (res->flags & CF_SHUTW)
5290 goto aborted_xfer;
5291
5292 /* stop waiting for data if the input is closed before the end. If the
5293 * client side was already closed, it means that the client has aborted,
5294 * so we don't want to count this as a server abort. Otherwise it's a
5295 * server abort.
5296 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005297 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005298 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005299 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005300 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005301 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005302 if (!(s->flags & SF_ERR_MASK))
5303 s->flags |= SF_ERR_SRVCL;
Olivier Houcharda798bf52019-03-08 18:52:00 +01005304 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005305 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01005306 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005307 goto return_bad_res_stats_ok;
5308 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005309 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005310
Willy Tarreau40dba092010-03-04 18:14:51 +01005311 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005312 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005313 goto return_bad_res;
5314
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005315 /* When TE: chunked is used, we need to get there again to parse
5316 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005317 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5318 * or if there are filters registered on the stream, we don't want to
5319 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005320 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005321 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005322 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005323 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5324 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005325 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005326
Willy Tarreau5c620922011-05-11 19:56:11 +02005327 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005328 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005329 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005330 * modes are already handled by the stream sock layer. We must not do
5331 * this in content-length mode because it could present the MSG_MORE
5332 * flag with the last block of forwarded data, which would cause an
5333 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005334 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005335 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005336 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005337
Willy Tarreau87b09662015-04-03 00:22:06 +02005338 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005339 return 0;
5340
Willy Tarreau40dba092010-03-04 18:14:51 +01005341 return_bad_res: /* let's centralize all bad responses */
Olivier Houcharda798bf52019-03-08 18:52:00 +01005342 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005343 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01005344 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005345
5346 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005347 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005348 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005349 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005350 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005351 res->analysers &= AN_RES_FLT_END;
5352 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 +01005353 if (objt_server(s->target))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01005354 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005355
Willy Tarreaue7dff022015-04-03 01:14:29 +02005356 if (!(s->flags & SF_ERR_MASK))
5357 s->flags |= SF_ERR_PRXCOND;
5358 if (!(s->flags & SF_FINST_MASK))
5359 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005360 return 0;
5361
5362 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005363 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005364 txn->rsp.msg_state = HTTP_MSG_ERROR;
5365 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005366 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005367 res->analysers &= AN_RES_FLT_END;
5368 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 +01005369
Olivier Houcharda798bf52019-03-08 18:52:00 +01005370 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5371 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005372 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01005373 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005374
Willy Tarreaue7dff022015-04-03 01:14:29 +02005375 if (!(s->flags & SF_ERR_MASK))
5376 s->flags |= SF_ERR_CLICL;
5377 if (!(s->flags & SF_FINST_MASK))
5378 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005379 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005380}
5381
5382
Christopher Faulet10079f52018-10-03 15:17:28 +02005383int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005384{
5385 struct channel *chn = msg->chn;
5386 int ret;
5387
5388 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5389
5390 if (msg->msg_state == HTTP_MSG_ENDING)
5391 goto ending;
5392
5393 /* Neither content-length, nor transfer-encoding was found, so we must
5394 * read the body until the server connection is closed. In that case, we
5395 * eat data as they come. Of course, this happens for response only. */
5396 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005397 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005398 msg->chunk_len += len;
5399 msg->body_len += len;
5400 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005401 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005402 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005403 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005404 msg->next += ret;
5405 msg->chunk_len -= ret;
5406 if (msg->chunk_len) {
5407 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005408 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005409 chn->flags |= CF_WAKE_WRITE;
5410 goto missing_data_or_waiting;
5411 }
5412
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005413 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5414 * always set for a request. */
5415 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5416 /* The server still sending data that should be filtered */
5417 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5418 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005419 msg->msg_state = HTTP_MSG_TUNNEL;
5420 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005421 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005422
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005423 msg->msg_state = HTTP_MSG_ENDING;
5424
5425 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005426 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005427 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005428 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5429 /* default_ret */ msg->next,
5430 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005431 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005432 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005433 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5434 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005435 if (msg->next)
5436 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005437
Christopher Fauletda02e172015-12-04 09:25:05 +01005438 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5439 /* default_ret */ 1,
5440 /* on_error */ goto error,
5441 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005442 if (msg->msg_state == HTTP_MSG_ENDING)
5443 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005444 return 1;
5445
5446 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005447 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005448 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5449 /* default_ret */ msg->next,
5450 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005451 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005452 msg->next -= ret;
5453 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5454 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005455 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005456 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005457 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005458 return 0;
5459 error:
5460 return -1;
5461}
5462
Christopher Faulet10079f52018-10-03 15:17:28 +02005463int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005464{
5465 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005466 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005467 int ret;
5468
5469 /* Here we have the guarantee to be in one of the following state:
5470 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5471 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5472
Christopher Fauletca874b82018-09-20 11:31:01 +02005473 if (msg->msg_state == HTTP_MSG_ENDING)
5474 goto ending;
5475
5476 /* Don't parse chunks if there is no input data */
5477 if (!ci_data(chn))
5478 goto waiting;
5479
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005480 switch_states:
5481 switch (msg->msg_state) {
5482 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005483 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005484 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005485 /* on_error */ goto error);
5486 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005487 msg->chunk_len -= ret;
5488 if (msg->chunk_len) {
5489 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005490 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005491 chn->flags |= CF_WAKE_WRITE;
5492 goto missing_data_or_waiting;
5493 }
5494
5495 /* nothing left to forward for this chunk*/
5496 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5497 /* fall through for HTTP_MSG_CHUNK_CRLF */
5498
5499 case HTTP_MSG_CHUNK_CRLF:
5500 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005501 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005502 if (ret == 0)
5503 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005504 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005505 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005506 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005507 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005508 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005509 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005510 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005511 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5512 /* fall through for HTTP_MSG_CHUNK_SIZE */
5513
5514 case HTTP_MSG_CHUNK_SIZE:
5515 /* read the chunk size and assign it to ->chunk_len,
5516 * then set ->next to point to the body and switch to
5517 * DATA or TRAILERS state.
5518 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005519 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005520 if (ret == 0)
5521 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005522 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005523 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005524 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005525 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005526 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005527 }
5528
5529 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005530 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005531 msg->chunk_len = chunk;
5532 msg->body_len += chunk;
5533
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005534 if (msg->chunk_len) {
5535 msg->msg_state = HTTP_MSG_DATA;
5536 goto switch_states;
5537 }
5538 msg->msg_state = HTTP_MSG_TRAILERS;
5539 /* fall through for HTTP_MSG_TRAILERS */
5540
5541 case HTTP_MSG_TRAILERS:
5542 ret = http_forward_trailers(msg);
5543 if (ret < 0)
5544 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005545 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5546 /* default_ret */ 1,
5547 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005548 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005549 if (!ret)
5550 goto missing_data_or_waiting;
5551 break;
5552
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005553 default:
5554 /* This should no happen in this function */
5555 goto error;
5556 }
5557
5558 msg->msg_state = HTTP_MSG_ENDING;
5559 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005560 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005561 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005562 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005563 /* default_ret */ msg->next,
5564 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005565 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005566 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005567 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5568 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005569 if (msg->next)
5570 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005571
Christopher Fauletda02e172015-12-04 09:25:05 +01005572 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005573 /* default_ret */ 1,
5574 /* on_error */ goto error,
5575 /* on_wait */ goto waiting);
5576 msg->msg_state = HTTP_MSG_DONE;
5577 return 1;
5578
5579 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005580 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005581 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005582 /* default_ret */ msg->next,
5583 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005584 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005585 msg->next -= ret;
5586 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5587 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005588 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005589 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005590 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005591 return 0;
5592
5593 chunk_parsing_error:
5594 if (msg->err_pos >= 0) {
5595 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005596 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005597 msg->msg_state, strm_fe(s));
5598 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005599 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005600 msg, msg->msg_state, s->be);
5601 }
5602 error:
5603 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005604}
5605
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005606
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005607/* Iterate the same filter through all request headers.
5608 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005609 * Since it can manage the switch to another backend, it updates the per-proxy
5610 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005611 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005612int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005613{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005614 char *cur_ptr, *cur_end, *cur_next;
5615 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005616 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005617 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005618 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005619
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005620 last_hdr = 0;
5621
Willy Tarreauf37954d2018-06-15 18:31:02 +02005622 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005623 old_idx = 0;
5624
5625 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005626 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005627 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005628 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005629 (exp->action == ACT_ALLOW ||
5630 exp->action == ACT_DENY ||
5631 exp->action == ACT_TARPIT))
5632 return 0;
5633
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005634 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005635 if (!cur_idx)
5636 break;
5637
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005638 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005639 cur_ptr = cur_next;
5640 cur_end = cur_ptr + cur_hdr->len;
5641 cur_next = cur_end + cur_hdr->cr + 1;
5642
5643 /* Now we have one header between cur_ptr and cur_end,
5644 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005645 */
5646
Willy Tarreau15a53a42015-01-21 13:39:42 +01005647 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005648 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005649 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005650 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005651 last_hdr = 1;
5652 break;
5653
5654 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005655 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005656 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005657 break;
5658
5659 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005660 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005661 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005662 break;
5663
5664 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005665 len = exp_replace(trash.area,
5666 trash.size, cur_ptr,
5667 exp->replace, pmatch);
5668 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005669 return -1;
5670
Willy Tarreau6e27be12018-08-22 04:46:47 +02005671 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5672
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005673 /* FIXME: if the user adds a newline in the replacement, the
5674 * index will not be recalculated for now, and the new line
5675 * will not be counted as a new header.
5676 */
5677
5678 cur_end += delta;
5679 cur_next += delta;
5680 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005681 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005682 break;
5683
5684 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005685 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005686 cur_next += delta;
5687
Willy Tarreaufa355d42009-11-29 18:12:29 +01005688 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005689 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5690 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005691 cur_hdr->len = 0;
5692 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005693 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005694 break;
5695
5696 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005697 }
5698
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005699 /* keep the link from this header to next one in case of later
5700 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005701 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005702 old_idx = cur_idx;
5703 }
5704 return 0;
5705}
5706
5707
5708/* Apply the filter to the request line.
5709 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5710 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005711 * Since it can manage the switch to another backend, it updates the per-proxy
5712 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005713 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005714int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005715{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005716 char *cur_ptr, *cur_end;
5717 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005718 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005719 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005720
Willy Tarreau3d300592007-03-18 18:34:41 +01005721 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005722 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005723 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005724 (exp->action == ACT_ALLOW ||
5725 exp->action == ACT_DENY ||
5726 exp->action == ACT_TARPIT))
5727 return 0;
5728 else if (exp->action == ACT_REMOVE)
5729 return 0;
5730
5731 done = 0;
5732
Willy Tarreauf37954d2018-06-15 18:31:02 +02005733 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005734 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005735
5736 /* Now we have the request line between cur_ptr and cur_end */
5737
Willy Tarreau15a53a42015-01-21 13:39:42 +01005738 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005739 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005740 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005741 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005742 done = 1;
5743 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005744
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005745 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005746 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005747 done = 1;
5748 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005749
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005750 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005751 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005752 done = 1;
5753 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005754
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005755 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005756 len = exp_replace(trash.area, trash.size,
5757 cur_ptr, exp->replace, pmatch);
5758 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005759 return -1;
5760
Willy Tarreau6e27be12018-08-22 04:46:47 +02005761 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5762
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005763 /* FIXME: if the user adds a newline in the replacement, the
5764 * index will not be recalculated for now, and the new line
5765 * will not be counted as a new header.
5766 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005767
Willy Tarreaufa355d42009-11-29 18:12:29 +01005768 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005769 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005770 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005771 HTTP_MSG_RQMETH,
5772 cur_ptr, cur_end + 1,
5773 NULL, NULL);
5774 if (unlikely(!cur_end))
5775 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005776
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005777 /* we have a full request and we know that we have either a CR
5778 * or an LF at <ptr>.
5779 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005780 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5781 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005782 /* there is no point trying this regex on headers */
5783 return 1;
5784 }
5785 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005786 return done;
5787}
Willy Tarreau97de6242006-12-27 17:18:38 +01005788
Willy Tarreau58f10d72006-12-04 02:26:12 +01005789
Willy Tarreau58f10d72006-12-04 02:26:12 +01005790
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005791/*
Willy Tarreau87b09662015-04-03 00:22:06 +02005792 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005793 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005794 * unparsable request. Since it can manage the switch to another backend, it
5795 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005796 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005797int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005798{
Willy Tarreau192252e2015-04-04 01:47:55 +02005799 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005800 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005801 struct hdr_exp *exp;
5802
5803 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005804 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005805
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005806 /*
5807 * The interleaving of transformations and verdicts
5808 * makes it difficult to decide to continue or stop
5809 * the evaluation.
5810 */
5811
Willy Tarreau6c123b12010-01-28 20:22:06 +01005812 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5813 break;
5814
Willy Tarreau3d300592007-03-18 18:34:41 +01005815 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005816 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005817 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005818 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005819
5820 /* if this filter had a condition, evaluate it now and skip to
5821 * next filter if the condition does not match.
5822 */
5823 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005824 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01005825 ret = acl_pass(ret);
5826 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5827 ret = !ret;
5828
5829 if (!ret)
5830 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005831 }
5832
5833 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005834 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005835 if (unlikely(ret < 0))
5836 return -1;
5837
5838 if (likely(ret == 0)) {
5839 /* The filter did not match the request, it can be
5840 * iterated through all headers.
5841 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01005842 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
5843 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005844 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005845 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005846 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005847}
5848
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005849
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005850/* Delete a value in a header between delimiters <from> and <next> in buffer
5851 * <buf>. The number of characters displaced is returned, and the pointer to
5852 * the first delimiter is updated if required. The function tries as much as
5853 * possible to respect the following principles :
5854 * - replace <from> delimiter by the <next> one unless <from> points to a
5855 * colon, in which case <next> is simply removed
5856 * - set exactly one space character after the new first delimiter, unless
5857 * there are not enough characters in the block being moved to do so.
5858 * - remove unneeded spaces before the previous delimiter and after the new
5859 * one.
5860 *
5861 * It is the caller's responsibility to ensure that :
5862 * - <from> points to a valid delimiter or the colon ;
5863 * - <next> points to a valid delimiter or the final CR/LF ;
5864 * - there are non-space chars before <from> ;
5865 * - there is a CR/LF at or after <next>.
5866 */
Willy Tarreauc5a4fd52018-12-11 11:42:27 +01005867static int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005868{
5869 char *prev = *from;
5870
5871 if (*prev == ':') {
5872 /* We're removing the first value, preserve the colon and add a
5873 * space if possible.
5874 */
Willy Tarreau2235b262016-11-05 15:50:20 +01005875 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005876 next++;
5877 prev++;
5878 if (prev < next)
5879 *prev++ = ' ';
5880
Willy Tarreau2235b262016-11-05 15:50:20 +01005881 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005882 next++;
5883 } else {
5884 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01005885 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005886 prev--;
5887 *from = prev;
5888
5889 /* copy the delimiter and if possible a space if we're
5890 * not at the end of the line.
5891 */
Willy Tarreau2235b262016-11-05 15:50:20 +01005892 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005893 *prev++ = *next++;
5894 if (prev + 1 < next)
5895 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01005896 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005897 next++;
5898 }
5899 }
Willy Tarreaue3128022018-07-12 15:55:34 +02005900 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005901}
5902
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005903/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005904 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005905 * desirable to call it only when needed. This code is quite complex because
5906 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5907 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005908 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005909void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005910{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005911 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005912 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005913 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005914 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005915 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5916 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005917
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005918 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005919 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005920 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005921
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005922 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005923 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005924 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005925
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005926 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005927 hdr_beg = hdr_next;
5928 hdr_end = hdr_beg + cur_hdr->len;
5929 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005930
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005931 /* We have one full header between hdr_beg and hdr_end, and the
5932 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005933 * "Cookie:" headers.
5934 */
5935
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005936 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005937 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005938 old_idx = cur_idx;
5939 continue;
5940 }
5941
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005942 del_from = NULL; /* nothing to be deleted */
5943 preserve_hdr = 0; /* assume we may kill the whole header */
5944
Willy Tarreau58f10d72006-12-04 02:26:12 +01005945 /* Now look for cookies. Conforming to RFC2109, we have to support
5946 * attributes whose name begin with a '$', and associate them with
5947 * the right cookie, if we want to delete this cookie.
5948 * So there are 3 cases for each cookie read :
5949 * 1) it's a special attribute, beginning with a '$' : ignore it.
5950 * 2) it's a server id cookie that we *MAY* want to delete : save
5951 * some pointers on it (last semi-colon, beginning of cookie...)
5952 * 3) it's an application cookie : we *MAY* have to delete a previous
5953 * "special" cookie.
5954 * At the end of loop, if a "special" cookie remains, we may have to
5955 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005956 * *MUST* delete it.
5957 *
5958 * Note: RFC2965 is unclear about the processing of spaces around
5959 * the equal sign in the ATTR=VALUE form. A careful inspection of
5960 * the RFC explicitly allows spaces before it, and not within the
5961 * tokens (attrs or values). An inspection of RFC2109 allows that
5962 * too but section 10.1.3 lets one think that spaces may be allowed
5963 * after the equal sign too, resulting in some (rare) buggy
5964 * implementations trying to do that. So let's do what servers do.
5965 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5966 * allowed quoted strings in values, with any possible character
5967 * after a backslash, including control chars and delimitors, which
5968 * causes parsing to become ambiguous. Browsers also allow spaces
5969 * within values even without quotes.
5970 *
5971 * We have to keep multiple pointers in order to support cookie
5972 * removal at the beginning, middle or end of header without
5973 * corrupting the header. All of these headers are valid :
5974 *
5975 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5976 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5977 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5978 * | | | | | | | | |
5979 * | | | | | | | | hdr_end <--+
5980 * | | | | | | | +--> next
5981 * | | | | | | +----> val_end
5982 * | | | | | +-----------> val_beg
5983 * | | | | +--------------> equal
5984 * | | | +----------------> att_end
5985 * | | +---------------------> att_beg
5986 * | +--------------------------> prev
5987 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005988 */
5989
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005990 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
5991 /* Iterate through all cookies on this line */
5992
5993 /* find att_beg */
5994 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01005995 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005996 att_beg++;
5997
5998 /* find att_end : this is the first character after the last non
5999 * space before the equal. It may be equal to hdr_end.
6000 */
6001 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006002
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006003 while (equal < hdr_end) {
6004 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006005 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006006 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006007 continue;
6008 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006009 }
6010
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006011 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6012 * is between <att_beg> and <equal>, both may be identical.
6013 */
6014
6015 /* look for end of cookie if there is an equal sign */
6016 if (equal < hdr_end && *equal == '=') {
6017 /* look for the beginning of the value */
6018 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006019 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006020 val_beg++;
6021
6022 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006023 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006024
6025 /* make val_end point to the first white space or delimitor after the value */
6026 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006027 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006028 val_end--;
6029 } else {
6030 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006031 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006032
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006033 /* We have nothing to do with attributes beginning with '$'. However,
6034 * they will automatically be removed if a header before them is removed,
6035 * since they're supposed to be linked together.
6036 */
6037 if (*att_beg == '$')
6038 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006039
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006040 /* Ignore cookies with no equal sign */
6041 if (equal == next) {
6042 /* This is not our cookie, so we must preserve it. But if we already
6043 * scheduled another cookie for removal, we cannot remove the
6044 * complete header, but we can remove the previous block itself.
6045 */
6046 preserve_hdr = 1;
6047 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006048 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006049 val_end += delta;
6050 next += delta;
6051 hdr_end += delta;
6052 hdr_next += delta;
6053 cur_hdr->len += delta;
6054 http_msg_move_end(&txn->req, delta);
6055 prev = del_from;
6056 del_from = NULL;
6057 }
6058 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006059 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006060
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006061 /* if there are spaces around the equal sign, we need to
6062 * strip them otherwise we'll get trouble for cookie captures,
6063 * or even for rewrites. Since this happens extremely rarely,
6064 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006065 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006066 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6067 int stripped_before = 0;
6068 int stripped_after = 0;
6069
6070 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006071 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006072 equal += stripped_before;
6073 val_beg += stripped_before;
6074 }
6075
6076 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006077 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006078 val_beg += stripped_after;
6079 stripped_before += stripped_after;
6080 }
6081
6082 val_end += stripped_before;
6083 next += stripped_before;
6084 hdr_end += stripped_before;
6085 hdr_next += stripped_before;
6086 cur_hdr->len += stripped_before;
6087 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006088 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006089 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006090
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006091 /* First, let's see if we want to capture this cookie. We check
6092 * that we don't already have a client side cookie, because we
6093 * can only capture one. Also as an optimisation, we ignore
6094 * cookies shorter than the declared name.
6095 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006096 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6097 (val_end - att_beg >= sess->fe->capture_namelen) &&
6098 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006099 int log_len = val_end - att_beg;
6100
Willy Tarreaubafbe012017-11-24 17:34:44 +01006101 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006102 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006103 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006104 if (log_len > sess->fe->capture_len)
6105 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006106 memcpy(txn->cli_cookie, att_beg, log_len);
6107 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006108 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006109 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006110
Willy Tarreaubca99692010-10-06 19:25:55 +02006111 /* Persistence cookies in passive, rewrite or insert mode have the
6112 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006113 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006114 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006115 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006116 * For cookies in prefix mode, the form is :
6117 *
6118 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006119 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006120 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6121 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6122 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006123 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006124
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006125 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6126 * have the server ID between val_beg and delim, and the original cookie between
6127 * delim+1 and val_end. Otherwise, delim==val_end :
6128 *
6129 * Cookie: NAME=SRV; # in all but prefix modes
6130 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6131 * | || || | |+-> next
6132 * | || || | +--> val_end
6133 * | || || +---------> delim
6134 * | || |+------------> val_beg
6135 * | || +-------------> att_end = equal
6136 * | |+-----------------> att_beg
6137 * | +------------------> prev
6138 * +-------------------------> hdr_beg
6139 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006140
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006141 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006142 for (delim = val_beg; delim < val_end; delim++)
6143 if (*delim == COOKIE_DELIM)
6144 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006145 } else {
6146 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006147 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006148 /* Now check if the cookie contains a date field, which would
6149 * appear after a vertical bar ('|') just after the server name
6150 * and before the delimiter.
6151 */
6152 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6153 if (vbar1) {
6154 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006155 * right is the last seen date. It is a base64 encoded
6156 * 30-bit value representing the UNIX date since the
6157 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006158 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006159 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006160 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006161 if (val_end - vbar1 >= 5) {
6162 val = b64tos30(vbar1);
6163 if (val > 0)
6164 txn->cookie_last_date = val << 2;
6165 }
6166 /* look for a second vertical bar */
6167 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6168 if (vbar1 && (val_end - vbar1 > 5)) {
6169 val = b64tos30(vbar1 + 1);
6170 if (val > 0)
6171 txn->cookie_first_date = val << 2;
6172 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006173 }
6174 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006175
Willy Tarreauf64d1412010-10-07 20:06:11 +02006176 /* if the cookie has an expiration date and the proxy wants to check
6177 * it, then we do that now. We first check if the cookie is too old,
6178 * then only if it has expired. We detect strict overflow because the
6179 * time resolution here is not great (4 seconds). Cookies with dates
6180 * in the future are ignored if their offset is beyond one day. This
6181 * allows an admin to fix timezone issues without expiring everyone
6182 * and at the same time avoids keeping unwanted side effects for too
6183 * long.
6184 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006185 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6186 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006187 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006188 txn->flags &= ~TX_CK_MASK;
6189 txn->flags |= TX_CK_OLD;
6190 delim = val_beg; // let's pretend we have not found the cookie
6191 txn->cookie_first_date = 0;
6192 txn->cookie_last_date = 0;
6193 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006194 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6195 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006196 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006197 txn->flags &= ~TX_CK_MASK;
6198 txn->flags |= TX_CK_EXPIRED;
6199 delim = val_beg; // let's pretend we have not found the cookie
6200 txn->cookie_first_date = 0;
6201 txn->cookie_last_date = 0;
6202 }
6203
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006204 /* Here, we'll look for the first running server which supports the cookie.
6205 * This allows to share a same cookie between several servers, for example
6206 * to dedicate backup servers to specific servers only.
6207 * However, to prevent clients from sticking to cookie-less backup server
6208 * when they have incidentely learned an empty cookie, we simply ignore
6209 * empty cookies and mark them as invalid.
6210 * The same behaviour is applied when persistence must be ignored.
6211 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006212 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006213 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006214
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006215 while (srv) {
6216 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6217 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006218 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006219 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006220 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006221 /* we found the server and we can use it */
6222 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006223 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006224 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006225 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006226 break;
6227 } else {
6228 /* we found a server, but it's down,
6229 * mark it as such and go on in case
6230 * another one is available.
6231 */
6232 txn->flags &= ~TX_CK_MASK;
6233 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006234 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006235 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006236 srv = srv->next;
6237 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006238
Willy Tarreauf64d1412010-10-07 20:06:11 +02006239 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006240 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006241 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006242 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006243 txn->flags |= TX_CK_UNUSED;
6244 else
6245 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006246 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006247
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006248 /* depending on the cookie mode, we may have to either :
6249 * - delete the complete cookie if we're in insert+indirect mode, so that
6250 * the server never sees it ;
6251 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlant5ba80252018-11-15 09:25:36 -08006252 * application cookie so that it does not get accidently removed later,
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006253 * if we're in cookie prefix mode
6254 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006255 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006256 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006257
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006258 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006259 val_end += delta;
6260 next += delta;
6261 hdr_end += delta;
6262 hdr_next += delta;
6263 cur_hdr->len += delta;
6264 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006265
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006266 del_from = NULL;
6267 preserve_hdr = 1; /* we want to keep this cookie */
6268 }
6269 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006270 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006271 del_from = prev;
6272 }
6273 } else {
6274 /* This is not our cookie, so we must preserve it. But if we already
6275 * scheduled another cookie for removal, we cannot remove the
6276 * complete header, but we can remove the previous block itself.
6277 */
6278 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006279
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006280 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006281 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006282 if (att_beg >= del_from)
6283 att_beg += delta;
6284 if (att_end >= del_from)
6285 att_end += delta;
6286 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006287 val_end += delta;
6288 next += delta;
6289 hdr_end += delta;
6290 hdr_next += delta;
6291 cur_hdr->len += delta;
6292 http_msg_move_end(&txn->req, delta);
6293 prev = del_from;
6294 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006295 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006296 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006297
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006298 /* continue with next cookie on this header line */
6299 att_beg = next;
6300 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006301
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006302 /* There are no more cookies on this line.
6303 * We may still have one (or several) marked for deletion at the
6304 * end of the line. We must do this now in two ways :
6305 * - if some cookies must be preserved, we only delete from the
6306 * mark to the end of line ;
6307 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006308 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006309 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006310 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006311 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006312 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006313 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006314 cur_hdr->len += delta;
6315 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006316 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006317
6318 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006319 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6320 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006321 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006322 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006323 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006324 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006325 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006326 }
6327
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006328 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006329 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006330 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006331}
6332
6333
Willy Tarreaua15645d2007-03-18 16:22:39 +01006334/* Iterate the same filter through all response headers contained in <rtr>.
6335 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6336 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006337int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006338{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006339 char *cur_ptr, *cur_end, *cur_next;
6340 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006341 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006342 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006343 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006344
6345 last_hdr = 0;
6346
Willy Tarreauf37954d2018-06-15 18:31:02 +02006347 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006348 old_idx = 0;
6349
6350 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006351 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006352 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006353 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006354 (exp->action == ACT_ALLOW ||
6355 exp->action == ACT_DENY))
6356 return 0;
6357
6358 cur_idx = txn->hdr_idx.v[old_idx].next;
6359 if (!cur_idx)
6360 break;
6361
6362 cur_hdr = &txn->hdr_idx.v[cur_idx];
6363 cur_ptr = cur_next;
6364 cur_end = cur_ptr + cur_hdr->len;
6365 cur_next = cur_end + cur_hdr->cr + 1;
6366
6367 /* Now we have one header between cur_ptr and cur_end,
6368 * and the next header starts at cur_next.
6369 */
6370
Willy Tarreau15a53a42015-01-21 13:39:42 +01006371 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006372 switch (exp->action) {
6373 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006374 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006375 last_hdr = 1;
6376 break;
6377
6378 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006379 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006380 last_hdr = 1;
6381 break;
6382
6383 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006384 len = exp_replace(trash.area,
6385 trash.size, cur_ptr,
6386 exp->replace, pmatch);
6387 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006388 return -1;
6389
Willy Tarreau6e27be12018-08-22 04:46:47 +02006390 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6391
Willy Tarreaua15645d2007-03-18 16:22:39 +01006392 /* FIXME: if the user adds a newline in the replacement, the
6393 * index will not be recalculated for now, and the new line
6394 * will not be counted as a new header.
6395 */
6396
6397 cur_end += delta;
6398 cur_next += delta;
6399 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006400 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006401 break;
6402
6403 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006404 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006405 cur_next += delta;
6406
Willy Tarreaufa355d42009-11-29 18:12:29 +01006407 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006408 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6409 txn->hdr_idx.used--;
6410 cur_hdr->len = 0;
6411 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006412 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006413 break;
6414
6415 }
6416 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006417
6418 /* keep the link from this header to next one in case of later
6419 * removal of next header.
6420 */
6421 old_idx = cur_idx;
6422 }
6423 return 0;
6424}
6425
6426
6427/* Apply the filter to the status line in the response buffer <rtr>.
6428 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6429 * or -1 if a replacement resulted in an invalid status line.
6430 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006431int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006432{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006433 char *cur_ptr, *cur_end;
6434 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006435 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006436 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006437
Willy Tarreau3d300592007-03-18 18:34:41 +01006438 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006439 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006440 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006441 (exp->action == ACT_ALLOW ||
6442 exp->action == ACT_DENY))
6443 return 0;
6444 else if (exp->action == ACT_REMOVE)
6445 return 0;
6446
6447 done = 0;
6448
Willy Tarreauf37954d2018-06-15 18:31:02 +02006449 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006450 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006451
6452 /* Now we have the status line between cur_ptr and cur_end */
6453
Willy Tarreau15a53a42015-01-21 13:39:42 +01006454 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006455 switch (exp->action) {
6456 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006457 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006458 done = 1;
6459 break;
6460
6461 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006462 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006463 done = 1;
6464 break;
6465
6466 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006467 len = exp_replace(trash.area, trash.size,
6468 cur_ptr, exp->replace, pmatch);
6469 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006470 return -1;
6471
Willy Tarreau6e27be12018-08-22 04:46:47 +02006472 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6473
Willy Tarreaua15645d2007-03-18 16:22:39 +01006474 /* FIXME: if the user adds a newline in the replacement, the
6475 * index will not be recalculated for now, and the new line
6476 * will not be counted as a new header.
6477 */
6478
Willy Tarreaufa355d42009-11-29 18:12:29 +01006479 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006480 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006481 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006482 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006483 cur_ptr, cur_end + 1,
6484 NULL, NULL);
6485 if (unlikely(!cur_end))
6486 return -1;
6487
6488 /* we have a full respnse and we know that we have either a CR
6489 * or an LF at <ptr>.
6490 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006491 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006492 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006493 /* there is no point trying this regex on headers */
6494 return 1;
6495 }
6496 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006497 return done;
6498}
6499
6500
6501
6502/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006503 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006504 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6505 * unparsable response.
6506 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006507int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006508{
Willy Tarreau192252e2015-04-04 01:47:55 +02006509 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006510 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006511 struct hdr_exp *exp;
6512
6513 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006514 int ret;
6515
6516 /*
6517 * The interleaving of transformations and verdicts
6518 * makes it difficult to decide to continue or stop
6519 * the evaluation.
6520 */
6521
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006522 if (txn->flags & TX_SVDENY)
6523 break;
6524
Willy Tarreau3d300592007-03-18 18:34:41 +01006525 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006526 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6527 exp->action == ACT_PASS)) {
6528 exp = exp->next;
6529 continue;
6530 }
6531
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006532 /* if this filter had a condition, evaluate it now and skip to
6533 * next filter if the condition does not match.
6534 */
6535 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006536 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006537 ret = acl_pass(ret);
6538 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6539 ret = !ret;
6540 if (!ret)
6541 continue;
6542 }
6543
Willy Tarreaua15645d2007-03-18 16:22:39 +01006544 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006545 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006546 if (unlikely(ret < 0))
6547 return -1;
6548
6549 if (likely(ret == 0)) {
6550 /* The filter did not match the response, it can be
6551 * iterated through all headers.
6552 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006553 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6554 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006555 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006556 }
6557 return 0;
6558}
6559
6560
Willy Tarreaua15645d2007-03-18 16:22:39 +01006561/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006562 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006563 * desirable to call it only when needed. This function is also used when we
6564 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006565 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006566void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006567{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006568 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006569 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006570 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006571 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006572 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006573 char *hdr_beg, *hdr_end, *hdr_next;
6574 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006575
Willy Tarreaua15645d2007-03-18 16:22:39 +01006576 /* Iterate through the headers.
6577 * we start with the start line.
6578 */
6579 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006580 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006581
6582 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6583 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006584 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006585
6586 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006587 hdr_beg = hdr_next;
6588 hdr_end = hdr_beg + cur_hdr->len;
6589 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006590
Willy Tarreau24581ba2010-08-31 22:39:35 +02006591 /* We have one full header between hdr_beg and hdr_end, and the
6592 * next header starts at hdr_next. We're only interested in
6593 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006594 */
6595
Willy Tarreau24581ba2010-08-31 22:39:35 +02006596 is_cookie2 = 0;
6597 prev = hdr_beg + 10;
6598 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006599 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006600 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6601 if (!val) {
6602 old_idx = cur_idx;
6603 continue;
6604 }
6605 is_cookie2 = 1;
6606 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006607 }
6608
Willy Tarreau24581ba2010-08-31 22:39:35 +02006609 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6610 * <prev> points to the colon.
6611 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006612 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006613
Willy Tarreau24581ba2010-08-31 22:39:35 +02006614 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6615 * check-cache is enabled) and we are not interested in checking
6616 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006617 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006618 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006619 return;
6620
Willy Tarreau24581ba2010-08-31 22:39:35 +02006621 /* OK so now we know we have to process this response cookie.
6622 * The format of the Set-Cookie header is slightly different
6623 * from the format of the Cookie header in that it does not
6624 * support the comma as a cookie delimiter (thus the header
6625 * cannot be folded) because the Expires attribute described in
6626 * the original Netscape's spec may contain an unquoted date
6627 * with a comma inside. We have to live with this because
6628 * many browsers don't support Max-Age and some browsers don't
6629 * support quoted strings. However the Set-Cookie2 header is
6630 * clean.
6631 *
6632 * We have to keep multiple pointers in order to support cookie
6633 * removal at the beginning, middle or end of header without
6634 * corrupting the header (in case of set-cookie2). A special
6635 * pointer, <scav> points to the beginning of the set-cookie-av
6636 * fields after the first semi-colon. The <next> pointer points
6637 * either to the end of line (set-cookie) or next unquoted comma
6638 * (set-cookie2). All of these headers are valid :
6639 *
6640 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6641 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6642 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6643 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6644 * | | | | | | | | | |
6645 * | | | | | | | | +-> next hdr_end <--+
6646 * | | | | | | | +------------> scav
6647 * | | | | | | +--------------> val_end
6648 * | | | | | +--------------------> val_beg
6649 * | | | | +----------------------> equal
6650 * | | | +------------------------> att_end
6651 * | | +----------------------------> att_beg
6652 * | +------------------------------> prev
6653 * +-----------------------------------------> hdr_beg
6654 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006655
Willy Tarreau24581ba2010-08-31 22:39:35 +02006656 for (; prev < hdr_end; prev = next) {
6657 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006658
Willy Tarreau24581ba2010-08-31 22:39:35 +02006659 /* find att_beg */
6660 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006661 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006662 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006663
Willy Tarreau24581ba2010-08-31 22:39:35 +02006664 /* find att_end : this is the first character after the last non
6665 * space before the equal. It may be equal to hdr_end.
6666 */
6667 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006668
Willy Tarreau24581ba2010-08-31 22:39:35 +02006669 while (equal < hdr_end) {
6670 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6671 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006672 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006673 continue;
6674 att_end = equal;
6675 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006676
Willy Tarreau24581ba2010-08-31 22:39:35 +02006677 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6678 * is between <att_beg> and <equal>, both may be identical.
6679 */
6680
6681 /* look for end of cookie if there is an equal sign */
6682 if (equal < hdr_end && *equal == '=') {
6683 /* look for the beginning of the value */
6684 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006685 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006686 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006687
Willy Tarreau24581ba2010-08-31 22:39:35 +02006688 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006689 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006690
6691 /* make val_end point to the first white space or delimitor after the value */
6692 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006693 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006694 val_end--;
6695 } else {
6696 /* <equal> points to next comma, semi-colon or EOL */
6697 val_beg = val_end = next = equal;
6698 }
6699
6700 if (next < hdr_end) {
6701 /* Set-Cookie2 supports multiple cookies, and <next> points to
6702 * a colon or semi-colon before the end. So skip all attr-value
6703 * pairs and look for the next comma. For Set-Cookie, since
6704 * commas are permitted in values, skip to the end.
6705 */
6706 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02006707 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006708 else
6709 next = hdr_end;
6710 }
6711
6712 /* Now everything is as on the diagram above */
6713
6714 /* Ignore cookies with no equal sign */
6715 if (equal == val_end)
6716 continue;
6717
6718 /* If there are spaces around the equal sign, we need to
6719 * strip them otherwise we'll get trouble for cookie captures,
6720 * or even for rewrites. Since this happens extremely rarely,
6721 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006722 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006723 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6724 int stripped_before = 0;
6725 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006726
Willy Tarreau24581ba2010-08-31 22:39:35 +02006727 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006728 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006729 equal += stripped_before;
6730 val_beg += stripped_before;
6731 }
6732
6733 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006734 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006735 val_beg += stripped_after;
6736 stripped_before += stripped_after;
6737 }
6738
6739 val_end += stripped_before;
6740 next += stripped_before;
6741 hdr_end += stripped_before;
6742 hdr_next += stripped_before;
6743 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006744 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006745 }
6746
6747 /* First, let's see if we want to capture this cookie. We check
6748 * that we don't already have a server side cookie, because we
6749 * can only capture one. Also as an optimisation, we ignore
6750 * cookies shorter than the declared name.
6751 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006752 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006753 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006754 (val_end - att_beg >= sess->fe->capture_namelen) &&
6755 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006756 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006757 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006758 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01006759 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006760 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006761 if (log_len > sess->fe->capture_len)
6762 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01006763 memcpy(txn->srv_cookie, att_beg, log_len);
6764 txn->srv_cookie[log_len] = 0;
6765 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006766 }
6767
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006768 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006769 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006770 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006771 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6772 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006773 /* assume passive cookie by default */
6774 txn->flags &= ~TX_SCK_MASK;
6775 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006776
6777 /* If the cookie is in insert mode on a known server, we'll delete
6778 * this occurrence because we'll insert another one later.
6779 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006780 * a direct access.
6781 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006782 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006783 /* The "preserve" flag was set, we don't want to touch the
6784 * server's cookie.
6785 */
6786 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006787 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006788 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006789 /* this cookie must be deleted */
6790 if (*prev == ':' && next == hdr_end) {
6791 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006792 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006793 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6794 txn->hdr_idx.used--;
6795 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006796 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006797 hdr_next += delta;
6798 http_msg_move_end(&txn->rsp, delta);
6799 /* note: while both invalid now, <next> and <hdr_end>
6800 * are still equal, so the for() will stop as expected.
6801 */
6802 } else {
6803 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006804 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006805 next = prev;
6806 hdr_end += delta;
6807 hdr_next += delta;
6808 cur_hdr->len += delta;
6809 http_msg_move_end(&txn->rsp, delta);
6810 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006811 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006812 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006813 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006814 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006815 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006816 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006817 * with this server since we know it.
6818 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006819 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006820 next += delta;
6821 hdr_end += delta;
6822 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006823 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006824 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006825
Willy Tarreauf1348312010-10-07 15:54:11 +02006826 txn->flags &= ~TX_SCK_MASK;
6827 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006828 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006829 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006830 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006831 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006832 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006833 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006834 next += delta;
6835 hdr_end += delta;
6836 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006837 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006838 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006839
Willy Tarreau827aee92011-03-10 16:55:02 +01006840 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006841 txn->flags &= ~TX_SCK_MASK;
6842 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006843 }
6844 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006845 /* that's done for this cookie, check the next one on the same
6846 * line when next != hdr_end (only if is_cookie2).
6847 */
6848 }
6849 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006850 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006851 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006852}
6853
6854
Willy Tarreaua15645d2007-03-18 16:22:39 +01006855/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006856 * Parses the Cache-Control and Pragma request header fields to determine if
6857 * the request may be served from the cache and/or if it is cacheable. Updates
6858 * s->txn->flags.
6859 */
6860void check_request_for_cacheability(struct stream *s, struct channel *chn)
6861{
6862 struct http_txn *txn = s->txn;
6863 char *p1, *p2;
6864 char *cur_ptr, *cur_end, *cur_next;
6865 int pragma_found;
6866 int cc_found;
6867 int cur_idx;
6868
Christopher Faulet25a02f62018-10-24 12:00:25 +02006869 if (IS_HTX_STRM(s))
6870 return htx_check_request_for_cacheability(s, chn);
6871
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006872 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
6873 return; /* nothing more to do here */
6874
6875 cur_idx = 0;
6876 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006877 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006878
6879 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6880 struct hdr_idx_elem *cur_hdr;
6881 int val;
6882
6883 cur_hdr = &txn->hdr_idx.v[cur_idx];
6884 cur_ptr = cur_next;
6885 cur_end = cur_ptr + cur_hdr->len;
6886 cur_next = cur_end + cur_hdr->cr + 1;
6887
6888 /* We have one full header between cur_ptr and cur_end, and the
6889 * next header starts at cur_next.
6890 */
6891
6892 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6893 if (val) {
6894 if ((cur_end - (cur_ptr + val) >= 8) &&
6895 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6896 pragma_found = 1;
6897 continue;
6898 }
6899 }
6900
William Lallemand8a16fe02018-05-22 11:04:33 +02006901 /* Don't use the cache and don't try to store if we found the
6902 * Authorization header */
6903 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
6904 if (val) {
6905 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6906 txn->flags |= TX_CACHE_IGNORE;
6907 continue;
6908 }
6909
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006910 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6911 if (!val)
6912 continue;
6913
6914 /* OK, right now we know we have a cache-control header at cur_ptr */
6915 cc_found = 1;
6916 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
6917
6918 if (p1 >= cur_end) /* no more info */
6919 continue;
6920
6921 /* p1 is at the beginning of the value */
6922 p2 = p1;
6923 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
6924 p2++;
6925
6926 /* we have a complete value between p1 and p2. We don't check the
6927 * values after max-age, max-stale nor min-fresh, we simply don't
6928 * use the cache when they're specified.
6929 */
6930 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
6931 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
6932 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
6933 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
6934 txn->flags |= TX_CACHE_IGNORE;
6935 continue;
6936 }
6937
6938 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
6939 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6940 continue;
6941 }
6942 }
6943
6944 /* RFC7234#5.4:
6945 * When the Cache-Control header field is also present and
6946 * understood in a request, Pragma is ignored.
6947 * When the Cache-Control header field is not present in a
6948 * request, caches MUST consider the no-cache request
6949 * pragma-directive as having the same effect as if
6950 * "Cache-Control: no-cache" were present.
6951 */
6952 if (!cc_found && pragma_found)
6953 txn->flags |= TX_CACHE_IGNORE;
6954}
6955
6956/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01006957 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006958 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006959void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006960{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006961 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006962 char *p1, *p2;
6963
6964 char *cur_ptr, *cur_end, *cur_next;
6965 int cur_idx;
6966
Christopher Faulet25a02f62018-10-24 12:00:25 +02006967
6968 if (IS_HTX_STRM(s))
6969 return htx_check_response_for_cacheability(s, rtr);
6970
Willy Tarreau12b32f22017-12-21 16:08:09 +01006971 if (txn->status < 200) {
6972 /* do not try to cache interim responses! */
6973 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006974 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01006975 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006976
6977 /* Iterate through the headers.
6978 * we start with the start line.
6979 */
6980 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006981 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006982
6983 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6984 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006985 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006986
6987 cur_hdr = &txn->hdr_idx.v[cur_idx];
6988 cur_ptr = cur_next;
6989 cur_end = cur_ptr + cur_hdr->len;
6990 cur_next = cur_end + cur_hdr->cr + 1;
6991
6992 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01006993 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006994 */
6995
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006996 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6997 if (val) {
6998 if ((cur_end - (cur_ptr + val) >= 8) &&
6999 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7000 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7001 return;
7002 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007003 }
7004
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007005 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7006 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007007 continue;
7008
7009 /* OK, right now we know we have a cache-control header at cur_ptr */
7010
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007011 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007012
7013 if (p1 >= cur_end) /* no more info */
7014 continue;
7015
7016 /* p1 is at the beginning of the value */
7017 p2 = p1;
7018
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007019 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007020 p2++;
7021
7022 /* we have a complete value between p1 and p2 */
7023 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007024 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7025 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7026 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7027 continue;
7028 }
7029
Willy Tarreaua15645d2007-03-18 16:22:39 +01007030 /* we have something of the form no-cache="set-cookie" */
7031 if ((cur_end - p1 >= 21) &&
7032 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7033 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007034 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007035 continue;
7036 }
7037
7038 /* OK, so we know that either p2 points to the end of string or to a comma */
7039 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007040 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007041 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007042 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007043 return;
7044 }
7045
7046 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007047 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007048 continue;
7049 }
7050 }
7051}
7052
Willy Tarreau58f10d72006-12-04 02:26:12 +01007053
Willy Tarreaub2513902006-12-17 14:52:38 +01007054/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007055 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007056 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007057 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007058 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007059 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007060 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007061 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007062 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007063int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007064{
7065 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007066 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007067 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007068
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007069 if (!uri_auth)
7070 return 0;
7071
Cyril Bonté70be45d2010-10-12 00:14:35 +02007072 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007073 return 0;
7074
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007075 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007076 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007077 return 0;
7078
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007079 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007080 return 0;
7081
Willy Tarreaub2513902006-12-17 14:52:38 +01007082 return 1;
7083}
7084
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007085/* Append the description of what is present in error snapshot <es> into <out>.
7086 * The description must be small enough to always fit in a trash. The output
7087 * buffer may be the trash so the trash must not be used inside this function.
7088 */
7089void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7090{
Christopher Fauleted26fb82018-11-29 22:53:30 +01007091 chunk_appendf(out,
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007092 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7093 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7094 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7095 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7096 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7097 es->ctx.http.m_flags, es->ctx.http.m_clen,
7098 es->ctx.http.m_blen, es->ctx.http.b_flags);
7099}
7100
Willy Tarreau4076a152009-04-02 15:18:36 +02007101/*
7102 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007103 * By default it tries to report the error position as msg->err_pos. However if
7104 * this one is not set, it will then report msg->next, which is the last known
7105 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007106 * displays buffers as a contiguous area starting at buf->p. The direction is
7107 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007108 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007109void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007110 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007111 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007112{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007113 union error_snapshot_ctx ctx;
7114 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007115
Willy Tarreauef3ca732018-09-07 15:47:35 +02007116 /* http-specific part now */
7117 ctx.http.sid = s->uniq_id;
7118 ctx.http.state = state;
7119 ctx.http.b_flags = msg->chn->flags;
7120 ctx.http.s_flags = s->flags;
7121 ctx.http.t_flags = s->txn->flags;
7122 ctx.http.m_flags = msg->flags;
7123 ctx.http.m_clen = msg->chunk_len;
7124 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007125
Willy Tarreauef3ca732018-09-07 15:47:35 +02007126 ofs = msg->chn->total - ci_data(msg->chn);
7127 if (ofs < 0)
7128 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007129
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007130 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007131 other_end, s->target,
7132 strm_sess(s), &msg->chn->buf,
7133 ofs, co_data(msg->chn),
7134 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7135 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007136}
Willy Tarreaub2513902006-12-17 14:52:38 +01007137
Willy Tarreaubaaee002006-06-26 02:48:02 +02007138/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007139 * Print a debug line with a header. Always stop at the first CR or LF char,
7140 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7141 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007142 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007143void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007144{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007145 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007146 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007147
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007148 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007149 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007150 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007151 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 +02007152
7153 for (max = 0; start + max < end; max++)
7154 if (start[max] == '\r' || start[max] == '\n')
7155 break;
7156
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007157 UBOUND(max, trash.size - trash.data - 3);
7158 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7159 trash.area[trash.data++] = '\n';
7160 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007161}
7162
Willy Tarreaueee5b512015-04-03 23:46:31 +02007163
7164/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7165 * The hdr_idx is allocated as well. In case of allocation failure, everything
7166 * allocated is freed and NULL is returned. Otherwise the new transaction is
7167 * assigned to the stream and returned.
7168 */
7169struct http_txn *http_alloc_txn(struct stream *s)
7170{
7171 struct http_txn *txn = s->txn;
7172
7173 if (txn)
7174 return txn;
7175
Willy Tarreaubafbe012017-11-24 17:34:44 +01007176 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007177 if (!txn)
7178 return txn;
7179
7180 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007181 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007182 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007183 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007184 return NULL;
7185 }
7186
7187 s->txn = txn;
7188 return txn;
7189}
7190
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007191void http_txn_reset_req(struct http_txn *txn)
7192{
7193 txn->req.flags = 0;
7194 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7195 txn->req.next = 0;
7196 txn->req.chunk_len = 0LL;
7197 txn->req.body_len = 0LL;
7198 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7199}
7200
7201void http_txn_reset_res(struct http_txn *txn)
7202{
7203 txn->rsp.flags = 0;
7204 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7205 txn->rsp.next = 0;
7206 txn->rsp.chunk_len = 0LL;
7207 txn->rsp.body_len = 0LL;
7208 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7209}
7210
Willy Tarreau0937bc42009-12-22 15:03:09 +01007211/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007212 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007213 * the required fields are properly allocated and that we only need to (re)init
7214 * them. This should be used before processing any new request.
7215 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007216void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007217{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007218 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007219 struct proxy *fe = strm_fe(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02007220 struct conn_stream *cs = objt_cs(s->si[0].end);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007221
Christopher Fauletf2824e62018-10-01 12:12:37 +02007222 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
7223 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
7224 : 0);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007225 txn->status = -1;
Willy Tarreauc9036c02019-01-11 19:38:25 +01007226 *(unsigned int *)txn->cache_hash = 0;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007227
Willy Tarreauf64d1412010-10-07 20:06:11 +02007228 txn->cookie_first_date = 0;
7229 txn->cookie_last_date = 0;
7230
Willy Tarreaueee5b512015-04-03 23:46:31 +02007231 txn->srv_cookie = NULL;
7232 txn->cli_cookie = NULL;
7233 txn->uri = NULL;
7234
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007235 http_txn_reset_req(txn);
7236 http_txn_reset_res(txn);
7237
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007238 txn->req.chn = &s->req;
7239 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007240
7241 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007242
7243 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7244 if (fe->options2 & PR_O2_REQBUG_OK)
7245 txn->req.err_pos = -1; /* let buggy requests pass */
7246
Willy Tarreau0937bc42009-12-22 15:03:09 +01007247 if (txn->hdr_idx.v)
7248 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007249
7250 vars_init(&s->vars_txn, SCOPE_TXN);
7251 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007252}
7253
7254/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007255void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007256{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007257 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007258 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007259
7260 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007261 pool_free(pool_head_requri, txn->uri);
7262 pool_free(pool_head_capture, txn->cli_cookie);
7263 pool_free(pool_head_capture, txn->srv_cookie);
7264 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007265
William Lallemanda73203e2012-03-12 12:48:57 +01007266 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007267 txn->uri = NULL;
7268 txn->srv_cookie = NULL;
7269 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007270
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007271 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007272 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007273 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007274 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007275 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007276 }
7277
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007278 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007279 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007280 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007281 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007282 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007283 }
7284
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007285 if (!LIST_ISEMPTY(&s->vars_txn.head))
7286 vars_prune(&s->vars_txn, s->sess, s);
7287 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7288 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007289}
7290
7291/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007292void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007293{
7294 http_end_txn(s);
7295 http_init_txn(s);
7296
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007297 /* reinitialise the current rule list pointer to NULL. We are sure that
7298 * any rulelist match the NULL pointer.
7299 */
7300 s->current_rule_list = NULL;
7301
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007302 s->be = strm_fe(s);
7303 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007304 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007305 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007306 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007307 /* re-init store persistence */
7308 s->store_count = 0;
Olivier Houcharda798bf52019-03-08 18:52:00 +01007309 s->uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007310
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007311 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007312
Willy Tarreau739cfba2010-01-25 23:11:14 +01007313 /* We must trim any excess data from the response buffer, because we
7314 * may have blocked an invalid response from a server that we don't
Joseph Herlant5ba80252018-11-15 09:25:36 -08007315 * want to accidently forward once we disable the analysers, nor do
Willy Tarreau739cfba2010-01-25 23:11:14 +01007316 * we want those data to come along with next response. A typical
7317 * example of such data would be from a buggy server responding to
7318 * a HEAD with some data, or sending more than the advertised
7319 * content-length.
7320 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007321 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007322 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007323
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007324 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007325 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007326
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007327 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007328 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007329
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007330 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007331 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007332
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007333 s->req.rex = TICK_ETERNITY;
7334 s->req.wex = TICK_ETERNITY;
7335 s->req.analyse_exp = TICK_ETERNITY;
7336 s->res.rex = TICK_ETERNITY;
7337 s->res.wex = TICK_ETERNITY;
7338 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007339 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007340}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007341
Willy Tarreau79e57332018-10-02 16:01:16 +02007342/* This function executes one of the set-{method,path,query,uri} actions. It
7343 * takes the string from the variable 'replace' with length 'len', then modifies
7344 * the relevant part of the request line accordingly. Then it updates various
7345 * pointers to the next elements which were moved, and the total buffer length.
7346 * It finds the action to be performed in p[2], previously filled by function
7347 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7348 * error, though this can be revisited when this code is finally exploited.
7349 *
7350 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7351 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007352 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007353 * In query string case, the mark question '?' must be set at the start of the
7354 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007355 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007356int http_replace_req_line(int action, const char *replace, int len,
7357 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007358{
Willy Tarreau79e57332018-10-02 16:01:16 +02007359 struct http_txn *txn = s->txn;
7360 char *cur_ptr, *cur_end;
7361 int offset = 0;
7362 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007363
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007364 if (IS_HTX_STRM(s))
7365 return htx_req_replace_stline(action, replace, len, px, s);
7366
Willy Tarreau79e57332018-10-02 16:01:16 +02007367 switch (action) {
7368 case 0: // method
7369 cur_ptr = ci_head(&s->req);
7370 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007371
Willy Tarreau79e57332018-10-02 16:01:16 +02007372 /* adjust req line offsets and lengths */
7373 delta = len - offset - (cur_end - cur_ptr);
7374 txn->req.sl.rq.m_l += delta;
7375 txn->req.sl.rq.u += delta;
7376 txn->req.sl.rq.v += delta;
7377 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007378
Willy Tarreau79e57332018-10-02 16:01:16 +02007379 case 1: // path
7380 cur_ptr = http_txn_get_path(txn);
7381 if (!cur_ptr)
7382 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007383
Willy Tarreau79e57332018-10-02 16:01:16 +02007384 cur_end = cur_ptr;
7385 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7386 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007387
Willy Tarreau79e57332018-10-02 16:01:16 +02007388 /* adjust req line offsets and lengths */
7389 delta = len - offset - (cur_end - cur_ptr);
7390 txn->req.sl.rq.u_l += delta;
7391 txn->req.sl.rq.v += delta;
7392 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007393
Willy Tarreau79e57332018-10-02 16:01:16 +02007394 case 2: // query
7395 offset = 1;
7396 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7397 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7398 while (cur_ptr < cur_end && *cur_ptr != '?')
7399 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007400
Willy Tarreau79e57332018-10-02 16:01:16 +02007401 /* skip the question mark or indicate that we must insert it
7402 * (but only if the format string is not empty then).
7403 */
7404 if (cur_ptr < cur_end)
7405 cur_ptr++;
7406 else if (len > 1)
7407 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007408
Willy Tarreau79e57332018-10-02 16:01:16 +02007409 /* adjust req line offsets and lengths */
7410 delta = len - offset - (cur_end - cur_ptr);
7411 txn->req.sl.rq.u_l += delta;
7412 txn->req.sl.rq.v += delta;
7413 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007414
Willy Tarreau79e57332018-10-02 16:01:16 +02007415 case 3: // uri
7416 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7417 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007418
Willy Tarreau79e57332018-10-02 16:01:16 +02007419 /* adjust req line offsets and lengths */
7420 delta = len - offset - (cur_end - cur_ptr);
7421 txn->req.sl.rq.u_l += delta;
7422 txn->req.sl.rq.v += delta;
7423 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007424
Willy Tarreau79e57332018-10-02 16:01:16 +02007425 default:
7426 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007427 }
7428
Willy Tarreau79e57332018-10-02 16:01:16 +02007429 /* commit changes and adjust end of message */
7430 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7431 txn->req.sl.rq.l += delta;
7432 txn->hdr_idx.v[0].len += delta;
7433 http_msg_move_end(&txn->req, delta);
7434 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007435}
7436
Willy Tarreau79e57332018-10-02 16:01:16 +02007437/* This function replace the HTTP status code and the associated message. The
7438 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007439 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007440void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007441{
Willy Tarreau79e57332018-10-02 16:01:16 +02007442 struct http_txn *txn = s->txn;
7443 char *cur_ptr, *cur_end;
7444 int delta;
7445 char *res;
7446 int c_l;
7447 const char *msg = reason;
7448 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007449
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007450 if (IS_HTX_STRM(s))
7451 return htx_res_set_status(status, reason, s);
7452
Willy Tarreau79e57332018-10-02 16:01:16 +02007453 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007454
Willy Tarreau79e57332018-10-02 16:01:16 +02007455 res = ultoa_o(status, trash.area, trash.size);
7456 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007457
Willy Tarreau79e57332018-10-02 16:01:16 +02007458 trash.area[c_l] = ' ';
7459 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007460
Willy Tarreau79e57332018-10-02 16:01:16 +02007461 /* Do we have a custom reason format string? */
7462 if (msg == NULL)
7463 msg = http_get_reason(status);
7464 msg_len = strlen(msg);
7465 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7466 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007467
Willy Tarreau79e57332018-10-02 16:01:16 +02007468 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7469 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007470
Willy Tarreau79e57332018-10-02 16:01:16 +02007471 /* commit changes and adjust message */
7472 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7473 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007474
Willy Tarreau79e57332018-10-02 16:01:16 +02007475 /* adjust res line offsets and lengths */
7476 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7477 txn->rsp.sl.st.c_l = c_l;
7478 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007479
Willy Tarreau79e57332018-10-02 16:01:16 +02007480 delta = trash.data - (cur_end - cur_ptr);
7481 txn->rsp.sl.st.l += delta;
7482 txn->hdr_idx.v[0].len += delta;
7483 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007484}
7485
Willy Tarreau58f10d72006-12-04 02:26:12 +01007486/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007487 * Local variables:
7488 * c-indent-level: 8
7489 * c-basic-offset: 8
7490 * End:
7491 */