blob: 4190313d5927f9e8a5acb329df7209b03cf42cbf [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
Willy Tarreau2d3d94c2008-11-30 20:20:08 +010078/* This function handles a server error at the stream interface level. The
79 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +010080 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +010081 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +010082 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +020083 */
Willy Tarreau87b09662015-04-03 00:22:06 +020084static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau83061a82018-07-13 11:56:34 +020085 int err, int finst, const struct buffer *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +020086{
Christopher Faulet9768c262018-10-22 09:34:31 +020087 if (IS_HTX_STRM(s))
88 return htx_server_error(s, si, err, finst, msg);
89
Willy Tarreau2019f952017-03-14 11:07:31 +010090 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +010091 channel_auto_read(si_oc(si));
92 channel_abort(si_oc(si));
93 channel_auto_close(si_oc(si));
94 channel_erase(si_oc(si));
95 channel_auto_close(si_ic(si));
96 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +020097 if (msg)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020098 co_inject(si_ic(si), msg->area, msg->data);
Willy Tarreaue7dff022015-04-03 01:14:29 +020099 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200100 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200101 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200102 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200103}
104
Willy Tarreau87b09662015-04-03 00:22:06 +0200105/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100106 * and message.
107 */
108
Willy Tarreau83061a82018-07-13 11:56:34 +0200109struct buffer *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100110{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200111 const int msgnum = http_get_status_idx(s->txn->status);
112
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100113 if (IS_HTX_STRM(s))
114 return htx_error_message(s);
115
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200116 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200117 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200118 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200119 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100120 else
121 return &http_err_chunks[msgnum];
122}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200123
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100124void
Willy Tarreau83061a82018-07-13 11:56:34 +0200125http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100126{
Christopher Faulet9768c262018-10-22 09:34:31 +0200127 if (IS_HTX_STRM(s))
128 return htx_reply_and_close(s, status, msg);
129
Christopher Fauletd7c91962015-04-30 11:48:27 +0200130 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100131 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100132 si_retnclose(&s->si[0], msg);
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100133}
134
Willy Tarreau21d2af32008-02-14 20:25:24 +0100135/* Parse the URI from the given transaction (which is assumed to be in request
136 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
137 * It is returned otherwise.
138 */
Willy Tarreau6b952c82018-09-10 17:45:34 +0200139char *http_txn_get_path(const struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100140{
Willy Tarreau6b952c82018-09-10 17:45:34 +0200141 struct ist ret;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100142
Willy Tarreau9d9ccdb2018-10-28 20:13:12 +0100143 if (!txn->req.chn->buf.size)
144 return NULL;
145
Willy Tarreau6b952c82018-09-10 17:45:34 +0200146 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 +0100147
Willy Tarreau6b952c82018-09-10 17:45:34 +0200148 return ret.ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +0100149}
150
Willy Tarreau71241ab2012-12-27 11:30:54 +0100151/* Returns a 302 for a redirectable request that reaches a server working in
152 * in redirect mode. This may only be called just after the stream interface
153 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
154 * follow normal proxy processing. NOTE: this function is designed to support
155 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100156 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200157void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100158{
159 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100160 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100161 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200162 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100163
Christopher Fauletfefc73d2018-10-24 21:18:04 +0200164 if (IS_HTX_STRM(s))
165 return htx_perform_server_redirect(s, si);
166
Willy Tarreauefb453c2008-10-26 20:49:47 +0100167 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200168 trash.data = strlen(HTTP_302);
169 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100170
Willy Tarreaub05e48a2018-09-20 11:12:58 +0200171 srv = __objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100172
Willy Tarreauefb453c2008-10-26 20:49:47 +0100173 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200174 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100175 return;
176
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100177 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100178 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200179 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
180 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100181 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100182
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200183 /* 3: add the request URI. Since it was already forwarded, we need
184 * to temporarily rewind the buffer.
185 */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200186 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200187 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200188
Willy Tarreau6b952c82018-09-10 17:45:34 +0200189 path = http_txn_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200190 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 +0200191
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200192 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200193
Willy Tarreauefb453c2008-10-26 20:49:47 +0100194 if (!path)
195 return;
196
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200197 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100198 return;
199
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200200 memcpy(trash.area + trash.data, path, len);
201 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100202
203 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200204 memcpy(trash.area + trash.data,
205 "\r\nProxy-Connection: close\r\n\r\n", 29);
206 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100207 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200208 memcpy(trash.area + trash.data,
209 "\r\nConnection: close\r\n\r\n", 23);
210 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100211 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100212
213 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200214 si_shutr(si);
215 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100216 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100217 si->state = SI_ST_CLO;
218
219 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +0100220 txn->status = 302;
221 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100222
223 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100224 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500225 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100226}
227
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100228/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100229 * that the server side is closed. Note that err_type is actually a
230 * bitmask, where almost only aborts may be cumulated with other
231 * values. We consider that aborted operations are more important
232 * than timeouts or errors due to the fact that nobody else in the
233 * logs might explain incomplete retries. All others should avoid
234 * being cumulated. It should normally not be possible to have multiple
235 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100236 * Note that connection errors appearing on the second request of a keep-alive
237 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100238 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200239void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100240{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100241 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100242
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200243 /* set s->txn->status for http_error_message(s) */
244 s->txn->status = 503;
245
Willy Tarreauefb453c2008-10-26 20:49:47 +0100246 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200247 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100248 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100249 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200250 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100251 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200252 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100253 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200254 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100255 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100256 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200257 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100258 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100259 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200260 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100261 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200262 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100263 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200264 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100265 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200266 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200267 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200268 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100269 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200270 http_error_message(s));
271 else { /* SI_ET_CONN_OTHER and others */
272 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200273 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100274 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200275 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100276}
277
Willy Tarreau42250582007-04-01 01:30:43 +0200278extern const char sess_term_cond[8];
279extern const char sess_fin_state[8];
280extern const char *monthname[12];
Willy Tarreau8ceae722018-11-26 11:58:30 +0100281
282DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
283DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
284
285struct pool_head *pool_head_requri = NULL;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100286struct pool_head *pool_head_capture = NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100287
Willy Tarreau117f59e2007-03-04 18:17:17 +0100288/*
289 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +0200290 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +0100291 */
Christopher Faulet10079f52018-10-03 15:17:28 +0200292void http_capture_headers(char *som, struct hdr_idx *idx,
293 char **cap, struct cap_hdr *cap_hdr)
Willy Tarreau117f59e2007-03-04 18:17:17 +0100294{
295 char *eol, *sol, *col, *sov;
296 int cur_idx;
297 struct cap_hdr *h;
298 int len;
299
300 sol = som + hdr_idx_first_pos(idx);
301 cur_idx = hdr_idx_first_idx(idx);
302
303 while (cur_idx) {
304 eol = sol + idx->v[cur_idx].len;
305
306 col = sol;
307 while (col < eol && *col != ':')
308 col++;
309
310 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100311 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +0100312 sov++;
313
314 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +0200315 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +0100316 (strncasecmp(sol, h->name, h->namelen) == 0)) {
317 if (cap[h->index] == NULL)
318 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +0100319 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100320
321 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100322 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +0100323 continue;
324 }
325
326 len = eol - sov;
327 if (len > h->len)
328 len = h->len;
329
330 memcpy(cap[h->index], sov, len);
331 cap[h->index][len]=0;
332 }
333 }
334 sol = eol + idx->v[cur_idx].cr + 1;
335 cur_idx = idx->v[cur_idx].next;
336 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100337}
338
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200339/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
340 * conversion succeeded, 0 in case of error. If the request was already 1.X,
341 * nothing is done and 1 is returned.
342 */
Willy Tarreau79e57332018-10-02 16:01:16 +0200343int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200344{
345 int delta;
346 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100347 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200348
349 if (msg->sl.rq.v_l != 0)
350 return 1;
351
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300352 /* RFC 1945 allows only GET for HTTP/0.9 requests */
353 if (txn->meth != HTTP_METH_GET)
354 return 0;
355
Willy Tarreauf37954d2018-06-15 18:31:02 +0200356 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200357
358 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300359 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
360 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200361 }
362 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200363 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +0100364 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200365 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200366 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200367 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +0200368 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200369 NULL, NULL);
370 if (unlikely(!cur_end))
371 return 0;
372
373 /* we have a full HTTP/1.0 request now and we know that
374 * we have either a CR or an LF at <ptr>.
375 */
376 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
377 return 1;
378}
379
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100380/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100381 * and "keep-alive" values. If we already know that some headers may safely
382 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100383 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
384 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +0100385 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100386 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
387 * found, and TX_CON_*_SET is adjusted depending on what is left so only
388 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100389 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +0100390 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100391void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +0100392{
Willy Tarreau5b154472009-12-21 20:11:07 +0100393 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100394 const char *hdr_val = "Connection";
395 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +0100396
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100397 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +0100398 return;
399
Willy Tarreau88d349d2010-01-25 12:15:43 +0100400 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
401 hdr_val = "Proxy-Connection";
402 hdr_len = 16;
403 }
404
Willy Tarreau5b154472009-12-21 20:11:07 +0100405 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100406 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200407 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100408 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
409 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100410 if (to_del & 2)
411 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100412 else
413 txn->flags |= TX_CON_KAL_SET;
414 }
415 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
416 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100417 if (to_del & 1)
418 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100419 else
420 txn->flags |= TX_CON_CLO_SET;
421 }
Willy Tarreau50fc7772012-11-11 22:19:57 +0100422 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
423 txn->flags |= TX_HDR_CONN_UPG;
424 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100425 }
426
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100427 txn->flags |= TX_HDR_CONN_PRS;
428 return;
429}
Willy Tarreau5b154472009-12-21 20:11:07 +0100430
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100431/* Apply desired changes on the Connection: header. Values may be removed and/or
432 * added depending on the <wanted> flags, which are exclusively composed of
433 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
434 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
435 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100436void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100437{
438 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100439 const char *hdr_val = "Connection";
440 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100441
442 ctx.idx = 0;
443
Willy Tarreau88d349d2010-01-25 12:15:43 +0100444
445 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
446 hdr_val = "Proxy-Connection";
447 hdr_len = 16;
448 }
449
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100450 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200451 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100452 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
453 if (wanted & TX_CON_KAL_SET)
454 txn->flags |= TX_CON_KAL_SET;
455 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100456 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +0100457 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100458 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
459 if (wanted & TX_CON_CLO_SET)
460 txn->flags |= TX_CON_CLO_SET;
461 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100462 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +0100463 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100464 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100465
466 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
467 return;
468
469 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
470 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100471 hdr_val = "Connection: close";
472 hdr_len = 17;
473 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
474 hdr_val = "Proxy-Connection: close";
475 hdr_len = 23;
476 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100477 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100478 }
479
480 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
481 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100482 hdr_val = "Connection: keep-alive";
483 hdr_len = 22;
484 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
485 hdr_val = "Proxy-Connection: keep-alive";
486 hdr_len = 28;
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 return;
Willy Tarreau5b154472009-12-21 20:11:07 +0100491}
492
Willy Tarreau87b09662015-04-03 00:22:06 +0200493void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200494{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200495 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200496 int tmp = TX_CON_WANT_KAL;
497
Christopher Fauletf2824e62018-10-01 12:12:37 +0200498 if (IS_HTX_STRM(s))
Christopher Faulet0f226952018-10-22 09:29:56 +0200499 return htx_adjust_conn_mode(s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200500
Christopher Faulet315b39c2018-09-21 16:26:19 +0200501 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
502 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
503 tmp = TX_CON_WANT_TUN;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200504
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200505 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Faulet315b39c2018-09-21 16:26:19 +0200506 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200507 tmp = TX_CON_WANT_SCL;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200508
Christopher Faulet315b39c2018-09-21 16:26:19 +0200509 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
510 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200511 tmp = TX_CON_WANT_CLO;
512
513 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
514 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
515
516 if (!(txn->flags & TX_HDR_CONN_PRS) &&
517 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
518 /* parse the Connection header and possibly clean it */
519 int to_del = 0;
520 if ((msg->flags & HTTP_MSGF_VER_11) ||
521 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200522 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200523 to_del |= 2; /* remove "keep-alive" */
524 if (!(msg->flags & HTTP_MSGF_VER_11))
525 to_del |= 1; /* remove "close" */
526 http_parse_connection_header(txn, msg, to_del);
527 }
528
529 /* check if client or config asks for explicit close in KAL/SCL */
530 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
531 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
532 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
533 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
534 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200535 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200536 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
537}
William Lallemand82fe75c2012-10-23 10:25:10 +0200538
Willy Tarreaud787e662009-07-07 10:14:51 +0200539/* This stream analyser waits for a complete HTTP request. It returns 1 if the
540 * processing can continue on next analysers, or zero if it either needs more
541 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100542 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200543 * when it has nothing left to do, and may remove any analyser when it wants to
544 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100545 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200546int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100547{
Willy Tarreau59234e92008-11-30 23:51:27 +0100548 /*
549 * We will parse the partial (or complete) lines.
550 * We will check the request syntax, and also join multi-line
551 * headers. An index of all the lines will be elaborated while
552 * parsing.
553 *
554 * For the parsing, we use a 28 states FSM.
555 *
556 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200557 * ci_head(req) = beginning of request
558 * ci_head(req) + msg->eoh = end of processed headers / start of current one
559 * ci_tail(req) = end of input data
560 * msg->eol = end of current header or line (LF or CRLF)
561 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200562 *
563 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200564 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200565 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
566 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100567 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100568
Willy Tarreau59234e92008-11-30 23:51:27 +0100569 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200570 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200571 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100572 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200573 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100574
Christopher Faulete0768eb2018-10-03 16:38:02 +0200575 if (IS_HTX_STRM(s))
576 return htx_wait_for_request(s, req, an_bit);
577
Christopher Faulet45073512018-07-20 10:16:29 +0200578 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 +0100579 now_ms, __FUNCTION__,
580 s,
581 req,
582 req->rex, req->wex,
583 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200584 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100585 req->analysers);
586
Willy Tarreau52a0c602009-08-16 22:45:38 +0200587 /* we're speaking HTTP here, so let's speak HTTP to the client */
588 s->srv_error = http_return_srv_error;
589
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300590 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200591 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300592 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
593
Willy Tarreau83e3af02009-12-28 17:39:57 +0100594 /* There's a protected area at the end of the buffer for rewriting
595 * purposes. We don't want to start to parse the request if the
596 * protected area is affected, because we may have to move processed
597 * data later, which is much more complicated.
598 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200599 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200600 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100601 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200602 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100603 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100604 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200605 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200606 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100607 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100608 return 0;
609 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200610 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200611 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200612 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100613 }
614
Willy Tarreauf37954d2018-06-15 18:31:02 +0200615 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100616 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100617 }
618
Willy Tarreau59234e92008-11-30 23:51:27 +0100619 /* 1: we might have to print this header in debug mode */
620 if (unlikely((global.mode & MODE_DEBUG) &&
621 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200622 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100623 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100624
Willy Tarreauf37954d2018-06-15 18:31:02 +0200625 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200626 /* this is a bit complex : in case of error on the request line,
627 * we know that rq.l is still zero, so we display only the part
628 * up to the end of the line (truncated by debug_hdr).
629 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200630 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100631 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100632
Willy Tarreau59234e92008-11-30 23:51:27 +0100633 sol += hdr_idx_first_pos(&txn->hdr_idx);
634 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100635
Willy Tarreau59234e92008-11-30 23:51:27 +0100636 while (cur_idx) {
637 eol = sol + txn->hdr_idx.v[cur_idx].len;
638 debug_hdr("clihdr", s, sol, eol);
639 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
640 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100641 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100642 }
643
Willy Tarreau58f10d72006-12-04 02:26:12 +0100644
Willy Tarreau59234e92008-11-30 23:51:27 +0100645 /*
646 * Now we quickly check if we have found a full valid request.
647 * If not so, we check the FD and buffer states before leaving.
648 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100649 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100650 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200651 * on a keep-alive stream, if we encounter and error, close, t/o,
652 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100653 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200654 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200655 * Last, we may increase some tracked counters' http request errors on
656 * the cases that are deliberately the client's fault. For instance,
657 * a timeout or connection reset is not counted as an error. However
658 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100659 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100660
Willy Tarreau655dce92009-11-08 13:10:58 +0100661 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100662 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100663 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100664 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100665 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200666 stream_inc_http_req_ctr(s);
667 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200668 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100669 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100670 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100671
Willy Tarreau59234e92008-11-30 23:51:27 +0100672 /* 1: Since we are in header mode, if there's no space
673 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200674 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100675 * must terminate it now.
676 */
Willy Tarreau23752332018-06-15 14:54:53 +0200677 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100678 /* FIXME: check if URI is set and return Status
679 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100680 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200681 stream_inc_http_req_ctr(s);
682 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200683 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +0200684 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +0200685 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +0100686 goto return_bad_req;
687 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100688
Willy Tarreau59234e92008-11-30 23:51:27 +0100689 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200690 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200691 if (!(s->flags & SF_ERR_MASK))
692 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100693
Willy Tarreaufcffa692010-01-10 14:21:19 +0100694 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100695 goto failed_keep_alive;
696
Willy Tarreau0f228a02015-05-01 15:37:53 +0200697 if (sess->fe->options & PR_O_IGNORE_PRB)
698 goto failed_keep_alive;
699
Willy Tarreau59234e92008-11-30 23:51:27 +0100700 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +0200701 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200702 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +0200703 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200704 }
705
Willy Tarreaudc979f22012-12-04 10:39:01 +0100706 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +0100707 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +0100708 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100709 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +0100710 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +0200711 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200712 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100713 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200714 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100715 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200716
Willy Tarreaue7dff022015-04-03 01:14:29 +0200717 if (!(s->flags & SF_FINST_MASK))
718 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +0100719 return 0;
720 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200721
Willy Tarreau59234e92008-11-30 23:51:27 +0100722 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200723 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200724 if (!(s->flags & SF_ERR_MASK))
725 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100726
Willy Tarreaufcffa692010-01-10 14:21:19 +0100727 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100728 goto failed_keep_alive;
729
Willy Tarreau0f228a02015-05-01 15:37:53 +0200730 if (sess->fe->options & PR_O_IGNORE_PRB)
731 goto failed_keep_alive;
732
Willy Tarreau59234e92008-11-30 23:51:27 +0100733 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +0200734 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200735 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +0200736 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200737 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100738 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +0100739 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +0100740 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200741 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +0100742 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200743
Willy Tarreau87b09662015-04-03 00:22:06 +0200744 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200745 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100746 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200747 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100748 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200749
Willy Tarreaue7dff022015-04-03 01:14:29 +0200750 if (!(s->flags & SF_FINST_MASK))
751 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +0100752 return 0;
753 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200754
Willy Tarreau59234e92008-11-30 23:51:27 +0100755 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200756 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200757 if (!(s->flags & SF_ERR_MASK))
758 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100759
Willy Tarreaufcffa692010-01-10 14:21:19 +0100760 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100761 goto failed_keep_alive;
762
Willy Tarreau0f228a02015-05-01 15:37:53 +0200763 if (sess->fe->options & PR_O_IGNORE_PRB)
764 goto failed_keep_alive;
765
Willy Tarreau4076a152009-04-02 15:18:36 +0200766 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200767 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100768 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +0100769 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +0100770 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200771 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +0100772 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +0200773 stream_inc_http_err_ctr(s);
774 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200775 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100776 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200777 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100778 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200779
Willy Tarreaue7dff022015-04-03 01:14:29 +0200780 if (!(s->flags & SF_FINST_MASK))
781 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +0200782 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100783 }
784
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200785 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200786 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100787 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100788
789 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req)) {
Willy Tarreau5e205522011-12-17 16:34:27 +0100790 /* We need more data, we have to re-enable quick-ack in case we
791 * previously disabled it, otherwise we might cause the client
792 * to delay next data.
793 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100794 conn_set_quickack(objt_conn(sess->origin), 1);
Willy Tarreau5e205522011-12-17 16:34:27 +0100795 }
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100796
Willy Tarreaufcffa692010-01-10 14:21:19 +0100797 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
798 /* If the client starts to talk, let's fall back to
799 * request timeout processing.
800 */
801 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +0100802 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +0100803 }
804
Willy Tarreau59234e92008-11-30 23:51:27 +0100805 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +0100806 if (!tick_isset(req->analyse_exp)) {
807 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
808 (txn->flags & TX_WAIT_NEXT_RQ) &&
809 tick_isset(s->be->timeout.httpka))
810 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
811 else
812 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
813 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100814
Willy Tarreau59234e92008-11-30 23:51:27 +0100815 /* we're not ready yet */
816 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +0100817
818 failed_keep_alive:
819 /* Here we process low-level errors for keep-alive requests. In
820 * short, if the request is not the first one and it experiences
821 * a timeout, read error or shutdown, we just silently close so
822 * that the client can try again.
823 */
824 txn->status = 0;
825 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +0100826 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +0100827 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +0200828 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100829 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100830 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +0100831 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +0100832 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100833
Willy Tarreaud787e662009-07-07 10:14:51 +0200834 /* OK now we have a complete HTTP request with indexed headers. Let's
835 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200836 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +0100837 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +0100838 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +0100839 * byte after the last LF. msg->sov points to the first byte of data.
840 * msg->eol cannot be trusted because it may have been left uninitialized
841 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +0200842 */
Willy Tarreau9cdde232007-05-02 20:58:19 +0200843
Willy Tarreau87b09662015-04-03 00:22:06 +0200844 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200845 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +0100846
Willy Tarreaub16a5742010-01-10 14:46:16 +0100847 if (txn->flags & TX_WAIT_NEXT_RQ) {
848 /* kill the pending keep-alive timeout */
849 txn->flags &= ~TX_WAIT_NEXT_RQ;
850 req->analyse_exp = TICK_ETERNITY;
851 }
852
853
Willy Tarreaud787e662009-07-07 10:14:51 +0200854 /* Maybe we found in invalid header name while we were configured not
855 * to block on that, so we have to capture it now.
856 */
857 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200858 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +0200859
Willy Tarreau59234e92008-11-30 23:51:27 +0100860 /*
861 * 1: identify the method
862 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200863 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +0100864
865 /* we can make use of server redirect on GET and HEAD */
866 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200867 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +0100868 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +0200869 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +0100870 /* PRI is reserved for the HTTP/2 preface */
871 msg->err_pos = 0;
872 goto return_bad_req;
873 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200874
Willy Tarreau59234e92008-11-30 23:51:27 +0100875 /*
876 * 2: check if the URI matches the monitor_uri.
877 * We have to do this for every request which gets in, because
878 * the monitor-uri is defined by the frontend.
879 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200880 if (unlikely((sess->fe->monitor_uri_len != 0) &&
881 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +0200882 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200883 sess->fe->monitor_uri,
884 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100885 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100886 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +0100887 */
Willy Tarreau59234e92008-11-30 23:51:27 +0100888 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +0100889
Willy Tarreaue7dff022015-04-03 01:14:29 +0200890 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100891 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +0100892
Willy Tarreau59234e92008-11-30 23:51:27 +0100893 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200894 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +0200895 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +0200896
Willy Tarreau59234e92008-11-30 23:51:27 +0100897 ret = acl_pass(ret);
898 if (cond->pol == ACL_COND_UNLESS)
899 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +0100900
Willy Tarreau59234e92008-11-30 23:51:27 +0100901 if (ret) {
902 /* we fail this request, let's return 503 service unavail */
903 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200904 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +0200905 if (!(s->flags & SF_ERR_MASK))
906 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +0100907 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +0100908 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100909 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100910
Joseph Herlant5ba80252018-11-15 09:25:36 -0800911 /* nothing to fail, let's reply normally */
Willy Tarreau59234e92008-11-30 23:51:27 +0100912 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200913 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +0200914 if (!(s->flags & SF_ERR_MASK))
915 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +0100916 goto return_prx_cond;
917 }
918
919 /*
920 * 3: Maybe we have to copy the original REQURI for the logs ?
921 * Note: we cannot log anymore if the request has been
922 * classified as invalid.
923 */
924 if (unlikely(s->logs.logwait & LW_REQ)) {
925 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100926 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100927 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100928
Stéphane Cottin23e9e932017-05-18 08:58:41 +0200929 if (urilen >= global.tune.requri_len )
930 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +0200931 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +0100932 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100933
Willy Tarreaud79a3b22012-12-28 09:40:16 +0100934 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +0100935 s->do_log(s);
936 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100937 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100938 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100939 }
Willy Tarreau06619262006-12-17 08:37:22 +0100940
Willy Tarreau91852eb2015-05-01 13:26:00 +0200941 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
942 * exactly one digit "." one digit. This check may be disabled using
943 * option accept-invalid-http-request.
944 */
945 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
946 if (msg->sl.rq.v_l != 8) {
947 msg->err_pos = msg->sl.rq.v;
948 goto return_bad_req;
949 }
950
Willy Tarreauf37954d2018-06-15 18:31:02 +0200951 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
952 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
953 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
954 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +0200955 msg->err_pos = msg->sl.rq.v + 4;
956 goto return_bad_req;
957 }
958 }
Willy Tarreau13317662015-05-01 13:47:08 +0200959 else {
960 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
961 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
962 goto return_bad_req;
963 }
Willy Tarreau91852eb2015-05-01 13:26:00 +0200964
Willy Tarreau5b154472009-12-21 20:11:07 +0100965 /* ... and check if the request is HTTP/1.1 or above */
966 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +0200967 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
968 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
969 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +0100970 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +0100971
972 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +0100973 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 +0100974
Willy Tarreau88d349d2010-01-25 12:15:43 +0100975 /* if the frontend has "option http-use-proxy-header", we'll check if
976 * we have what looks like a proxied connection instead of a connection,
977 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
978 * Note that this is *not* RFC-compliant, however browsers and proxies
979 * happen to do that despite being non-standard :-(
980 * We consider that a request not beginning with either '/' or '*' is
981 * a proxied connection, which covers both "scheme://location" and
982 * CONNECT ip:port.
983 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200984 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +0200985 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +0100986 txn->flags |= TX_USE_PX_CONN;
987
Willy Tarreaue8e785b2009-12-26 15:34:26 +0100988 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +0100989 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +0100990
Willy Tarreau59234e92008-11-30 23:51:27 +0100991 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200992 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +0200993 http_capture_headers(ci_head(req), &txn->hdr_idx,
994 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +0200995
Willy Tarreau557f1992015-05-01 10:05:17 +0200996 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
997 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +0100998 *
Willy Tarreau557f1992015-05-01 10:05:17 +0200999 * The length of a message body is determined by one of the following
1000 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001001 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001002 * 1. Any response to a HEAD request and any response with a 1xx
1003 * (Informational), 204 (No Content), or 304 (Not Modified) status
1004 * code is always terminated by the first empty line after the
1005 * header fields, regardless of the header fields present in the
1006 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001007 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001008 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1009 * the connection will become a tunnel immediately after the empty
1010 * line that concludes the header fields. A client MUST ignore any
1011 * Content-Length or Transfer-Encoding header fields received in
1012 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001013 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001014 * 3. If a Transfer-Encoding header field is present and the chunked
1015 * transfer coding (Section 4.1) is the final encoding, the message
1016 * body length is determined by reading and decoding the chunked
1017 * data until the transfer coding indicates the data is complete.
1018 *
1019 * If a Transfer-Encoding header field is present in a response and
1020 * the chunked transfer coding is not the final encoding, the
1021 * message body length is determined by reading the connection until
1022 * it is closed by the server. If a Transfer-Encoding header field
1023 * is present in a request and the chunked transfer coding is not
1024 * the final encoding, the message body length cannot be determined
1025 * reliably; the server MUST respond with the 400 (Bad Request)
1026 * status code and then close the connection.
1027 *
1028 * If a message is received with both a Transfer-Encoding and a
1029 * Content-Length header field, the Transfer-Encoding overrides the
1030 * Content-Length. Such a message might indicate an attempt to
1031 * perform request smuggling (Section 9.5) or response splitting
1032 * (Section 9.4) and ought to be handled as an error. A sender MUST
1033 * remove the received Content-Length field prior to forwarding such
1034 * a message downstream.
1035 *
1036 * 4. If a message is received without Transfer-Encoding and with
1037 * either multiple Content-Length header fields having differing
1038 * field-values or a single Content-Length header field having an
1039 * invalid value, then the message framing is invalid and the
1040 * recipient MUST treat it as an unrecoverable error. If this is a
1041 * request message, the server MUST respond with a 400 (Bad Request)
1042 * status code and then close the connection. If this is a response
1043 * message received by a proxy, the proxy MUST close the connection
1044 * to the server, discard the received response, and send a 502 (Bad
1045 * Gateway) response to the client. If this is a response message
1046 * received by a user agent, the user agent MUST close the
1047 * connection to the server and discard the received response.
1048 *
1049 * 5. If a valid Content-Length header field is present without
1050 * Transfer-Encoding, its decimal value defines the expected message
1051 * body length in octets. If the sender closes the connection or
1052 * the recipient times out before the indicated number of octets are
1053 * received, the recipient MUST consider the message to be
1054 * incomplete and close the connection.
1055 *
1056 * 6. If this is a request message and none of the above are true, then
1057 * the message body length is zero (no message body is present).
1058 *
1059 * 7. Otherwise, this is a response message without a declared message
1060 * body length, so the message body length is determined by the
1061 * number of octets received prior to the server closing the
1062 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001063 */
1064
Willy Tarreau32b47f42009-10-18 20:55:02 +02001065 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001066 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001067 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001068 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001069 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001070 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001071 /* chunked not last, return badreq */
1072 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001073 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001074 }
1075
Willy Tarreau1c913912015-04-30 10:57:51 +02001076 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001077 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001078 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001079 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001080 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1081 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001082 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001083 signed long long cl;
1084
Willy Tarreauad14f752011-09-02 20:33:27 +02001085 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001086 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001087 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001088 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001089
Willy Tarreauad14f752011-09-02 20:33:27 +02001090 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001091 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001092 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001093 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001094
Willy Tarreauad14f752011-09-02 20:33:27 +02001095 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001096 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001097 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001098 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001099
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001100 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001101 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001102 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001103 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001104
Christopher Fauletbe821b92017-03-30 11:21:53 +02001105 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001106 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001107 }
1108
Willy Tarreau34dfc602015-05-01 10:09:49 +02001109 /* even bodyless requests have a known length */
1110 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001111
Willy Tarreau179085c2014-04-28 16:48:56 +02001112 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1113 * only change if both the request and the config reference something else.
1114 * Option httpclose by itself sets tunnel mode where headers are mangled.
1115 * However, if another mode is set, it will affect it (eg: server-close/
1116 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1117 * if FE and BE have the same settings (common). The method consists in
1118 * checking if options changed between the two calls (implying that either
1119 * one is non-null, or one of them is non-null and we are there for the first
1120 * time.
1121 */
1122 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001123 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001124 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001125
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001126 /* we may have to wait for the request's body */
1127 if ((s->be->options & PR_O_WREQ_BODY) &&
1128 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1129 req->analysers |= AN_REQ_HTTP_BODY;
1130
Willy Tarreau83ece462017-12-21 15:13:09 +01001131 /*
1132 * RFC7234#4:
1133 * A cache MUST write through requests with methods
1134 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1135 * the origin server; i.e., a cache is not allowed
1136 * to generate a reply to such a request before
1137 * having forwarded the request and having received
1138 * a corresponding response.
1139 *
1140 * RFC7231#4.2.1:
1141 * Of the request methods defined by this
1142 * specification, the GET, HEAD, OPTIONS, and TRACE
1143 * methods are defined to be safe.
1144 */
1145 if (likely(txn->meth == HTTP_METH_GET ||
1146 txn->meth == HTTP_METH_HEAD ||
1147 txn->meth == HTTP_METH_OPTIONS ||
1148 txn->meth == HTTP_METH_TRACE))
1149 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1150
Willy Tarreaud787e662009-07-07 10:14:51 +02001151 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001152 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001153 req->analyse_exp = TICK_ETERNITY;
1154 return 1;
1155
1156 return_bad_req:
1157 /* We centralize bad requests processing here */
1158 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1159 /* we detected a parsing error. We want to archive this request
1160 * in the dedicated proxy area for later troubleshooting.
1161 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001162 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001163 }
1164
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001165 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001166 txn->req.msg_state = HTTP_MSG_ERROR;
1167 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001168 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001169
Olivier Houcharda798bf52019-03-08 18:52:00 +01001170 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001171 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001172 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001173
1174 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001175 if (!(s->flags & SF_ERR_MASK))
1176 s->flags |= SF_ERR_PRXCOND;
1177 if (!(s->flags & SF_FINST_MASK))
1178 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001179
Christopher Faulet0184ea72017-01-05 14:06:34 +01001180 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001181 req->analyse_exp = TICK_ETERNITY;
1182 return 0;
1183}
1184
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001185
Willy Tarreau347a35d2013-11-22 17:51:09 +01001186/* This function prepares an applet to handle the stats. It can deal with the
1187 * "100-continue" expectation, check that admin rules are met for POST requests,
1188 * and program a response message if something was unexpected. It cannot fail
1189 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001190 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001191 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001192 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001193 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001194int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001195{
1196 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001197 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001198 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001199 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001200 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001201 struct uri_auth *uri_auth = s->be->uri_auth;
1202 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001203 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001204
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001205 appctx = si_appctx(si);
1206 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1207 appctx->st1 = appctx->st2 = 0;
1208 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1209 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001210 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001211 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001212
Willy Tarreauf37954d2018-06-15 18:31:02 +02001213 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001214 lookup = uri + uri_auth->uri_len;
1215
1216 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1217 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001218 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001219 break;
1220 }
1221 }
1222
1223 if (uri_auth->refresh) {
1224 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1225 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001226 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001227 break;
1228 }
1229 }
1230 }
1231
1232 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1233 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001234 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001235 break;
1236 }
1237 }
1238
Willy Tarreau1e62df92016-01-11 18:57:53 +01001239 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1240 if (memcmp(h, ";typed", 6) == 0) {
1241 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1242 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1243 break;
1244 }
1245 }
1246
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001247 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1248 if (memcmp(h, ";st=", 4) == 0) {
1249 int i;
1250 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001251 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001252 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1253 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001254 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001255 break;
1256 }
1257 }
1258 break;
1259 }
1260 }
1261
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001262 appctx->ctx.stats.scope_str = 0;
1263 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001264 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1265 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1266 int itx = 0;
1267 const char *h2;
1268 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1269 const char *err;
1270
1271 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1272 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001273 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001274 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1275 itx++;
1276 h++;
1277 }
1278
1279 if (itx > STAT_SCOPE_TXT_MAXLEN)
1280 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001281 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001282
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001283 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001284 memcpy(scope_txt, h2, itx);
1285 scope_txt[itx] = '\0';
1286 err = invalid_char(scope_txt);
1287 if (err) {
1288 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001289 appctx->ctx.stats.scope_str = 0;
1290 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001291 }
1292 break;
1293 }
1294 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001295
1296 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001297 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001298 int ret = 1;
1299
1300 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001301 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 +01001302 ret = acl_pass(ret);
1303 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1304 ret = !ret;
1305 }
1306
1307 if (ret) {
1308 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001309 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001310 break;
1311 }
1312 }
1313
Christopher Faulet5d45e382019-02-27 15:15:23 +01001314 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1315 appctx->st0 = STAT_HTTP_HEAD;
1316 else if (txn->meth == HTTP_METH_POST && (msg->flags & HTTP_MSGF_CNT_LEN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001317 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001318 /* we'll need the request body, possibly after sending 100-continue */
Christopher Faulet5d45e382019-02-27 15:15:23 +01001319 if (msg->msg_state < HTTP_MSG_DATA)
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001320 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001321 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001322 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001323 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01001324 /* POST without admin level */
1325 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001326 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1327 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001328 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001329 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001330 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01001331 /* Unsupported method or chunked POST */
1332 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
1333 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
1334 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001335 }
1336
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001337 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001338 return 1;
1339}
1340
Willy Tarreau87b09662015-04-03 00:22:06 +02001341int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001342 const char* name, unsigned int name_len,
1343 const char *str, struct my_regex *re,
1344 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001345{
Willy Tarreaueee5b512015-04-03 23:46:31 +02001346 struct hdr_idx *idx = &s->txn->hdr_idx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001347 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001348
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001349 /* Choose the header browsing function. */
1350 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001351 case ACT_HTTP_REPLACE_VAL:
Willy Tarreauc5a4fd52018-12-11 11:42:27 +01001352 return http_legacy_replace_header(idx, msg, name, name_len, str, re, output);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001353 case ACT_HTTP_REPLACE_HDR:
Willy Tarreauc5a4fd52018-12-11 11:42:27 +01001354 return http_legacy_replace_full_header(idx, msg, name, name_len, str, re, output);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001355 default: /* impossible */
1356 return -1;
1357 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001358}
1359
Willy Tarreau87b09662015-04-03 00:22:06 +02001360static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001361 const char* name, unsigned int name_len,
1362 struct list *fmt, struct my_regex *re,
1363 int action)
1364{
Willy Tarreau83061a82018-07-13 11:56:34 +02001365 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001366 int ret = -1;
1367
1368 replace = alloc_trash_chunk();
1369 if (!replace)
1370 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001371
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001372 replace->data = build_logline(s, replace->area, replace->size, fmt);
1373 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001374 goto leave;
1375
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001376 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1377 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001378
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001379 leave:
1380 free_trash_chunk(replace);
1381 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001382}
1383
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001384/*
1385 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
1386 * built according to <fmt> log line format.
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001387 * If <early_hints> is NULL, it is allocated and the HTTP 103 response first
1388 * line is inserted before the header. If an error occurred <early_hints> is
1389 * released and NULL is returned. On success the updated buffer is returned.
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001390 */
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001391static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints,
1392 const char* name, unsigned int name_len,
1393 struct list *fmt)
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001394{
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001395 if (!early_hints) {
1396 early_hints = alloc_trash_chunk();
1397 if (!early_hints)
1398 goto fail;
1399 if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len))
1400 goto fail;
1401 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001402
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001403 if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2))
1404 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001405
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001406 early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt);
1407 if (!chunk_memcat(early_hints, "\r\n", 2))
1408 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001409
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001410 return early_hints;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001411
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001412 fail:
1413 free_trash_chunk(early_hints);
1414 return NULL;
1415}
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001416
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001417/* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the
1418 * response is added. If an error occurred or if another response was already
1419 * sent, this function does nothing.
1420 */
1421static void http_send_early_hints(struct stream *s, struct buffer *early_hints)
1422{
1423 struct channel *chn = s->txn->rsp.chn;
1424 char *cur_ptr = ci_head(chn);
1425 int ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001426
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001427 /* If a response was already sent, skip early hints */
1428 if (s->txn->status > 0)
1429 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001430
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001431 if (!chunk_memcat(early_hints, "\r\n", 2))
1432 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001433
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001434 ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints));
1435 c_adv(chn, ret);
1436 chn->total += ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001437}
1438
Willy Tarreau87b09662015-04-03 00:22:06 +02001439/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001440 * transaction <txn>. Returns the verdict of the first rule that prevents
1441 * further processing of the request (auth, deny, ...), and defaults to
1442 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1443 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001444 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1445 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1446 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001447 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001448enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001449http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001450{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001451 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001452 struct http_txn *txn = s->txn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001453 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001454 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001455 const char *auth_realm;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001456 struct buffer *early_hints = NULL;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001457 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001458 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001459 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001460
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001461 /* If "the current_rule_list" match the executed rule list, we are in
1462 * resume condition. If a resume is needed it is always in the action
1463 * and never in the ACL or converters. In this case, we initialise the
1464 * current rule, and go to the action execution point.
1465 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001466 if (s->current_rule) {
1467 rule = s->current_rule;
1468 s->current_rule = NULL;
1469 if (s->current_rule_list == rules)
1470 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001471 }
1472 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001473
Willy Tarreauff011f22011-01-06 17:51:27 +01001474 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001475
Willy Tarreau96257ec2012-12-27 10:46:37 +01001476 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001477 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001478 int ret;
1479
Willy Tarreau192252e2015-04-04 01:47:55 +02001480 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001481 ret = acl_pass(ret);
1482
Willy Tarreauff011f22011-01-06 17:51:27 +01001483 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001484 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001485
1486 if (!ret) /* condition not matched */
1487 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001488 }
1489
Willy Tarreauacc98002015-09-27 23:34:39 +02001490 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001491resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001492 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001493 case ACT_ACTION_ALLOW:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001494 rule_ret = HTTP_RULE_RES_STOP;
1495 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001496
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001497 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001498 if (deny_status)
1499 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001500 rule_ret = HTTP_RULE_RES_DENY;
1501 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001502
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001503 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001504 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001505 if (deny_status)
1506 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001507 rule_ret = HTTP_RULE_RES_DENY;
1508 goto end;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001509
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001510 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001511 /* Be sure to send any pending HTTP 103 response first */
1512 if (early_hints) {
1513 http_send_early_hints(s, early_hints);
1514 free_trash_chunk(early_hints);
1515 early_hints = NULL;
1516 }
Willy Tarreauae3c0102014-04-28 23:22:08 +02001517 /* Auth might be performed on regular http-req rules as well as on stats */
1518 auth_realm = rule->arg.auth.realm;
1519 if (!auth_realm) {
1520 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1521 auth_realm = STATS_DEFAULT_REALM;
1522 else
1523 auth_realm = px->id;
1524 }
1525 /* send 401/407 depending on whether we use a proxy or not. We still
1526 * count one error, because normal browsing won't significantly
1527 * increase the counter but brute force attempts will.
1528 */
1529 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1530 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001531 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001532 stream_inc_http_err_ctr(s);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001533 rule_ret = HTTP_RULE_RES_ABRT;
1534 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001535
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001536 case ACT_HTTP_REDIR:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001537 /* Be sure to send any pending HTTP 103 response first */
1538 if (early_hints) {
1539 http_send_early_hints(s, early_hints);
1540 free_trash_chunk(early_hints);
1541 early_hints = NULL;
1542 }
Christopher Fauletea827bd2018-11-15 15:34:11 +01001543 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau0b748332014-04-29 00:13:29 +02001544 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Fauletea827bd2018-11-15 15:34:11 +01001545 rule_ret = HTTP_RULE_RES_BADREQ;
1546 goto end;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001547
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001548 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001549 s->task->nice = rule->arg.nice;
1550 break;
1551
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001552 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001553 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001554 break;
1555
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001556 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001557 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Willy Tarreau51347ed2013-06-11 19:34:13 +02001558 break;
1559
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001560 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001561 s->logs.level = rule->arg.loglevel;
1562 break;
1563
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001564 case ACT_HTTP_REPLACE_HDR:
1565 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001566 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1567 rule->arg.hdr_add.name_len,
1568 &rule->arg.hdr_add.fmt,
Christopher Fauletea827bd2018-11-15 15:34:11 +01001569 &rule->arg.hdr_add.re, rule->action)) {
1570 rule_ret = HTTP_RULE_RES_BADREQ;
1571 goto end;
1572 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001573 break;
1574
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001575 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001576 ctx.idx = 0;
1577 /* remove all occurrences of the header */
1578 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001579 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001580 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001581 }
Willy Tarreau85603282015-01-21 20:39:27 +01001582 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001583
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001584 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001585 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001586 /* The scope of the trash buffer must be limited to this function. The
1587 * build_logline() function can execute a lot of other function which
1588 * can use the trash buffer. So for limiting the scope of this global
1589 * buffer, we build first the header value using build_logline, and
1590 * after we store the header name.
1591 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001592 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001593
1594 replace = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001595 if (!replace) {
1596 rule_ret = HTTP_RULE_RES_BADREQ;
1597 goto end;
1598 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001599
Thierry Fournier4b788f72016-06-01 13:35:36 +02001600 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001601 len += build_logline(s, replace->area + len,
1602 replace->size - len,
1603 &rule->arg.hdr_add.fmt);
1604 memcpy(replace->area, rule->arg.hdr_add.name,
1605 rule->arg.hdr_add.name_len);
1606 replace->area[rule->arg.hdr_add.name_len] = ':';
1607 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1608 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001609
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001610 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001611 /* remove all occurrences of the header */
1612 ctx.idx = 0;
1613 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001614 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001615 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1616 }
1617 }
1618
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001619 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001620 static unsigned char rate_limit = 0;
1621
1622 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001623 replace->area[rule->arg.hdr_add.name_len] = 0;
1624 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,
1625 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001626 }
1627
Olivier Houcharda798bf52019-03-08 18:52:00 +01001628 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001629 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001630 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001631 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001632 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001633 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001634
1635 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001636 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001637 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001638
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001639 case ACT_HTTP_DEL_ACL:
1640 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001641 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001642 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001643
1644 /* collect reference */
1645 ref = pat_ref_lookup(rule->arg.map.ref);
1646 if (!ref)
1647 continue;
1648
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001649 /* allocate key */
1650 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001651 if (!key) {
1652 rule_ret = HTTP_RULE_RES_BADREQ;
1653 goto end;
1654 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001655
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001656 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001657 key->data = build_logline(s, key->area, key->size,
1658 &rule->arg.map.key);
1659 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001660
1661 /* perform update */
1662 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001663 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001664 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001665 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001666
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001667 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001668 break;
1669 }
1670
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001671 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001672 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001673 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001674
1675 /* collect reference */
1676 ref = pat_ref_lookup(rule->arg.map.ref);
1677 if (!ref)
1678 continue;
1679
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001680 /* allocate key */
1681 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001682 if (!key) {
1683 rule_ret = HTTP_RULE_RES_BADREQ;
1684 goto end;
1685 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001686
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001687 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001688 key->data = build_logline(s, key->area, key->size,
1689 &rule->arg.map.key);
1690 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001691
1692 /* perform update */
1693 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001694 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001695 if (pat_ref_find_elt(ref, key->area) == NULL)
1696 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001697 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001698
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001699 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001700 break;
1701 }
1702
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001703 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001704 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001705 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001706
1707 /* collect reference */
1708 ref = pat_ref_lookup(rule->arg.map.ref);
1709 if (!ref)
1710 continue;
1711
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001712 /* allocate key */
1713 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001714 if (!key) {
1715 rule_ret = HTTP_RULE_RES_BADREQ;
1716 goto end;
1717 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001718
1719 /* allocate value */
1720 value = alloc_trash_chunk();
1721 if (!value) {
1722 free_trash_chunk(key);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001723 rule_ret = HTTP_RULE_RES_BADREQ;
1724 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001725 }
1726
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001727 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001728 key->data = build_logline(s, key->area, key->size,
1729 &rule->arg.map.key);
1730 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001731
1732 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001733 value->data = build_logline(s, value->area,
1734 value->size,
1735 &rule->arg.map.value);
1736 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001737
1738 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001739 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001740 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001741 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001742 else
1743 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001744 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001745
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001746 free_trash_chunk(key);
1747 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001748 break;
1749 }
William Lallemand73025dd2014-04-24 14:38:37 +02001750
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001751 case ACT_HTTP_EARLY_HINT:
1752 if (!(txn->req.flags & HTTP_MSGF_VER_11))
1753 break;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001754 early_hints = http_apply_early_hint_rule(s, early_hints,
1755 rule->arg.early_hint.name,
1756 rule->arg.early_hint.name_len,
1757 &rule->arg.early_hint.fmt);
1758 if (!early_hints) {
Christopher Fauletea827bd2018-11-15 15:34:11 +01001759 rule_ret = HTTP_RULE_RES_DONE;
1760 goto end;
1761 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001762 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02001763 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01001764 if ((s->req.flags & CF_READ_ERROR) ||
1765 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
1766 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
1767 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02001768 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02001769
Willy Tarreauacc98002015-09-27 23:34:39 +02001770 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02001771 case ACT_RET_ERR:
1772 case ACT_RET_CONT:
1773 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02001774 case ACT_RET_STOP:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001775 rule_ret = HTTP_RULE_RES_DONE;
1776 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02001777 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02001778 s->current_rule = rule;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001779 rule_ret = HTTP_RULE_RES_YIELD;
1780 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001781 }
William Lallemand73025dd2014-04-24 14:38:37 +02001782 break;
1783
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001784 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02001785 /* Note: only the first valid tracking parameter of each
1786 * applies.
1787 */
1788
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001789 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02001790 struct stktable *t;
1791 struct stksess *ts;
1792 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001793 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02001794
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02001795 t = rule->arg.trk_ctr.table.t;
1796 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 +02001797
1798 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001799 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02001800
1801 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001802 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1803 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1804 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001805 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02001806
Emeric Brun819fc6f2017-06-13 19:37:32 +02001807 if (ptr1)
1808 stktable_data_cast(ptr1, http_req_cnt)++;
1809
1810 if (ptr2)
1811 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
1812 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
1813
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001814 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01001815
1816 /* If data was modified, we need to touch to re-schedule sync */
1817 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001818 }
Willy Tarreau09448f72014-06-25 18:12:15 +02001819
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001820 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001821 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001822 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02001823 }
1824 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02001825 break;
1826
Joseph Herlant5ba80252018-11-15 09:25:36 -08001827 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02001828 default:
1829 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001830 }
1831 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01001832
Christopher Fauletea827bd2018-11-15 15:34:11 +01001833 end:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001834 if (early_hints) {
1835 http_send_early_hints(s, early_hints);
1836 free_trash_chunk(early_hints);
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001837 }
1838
Willy Tarreau96257ec2012-12-27 10:46:37 +01001839 /* we reached the end of the rules, nothing to report */
Christopher Fauletea827bd2018-11-15 15:34:11 +01001840 return rule_ret;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001841}
1842
Willy Tarreau71241ab2012-12-27 11:30:54 +01001843
Willy Tarreau51d861a2015-05-22 17:30:48 +02001844/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
1845 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
1846 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
1847 * is returned, the process can continue the evaluation of next rule list. If
1848 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
1849 * is returned, it means the operation could not be processed and a server error
1850 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
1851 * deny rule. If *YIELD is returned, the caller must call again the function
1852 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001853 */
Christopher Faulet10079f52018-10-03 15:17:28 +02001854enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001855http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001856{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001857 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001858 struct http_txn *txn = s->txn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001859 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001860 struct hdr_ctx ctx;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001861 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001862 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001863
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001864 /* If "the current_rule_list" match the executed rule list, we are in
1865 * resume condition. If a resume is needed it is always in the action
1866 * and never in the ACL or converters. In this case, we initialise the
1867 * current rule, and go to the action execution point.
1868 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001869 if (s->current_rule) {
1870 rule = s->current_rule;
1871 s->current_rule = NULL;
1872 if (s->current_rule_list == rules)
1873 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001874 }
1875 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001876
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001877 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001878
1879 /* check optional condition */
1880 if (rule->cond) {
1881 int ret;
1882
Willy Tarreau192252e2015-04-04 01:47:55 +02001883 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001884 ret = acl_pass(ret);
1885
1886 if (rule->cond->pol == ACL_COND_UNLESS)
1887 ret = !ret;
1888
1889 if (!ret) /* condition not matched */
1890 continue;
1891 }
1892
Willy Tarreauacc98002015-09-27 23:34:39 +02001893 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001894resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001895 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001896 case ACT_ACTION_ALLOW:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001897 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
1898 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001899
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001900 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001901 txn->flags |= TX_SVDENY;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001902 rule_ret = HTTP_RULE_RES_STOP;
1903 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001904
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001905 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001906 s->task->nice = rule->arg.nice;
1907 break;
1908
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001909 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001910 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001911 break;
1912
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001913 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001914 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Willy Tarreau51347ed2013-06-11 19:34:13 +02001915 break;
1916
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001917 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001918 s->logs.level = rule->arg.loglevel;
1919 break;
1920
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001921 case ACT_HTTP_REPLACE_HDR:
1922 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001923 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
1924 rule->arg.hdr_add.name_len,
1925 &rule->arg.hdr_add.fmt,
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001926 &rule->arg.hdr_add.re, rule->action)) {
1927 rule_ret = HTTP_RULE_RES_BADREQ;
1928 goto end;
1929 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001930 break;
1931
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001932 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001933 ctx.idx = 0;
1934 /* remove all occurrences of the header */
1935 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001936 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001937 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
1938 }
Willy Tarreau85603282015-01-21 20:39:27 +01001939 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001940
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001941 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001942 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02001943 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001944
1945 replace = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001946 if (!replace) {
1947 rule_ret = HTTP_RULE_RES_BADREQ;
1948 goto end;
1949 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001950
1951 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001952 memcpy(replace->area, rule->arg.hdr_add.name,
1953 rule->arg.hdr_add.name_len);
1954 replace->data = rule->arg.hdr_add.name_len;
1955 replace->area[replace->data++] = ':';
1956 replace->area[replace->data++] = ' ';
1957 replace->data += build_logline(s,
1958 replace->area + replace->data,
1959 replace->size - replace->data,
1960 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01001961
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001962 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001963 /* remove all occurrences of the header */
1964 ctx.idx = 0;
1965 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001966 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001967 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
1968 }
1969 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001970
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001971 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001972 static unsigned char rate_limit = 0;
1973
1974 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001975 replace->area[rule->arg.hdr_add.name_len] = 0;
1976 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,
1977 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001978 }
1979
Olivier Houcharda798bf52019-03-08 18:52:00 +01001980 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001981 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001982 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001983 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001984 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001985 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001986 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001987 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001988
1989 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001990 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001991 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001992
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001993 case ACT_HTTP_DEL_ACL:
1994 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001995 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001996 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001997
1998 /* collect reference */
1999 ref = pat_ref_lookup(rule->arg.map.ref);
2000 if (!ref)
2001 continue;
2002
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002003 /* allocate key */
2004 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002005 if (!key) {
2006 rule_ret = HTTP_RULE_RES_BADREQ;
2007 goto end;
2008 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002009
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002010 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002011 key->data = build_logline(s, key->area, key->size,
2012 &rule->arg.map.key);
2013 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002014
2015 /* perform update */
2016 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002017 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002018 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002019 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002020
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002021 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002022 break;
2023 }
2024
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002025 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002026 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002027 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002028
2029 /* collect reference */
2030 ref = pat_ref_lookup(rule->arg.map.ref);
2031 if (!ref)
2032 continue;
2033
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002034 /* allocate key */
2035 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002036 if (!key) {
2037 rule_ret = HTTP_RULE_RES_BADREQ;
2038 goto end;
2039 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002040
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002041 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002042 key->data = build_logline(s, key->area, key->size,
2043 &rule->arg.map.key);
2044 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002045
2046 /* perform update */
2047 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002048 if (pat_ref_find_elt(ref, key->area) == NULL)
2049 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002050
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002051 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002052 break;
2053 }
2054
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002055 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002056 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002057 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002058
2059 /* collect reference */
2060 ref = pat_ref_lookup(rule->arg.map.ref);
2061 if (!ref)
2062 continue;
2063
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002064 /* allocate key */
2065 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002066 if (!key) {
2067 rule_ret = HTTP_RULE_RES_BADREQ;
2068 goto end;
2069 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002070
2071 /* allocate value */
2072 value = alloc_trash_chunk();
2073 if (!value) {
2074 free_trash_chunk(key);
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002075 rule_ret = HTTP_RULE_RES_BADREQ;
2076 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002077 }
2078
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002079 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002080 key->data = build_logline(s, key->area, key->size,
2081 &rule->arg.map.key);
2082 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002083
2084 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002085 value->data = build_logline(s, value->area,
2086 value->size,
2087 &rule->arg.map.value);
2088 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002089
2090 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002091 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002092 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002093 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002094 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002095 else
2096 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002097 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002098 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002099 free_trash_chunk(key);
2100 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002101 break;
2102 }
William Lallemand73025dd2014-04-24 14:38:37 +02002103
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002104 case ACT_HTTP_REDIR:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002105 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002106 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002107 rule_ret = HTTP_RULE_RES_BADREQ;
2108 goto end;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002109
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002110 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2111 /* Note: only the first valid tracking parameter of each
2112 * applies.
2113 */
2114
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002115 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002116 struct stktable *t;
2117 struct stksess *ts;
2118 struct stktable_key *key;
2119 void *ptr;
2120
2121 t = rule->arg.trk_ctr.table.t;
2122 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2123
2124 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002125 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002126
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002127 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002128
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002129 /* let's count a new HTTP request as it's the first time we do it */
2130 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2131 if (ptr)
2132 stktable_data_cast(ptr, http_req_cnt)++;
2133
2134 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2135 if (ptr)
2136 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2137 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2138
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002139 /* When the client triggers a 4xx from the server, it's most often due
2140 * to a missing object or permission. These events should be tracked
2141 * because if they happen often, it may indicate a brute force or a
2142 * vulnerability scan. Normally this is done when receiving the response
2143 * but here we're tracking after this ought to have been done so we have
2144 * to do it on purpose.
2145 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002146 if ((unsigned)(txn->status - 400) < 100) {
2147 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2148 if (ptr)
2149 stktable_data_cast(ptr, http_err_cnt)++;
2150
2151 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2152 if (ptr)
2153 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2154 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2155 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002156
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002157 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002158
Emeric Brun0fed0b02017-11-29 16:15:07 +01002159 /* If data was modified, we need to touch to re-schedule sync */
2160 stktable_touch_local(t, ts, 0);
2161
Emeric Brun819fc6f2017-06-13 19:37:32 +02002162 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2163 if (sess->fe != s->be)
2164 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2165
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002166 }
2167 }
2168 break;
2169
Thierry FOURNIER42148732015-09-02 17:17:33 +02002170 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002171 if ((s->req.flags & CF_READ_ERROR) ||
2172 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2173 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2174 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002175 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002176
Willy Tarreauacc98002015-09-27 23:34:39 +02002177 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002178 case ACT_RET_ERR:
2179 case ACT_RET_CONT:
2180 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002181 case ACT_RET_STOP:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002182 rule_ret = HTTP_RULE_RES_STOP;
2183 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002184 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002185 s->current_rule = rule;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002186 rule_ret = HTTP_RULE_RES_YIELD;
2187 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002188 }
William Lallemand73025dd2014-04-24 14:38:37 +02002189 break;
2190
Joseph Herlant5ba80252018-11-15 09:25:36 -08002191 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002192 default:
2193 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002194 }
2195 }
2196
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002197 end:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002198 /* we reached the end of the rules, nothing to report */
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002199 return rule_ret;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002200}
2201
2202
Willy Tarreau71241ab2012-12-27 11:30:54 +01002203/* Perform an HTTP redirect based on the information in <rule>. The function
2204 * returns non-zero on success, or zero in case of a, irrecoverable error such
2205 * as too large a request to build a valid response.
2206 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002207int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002208{
Willy Tarreaub329a312015-05-22 16:27:37 +02002209 struct http_msg *req = &txn->req;
2210 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002211 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002212 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002213 int ret = 0;
2214
Christopher Fauletf2824e62018-10-01 12:12:37 +02002215 if (IS_HTX_STRM(s))
2216 return htx_apply_redirect_rule(rule, s, txn);
2217
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002218 chunk = alloc_trash_chunk();
2219 if (!chunk)
2220 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002221
2222 /* build redirect message */
2223 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002224 case 308:
2225 msg_fmt = HTTP_308;
2226 break;
2227 case 307:
2228 msg_fmt = HTTP_307;
2229 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002230 case 303:
2231 msg_fmt = HTTP_303;
2232 break;
2233 case 301:
2234 msg_fmt = HTTP_301;
2235 break;
2236 case 302:
2237 default:
2238 msg_fmt = HTTP_302;
2239 break;
2240 }
2241
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002242 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2243 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002244
2245 switch(rule->type) {
2246 case REDIRECT_TYPE_SCHEME: {
2247 const char *path;
2248 const char *host;
2249 struct hdr_ctx ctx;
2250 int pathlen;
2251 int hostlen;
2252
2253 host = "";
2254 hostlen = 0;
2255 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002256 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002257 host = ctx.line + ctx.val;
2258 hostlen = ctx.vlen;
2259 }
2260
Willy Tarreau6b952c82018-09-10 17:45:34 +02002261 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002262 /* build message using path */
2263 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002264 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002265 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2266 int qs = 0;
2267 while (qs < pathlen) {
2268 if (path[qs] == '?') {
2269 pathlen = qs;
2270 break;
2271 }
2272 qs++;
2273 }
2274 }
2275 } else {
2276 path = "/";
2277 pathlen = 1;
2278 }
2279
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002280 if (rule->rdr_str) { /* this is an old "redirect" rule */
2281 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002282 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002283 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002284
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002285 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002286 memcpy(chunk->area + chunk->data, rule->rdr_str,
2287 rule->rdr_len);
2288 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002289 }
2290 else {
2291 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002292 chunk->data += build_logline(s,
2293 chunk->area + chunk->data,
2294 chunk->size - chunk->data,
2295 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002296
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002297 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002298 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002299 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002300 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002301 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002302 memcpy(chunk->area + chunk->data, "://", 3);
2303 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002304
2305 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002306 memcpy(chunk->area + chunk->data, host, hostlen);
2307 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002308
2309 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002310 memcpy(chunk->area + chunk->data, path, pathlen);
2311 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002312
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002313 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002314 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002315 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002316 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002317 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002318 chunk->area[chunk->data] = '/';
2319 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002320 }
2321
2322 break;
2323 }
2324 case REDIRECT_TYPE_PREFIX: {
2325 const char *path;
2326 int pathlen;
2327
Willy Tarreau6b952c82018-09-10 17:45:34 +02002328 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002329 /* build message using path */
2330 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002331 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002332 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2333 int qs = 0;
2334 while (qs < pathlen) {
2335 if (path[qs] == '?') {
2336 pathlen = qs;
2337 break;
2338 }
2339 qs++;
2340 }
2341 }
2342 } else {
2343 path = "/";
2344 pathlen = 1;
2345 }
2346
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002347 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002348 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002349 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002350
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002351 /* add prefix. Note that if prefix == "/", we don't want to
2352 * add anything, otherwise it makes it hard for the user to
2353 * configure a self-redirection.
2354 */
2355 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002356 memcpy(chunk->area + chunk->data,
2357 rule->rdr_str, rule->rdr_len);
2358 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002359 }
2360 }
2361 else {
2362 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002363 chunk->data += build_logline(s,
2364 chunk->area + chunk->data,
2365 chunk->size - chunk->data,
2366 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002367
2368 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002369 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002370 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002371 }
2372
2373 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002374 memcpy(chunk->area + chunk->data, path, pathlen);
2375 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002376
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002377 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002378 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002379 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002380 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002381 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002382 chunk->area[chunk->data] = '/';
2383 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002384 }
2385
2386 break;
2387 }
2388 case REDIRECT_TYPE_LOCATION:
2389 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002390 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002391 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002392 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002393
2394 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002395 memcpy(chunk->area + chunk->data, rule->rdr_str,
2396 rule->rdr_len);
2397 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002398 }
2399 else {
2400 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002401 chunk->data += build_logline(s,
2402 chunk->area + chunk->data,
2403 chunk->size - chunk->data,
2404 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002405
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002406 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002407 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002408 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002409 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002410 break;
2411 }
2412
2413 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002414 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2415 chunk->data += 14;
2416 memcpy(chunk->area + chunk->data, rule->cookie_str,
2417 rule->cookie_len);
2418 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002419 }
2420
Willy Tarreau19b14122017-02-28 09:48:11 +01002421 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002422 txn->status = rule->code;
2423 /* let's log the request time */
2424 s->logs.tv_request = now;
2425
Christopher Fauletbe821b92017-03-30 11:21:53 +02002426 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002427 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2428 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2429 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002430 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002431 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002432 memcpy(chunk->area + chunk->data,
2433 "\r\nProxy-Connection: keep-alive", 30);
2434 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002435 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002436 memcpy(chunk->area + chunk->data,
2437 "\r\nConnection: keep-alive", 24);
2438 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002439 }
2440 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002441 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2442 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002443 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002444 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002445 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002446 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002447 req->next -= req->sov;
2448 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002449 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002450 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002451 req->msg_state = HTTP_MSG_CLOSED;
2452 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002453 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002454 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002455 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002456 /* let the server side turn to SI_ST_CLO */
2457 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002458 } else {
2459 /* keep-alive not possible */
2460 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002461 memcpy(chunk->area + chunk->data,
2462 "\r\nProxy-Connection: close\r\n\r\n", 29);
2463 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002464 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002465 memcpy(chunk->area + chunk->data,
2466 "\r\nConnection: close\r\n\r\n", 23);
2467 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002468 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002469 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002470 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002471 }
2472
Willy Tarreaue7dff022015-04-03 01:14:29 +02002473 if (!(s->flags & SF_ERR_MASK))
2474 s->flags |= SF_ERR_LOCAL;
2475 if (!(s->flags & SF_FINST_MASK))
2476 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002477
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002478 ret = 1;
2479 leave:
2480 free_trash_chunk(chunk);
2481 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002482}
2483
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002484/* This stream analyser runs all HTTP request processing which is common to
2485 * frontends and backends, which means blocking ACLs, filters, connection-close,
2486 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002487 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002488 * either needs more data or wants to immediately abort the request (eg: deny,
2489 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002490 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002491int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002492{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002493 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002494 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002495 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002496 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002497 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002498 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002499 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002500 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002501
Christopher Faulete0768eb2018-10-03 16:38:02 +02002502 if (IS_HTX_STRM(s))
2503 return htx_process_req_common(s, req, an_bit, px);
2504
Willy Tarreau655dce92009-11-08 13:10:58 +01002505 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002506 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002507 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002508 }
2509
Christopher Faulet45073512018-07-20 10:16:29 +02002510 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 +02002511 now_ms, __FUNCTION__,
2512 s,
2513 req,
2514 req->rex, req->wex,
2515 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002516 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002517 req->analysers);
2518
Willy Tarreau65410832014-04-28 21:25:43 +02002519 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002520 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002521
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002522 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002523 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002524 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002525
Willy Tarreau0b748332014-04-29 00:13:29 +02002526 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002527 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2528 goto return_prx_yield;
2529
Willy Tarreau0b748332014-04-29 00:13:29 +02002530 case HTTP_RULE_RES_CONT:
2531 case HTTP_RULE_RES_STOP: /* nothing to do */
2532 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002533
Willy Tarreau0b748332014-04-29 00:13:29 +02002534 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2535 if (txn->flags & TX_CLTARPIT)
2536 goto tarpit;
2537 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002538
Willy Tarreau0b748332014-04-29 00:13:29 +02002539 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2540 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002541
Willy Tarreau0b748332014-04-29 00:13:29 +02002542 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002543 goto done;
2544
Willy Tarreau0b748332014-04-29 00:13:29 +02002545 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2546 goto return_bad_req;
2547 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002548 }
2549
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002550 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2551 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002552 struct hdr_ctx ctx;
2553
2554 ctx.idx = 0;
2555 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002556 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002557 if (unlikely(http_header_add_tail2(&txn->req,
2558 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002559 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002560 goto return_bad_req;
2561 }
2562 }
2563
2564 }
2565
Willy Tarreau52542592014-04-28 18:33:26 +02002566 /* OK at this stage, we know that the request was accepted according to
2567 * the http-request rules, we can check for the stats. Note that the
2568 * URI is detected *before* the req* rules in order not to be affected
2569 * by a possible reqrep, while they are processed *after* so that a
2570 * reqdeny can still block them. This clearly needs to change in 1.6!
2571 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002572 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002573 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01002574 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002575 txn->status = 500;
2576 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002577 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002578
Willy Tarreaue7dff022015-04-03 01:14:29 +02002579 if (!(s->flags & SF_ERR_MASK))
2580 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002581 goto return_prx_cond;
2582 }
2583
2584 /* parse the whole stats request and extract the relevant information */
2585 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002586 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002587 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002588
Willy Tarreau0b748332014-04-29 00:13:29 +02002589 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2590 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002591
Willy Tarreau0b748332014-04-29 00:13:29 +02002592 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2593 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002594 }
2595
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002596 /* evaluate the req* rules except reqadd */
2597 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002598 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002599 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002600
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002601 if (txn->flags & TX_CLDENY)
2602 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002603
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002604 if (txn->flags & TX_CLTARPIT) {
2605 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002606 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002607 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002608 }
Willy Tarreau06619262006-12-17 08:37:22 +01002609
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002610 /* add request headers from the rule sets in the same order */
2611 list_for_each_entry(wl, &px->req_add, list) {
2612 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002613 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002614 ret = acl_pass(ret);
2615 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2616 ret = !ret;
2617 if (!ret)
2618 continue;
2619 }
2620
Christopher Faulet10079f52018-10-03 15:17:28 +02002621 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002622 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002623 }
2624
Willy Tarreau52542592014-04-28 18:33:26 +02002625
2626 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002627 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2628 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002629 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002630 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +01002631 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002632
Willy Tarreaue7dff022015-04-03 01:14:29 +02002633 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2634 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2635 if (!(s->flags & SF_FINST_MASK))
2636 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002637
Willy Tarreau70730dd2014-04-24 18:06:27 +02002638 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002639 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2640 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002641 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002642 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002643 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002644
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002645 /* check whether we have some ACLs set to redirect this request */
2646 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002647 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002648 int ret;
2649
Willy Tarreau192252e2015-04-04 01:47:55 +02002650 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002651 ret = acl_pass(ret);
2652 if (rule->cond->pol == ACL_COND_UNLESS)
2653 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002654 if (!ret)
2655 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01002656 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002657 if (!http_apply_redirect_rule(rule, s, txn))
2658 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002659 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002660 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002661
Willy Tarreau2be39392010-01-03 17:24:51 +01002662 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2663 * If this happens, then the data will not come immediately, so we must
2664 * send all what we have without waiting. Note that due to the small gain
2665 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002666 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01002667 * itself once used.
2668 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002669 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01002670
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002671 done: /* done with this analyser, continue with next ones that the calling
2672 * points will have set, if any.
2673 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002674 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002675 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
2676 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002677 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002678
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002679 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02002680 /* Allow cookie logging
2681 */
2682 if (s->be->cookie_name || sess->fe->capture_name)
2683 manage_client_side_cookies(s, req);
2684
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002685 /* When a connection is tarpitted, we use the tarpit timeout,
2686 * which may be the same as the connect timeout if unspecified.
2687 * If unset, then set it to zero because we really want it to
2688 * eventually expire. We build the tarpit as an analyser.
2689 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002690 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002691
2692 /* wipe the request out so that we can drop the connection early
2693 * if the client closes first.
2694 */
2695 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002696
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002697 txn->status = http_err_codes[deny_status];
2698
Christopher Faulet0184ea72017-01-05 14:06:34 +01002699 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002700 req->analysers |= AN_REQ_HTTP_TARPIT;
2701 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2702 if (!req->analyse_exp)
2703 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02002704 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +01002705 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002706 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002707 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002708 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002709 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002710 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002711
2712 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002713
2714 /* Allow cookie logging
2715 */
2716 if (s->be->cookie_name || sess->fe->capture_name)
2717 manage_client_side_cookies(s, req);
2718
Willy Tarreau0b748332014-04-29 00:13:29 +02002719 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002720 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002721 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002722 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02002723 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +01002724 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002725 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002726 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002727 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002728 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002729 goto return_prx_cond;
2730
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002731 return_bad_req:
2732 /* We centralize bad requests processing here */
2733 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2734 /* we detected a parsing error. We want to archive this request
2735 * in the dedicated proxy area for later troubleshooting.
2736 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02002737 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002738 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002739
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002740 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002741 txn->req.msg_state = HTTP_MSG_ERROR;
2742 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002743 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002744
Olivier Houcharda798bf52019-03-08 18:52:00 +01002745 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002746 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002747 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002748
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002749 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002750 if (!(s->flags & SF_ERR_MASK))
2751 s->flags |= SF_ERR_PRXCOND;
2752 if (!(s->flags & SF_FINST_MASK))
2753 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002754
Christopher Faulet0184ea72017-01-05 14:06:34 +01002755 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002756 req->analyse_exp = TICK_ETERNITY;
2757 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002758
2759 return_prx_yield:
2760 channel_dont_connect(req);
2761 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002762}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002763
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002764/* This function performs all the processing enabled for the current request.
2765 * It returns 1 if the processing can continue on next analysers, or zero if it
2766 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002767 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002768 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002769int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002770{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002771 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002772 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002773 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02002774 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002775
Christopher Faulete0768eb2018-10-03 16:38:02 +02002776 if (IS_HTX_STRM(s))
2777 return htx_process_request(s, req, an_bit);
2778
Willy Tarreau655dce92009-11-08 13:10:58 +01002779 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002780 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002781 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002782 return 0;
2783 }
2784
Christopher Faulet45073512018-07-20 10:16:29 +02002785 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 +02002786 now_ms, __FUNCTION__,
2787 s,
2788 req,
2789 req->rex, req->wex,
2790 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002791 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002792 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002793
Willy Tarreau59234e92008-11-30 23:51:27 +01002794 /*
2795 * Right now, we know that we have processed the entire headers
2796 * and that unwanted requests have been filtered out. We can do
2797 * whatever we want with the remaining request. Also, now we
2798 * may have separate values for ->fe, ->be.
2799 */
Willy Tarreau06619262006-12-17 08:37:22 +01002800
Willy Tarreau59234e92008-11-30 23:51:27 +01002801 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002802 * If HTTP PROXY is set we simply get remote server address parsing
2803 * incoming request. Note that this requires that a connection is
2804 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01002805 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002806 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002807 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002808 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002809
Willy Tarreau9471b8c2013-12-15 13:31:35 +01002810 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002811 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002812 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002813 txn->req.msg_state = HTTP_MSG_ERROR;
2814 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002815 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002816 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002817
Willy Tarreaue7dff022015-04-03 01:14:29 +02002818 if (!(s->flags & SF_ERR_MASK))
2819 s->flags |= SF_ERR_RESOURCE;
2820 if (!(s->flags & SF_FINST_MASK))
2821 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002822
2823 return 0;
2824 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002825
Willy Tarreau6b952c82018-09-10 17:45:34 +02002826 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02002827 if (url2sa(ci_head(req) + msg->sl.rq.u,
2828 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02002829 &conn->addr.to, NULL) == -1)
2830 goto return_bad_req;
2831
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002832 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02002833 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
2834 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002835 * u_l characters by a single "/".
2836 */
2837 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002838 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002839 char *cur_end = cur_ptr + txn->req.sl.rq.l;
2840 int delta;
2841
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002842 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002843 http_msg_move_end(&txn->req, delta);
2844 cur_end += delta;
2845 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
2846 goto return_bad_req;
2847 }
2848 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002849 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002850 char *cur_end = cur_ptr + txn->req.sl.rq.l;
2851 int delta;
2852
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002853 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002854 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002855 http_msg_move_end(&txn->req, delta);
2856 cur_end += delta;
2857 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
2858 goto return_bad_req;
2859 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002860 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002861
Willy Tarreau59234e92008-11-30 23:51:27 +01002862 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002863 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002864 * Note that doing so might move headers in the request, but
2865 * the fields will stay coherent and the URI will not move.
2866 * This should only be performed in the backend.
2867 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002868 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002869 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002870
William Lallemanda73203e2012-03-12 12:48:57 +01002871 /* add unique-id if "header-unique-id" is specified */
2872
Thierry Fournierf4011dd2016-03-29 17:23:51 +02002873 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01002874 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002875 goto return_bad_req;
2876 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002877 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002878 }
William Lallemanda73203e2012-03-12 12:48:57 +01002879
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002880 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02002881 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01002882 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002883 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01002884 goto return_bad_req;
2885 }
2886
Cyril Bontéb21570a2009-11-29 20:04:48 +01002887 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002888 * 9: add X-Forwarded-For if either the frontend or the backend
2889 * asks for it.
2890 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002891 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02002892 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002893 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
2894 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
2895 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002896 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02002897 /* The header is set to be added only if none is present
2898 * and we found it, so don't do anything.
2899 */
2900 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002901 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002902 /* Add an X-Forwarded-For header unless the source IP is
2903 * in the 'except' network range.
2904 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002905 if ((!sess->fe->except_mask.s_addr ||
2906 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
2907 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01002908 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002909 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01002910 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002911 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002912 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002913 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002914
2915 /* Note: we rely on the backend to get the header name to be used for
2916 * x-forwarded-for, because the header is really meant for the backends.
2917 * However, if the backend did not specify any option, we have to rely
2918 * on the frontend's header name.
2919 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002920 if (s->be->fwdfor_hdr_len) {
2921 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002922 memcpy(trash.area,
2923 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002924 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002925 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002926 memcpy(trash.area,
2927 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002928 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002929 len += snprintf(trash.area + len,
2930 trash.size - len,
2931 ": %d.%d.%d.%d", pn[0], pn[1],
2932 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002933
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002934 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01002935 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002936 }
2937 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002938 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002939 /* FIXME: for the sake of completeness, we should also support
2940 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002941 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002942 int len;
2943 char pn[INET6_ADDRSTRLEN];
2944 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002945 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01002946 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002947
Willy Tarreau59234e92008-11-30 23:51:27 +01002948 /* Note: we rely on the backend to get the header name to be used for
2949 * x-forwarded-for, because the header is really meant for the backends.
2950 * However, if the backend did not specify any option, we have to rely
2951 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002952 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002953 if (s->be->fwdfor_hdr_len) {
2954 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002955 memcpy(trash.area, s->be->fwdfor_hdr_name,
2956 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01002957 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002958 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002959 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
2960 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002961 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002962 len += snprintf(trash.area + len, trash.size - len,
2963 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002964
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002965 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01002966 goto return_bad_req;
2967 }
2968 }
2969
2970 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02002971 * 10: add X-Original-To if either the frontend or the backend
2972 * asks for it.
2973 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002974 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02002975
2976 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002977 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02002978 /* Add an X-Original-To header unless the destination IP is
2979 * in the 'except' network range.
2980 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002981 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02002982
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002983 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002984 ((!sess->fe->except_mask_to.s_addr ||
2985 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
2986 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02002987 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002988 (((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 +02002989 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02002990 int len;
2991 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002992 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02002993
2994 /* Note: we rely on the backend to get the header name to be used for
2995 * x-original-to, because the header is really meant for the backends.
2996 * However, if the backend did not specify any option, we have to rely
2997 * on the frontend's header name.
2998 */
2999 if (s->be->orgto_hdr_len) {
3000 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003001 memcpy(trash.area,
3002 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003003 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003004 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003005 memcpy(trash.area,
3006 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003007 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003008 len += snprintf(trash.area + len,
3009 trash.size - len,
3010 ": %d.%d.%d.%d", pn[0], pn[1],
3011 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003012
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003013 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003014 goto return_bad_req;
3015 }
3016 }
3017 }
3018
Willy Tarreau50fc7772012-11-11 22:19:57 +01003019 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3020 * If an "Upgrade" token is found, the header is left untouched in order not to have
3021 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3022 * "Upgrade" is present in the Connection header.
3023 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003024 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003025 unsigned int want_flags = 0;
3026
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003027 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003028 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003029 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003030 want_flags |= TX_CON_CLO_SET;
3031 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003032 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003033 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003034 want_flags |= TX_CON_KAL_SET;
3035 }
3036
3037 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003038 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003039 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003040
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003041
Willy Tarreau522d6c02009-12-06 18:49:18 +01003042 /* If we have no server assigned yet and we're balancing on url_param
3043 * with a POST request, we may be interested in checking the body for
3044 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003045 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003046 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +01003047 s->txn->meth == HTTP_METH_POST &&
3048 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003049 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003050 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003051 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003052 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003053
Christopher Fauletbe821b92017-03-30 11:21:53 +02003054 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3055 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +01003056
Christopher Fauletbe821b92017-03-30 11:21:53 +02003057 /* We expect some data from the client. Unless we know for sure
3058 * we already have a full request, we have to re-enable quick-ack
3059 * in case we previously disabled it, otherwise we might cause
3060 * the client to delay further data.
3061 */
3062 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletbe821b92017-03-30 11:21:53 +02003063 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003064 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau1a18b542018-12-11 16:37:42 +01003065 conn_set_quickack(cli_conn, 1);
Willy Tarreau03945942009-12-22 16:50:27 +01003066
Willy Tarreau59234e92008-11-30 23:51:27 +01003067 /*************************************************************
3068 * OK, that's finished for the headers. We have done what we *
3069 * could. Let's switch to the DATA state. *
3070 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003071 req->analyse_exp = TICK_ETERNITY;
3072 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003073
Willy Tarreau59234e92008-11-30 23:51:27 +01003074 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003075 /* OK let's go on with the BODY now */
3076 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003077
Willy Tarreau59234e92008-11-30 23:51:27 +01003078 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003079 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003080 /* we detected a parsing error. We want to archive this request
3081 * in the dedicated proxy area for later troubleshooting.
3082 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003083 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003084 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003085
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003086 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003087 txn->req.msg_state = HTTP_MSG_ERROR;
3088 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003089 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003090 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003091
Olivier Houcharda798bf52019-03-08 18:52:00 +01003092 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003093 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003094 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003095
Willy Tarreaue7dff022015-04-03 01:14:29 +02003096 if (!(s->flags & SF_ERR_MASK))
3097 s->flags |= SF_ERR_PRXCOND;
3098 if (!(s->flags & SF_FINST_MASK))
3099 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003100 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003101}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003102
Willy Tarreau60b85b02008-11-30 23:28:40 +01003103/* This function is an analyser which processes the HTTP tarpit. It always
3104 * returns zero, at the beginning because it prevents any other processing
3105 * from occurring, and at the end because it terminates the request.
3106 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003107int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003108{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003109 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003110
Christopher Faulete0768eb2018-10-03 16:38:02 +02003111 if (IS_HTX_STRM(s))
3112 return htx_process_tarpit(s, req, an_bit);
3113
Willy Tarreau60b85b02008-11-30 23:28:40 +01003114 /* This connection is being tarpitted. The CLIENT side has
3115 * already set the connect expiration date to the right
3116 * timeout. We just have to check that the client is still
3117 * there and that the timeout has not expired.
3118 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003119 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003120 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003121 !tick_is_expired(req->analyse_exp, now_ms))
3122 return 0;
3123
3124 /* We will set the queue timer to the time spent, just for
3125 * logging purposes. We fake a 500 server error, so that the
3126 * attacker will not suspect his connection has been tarpitted.
3127 * It will not cause trouble to the logs because we can exclude
3128 * the tarpitted connections by filtering on the 'PT' status flags.
3129 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003130 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3131
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003132 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003133 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003134
Christopher Faulet0184ea72017-01-05 14:06:34 +01003135 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003136 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003137
Willy Tarreaue7dff022015-04-03 01:14:29 +02003138 if (!(s->flags & SF_ERR_MASK))
3139 s->flags |= SF_ERR_PRXCOND;
3140 if (!(s->flags & SF_FINST_MASK))
3141 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003142 return 0;
3143}
3144
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003145/* This function is an analyser which waits for the HTTP request body. It waits
3146 * for either the buffer to be full, or the full advertised contents to have
3147 * reached the buffer. It must only be called after the standard HTTP request
3148 * processing has occurred, because it expects the request to be parsed and will
3149 * look for the Expect header. It may send a 100-Continue interim response. It
3150 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3151 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3152 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003153 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003154int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003155{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003156 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003157 struct http_txn *txn = s->txn;
3158 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003159
Christopher Faulete0768eb2018-10-03 16:38:02 +02003160 if (IS_HTX_STRM(s))
3161 return htx_wait_for_request_body(s, req, an_bit);
3162
Willy Tarreaud34af782008-11-30 23:36:37 +01003163 /* We have to parse the HTTP request body to find any required data.
3164 * "balance url_param check_post" should have been the only way to get
3165 * into this. We were brought here after HTTP header analysis, so all
3166 * related structures are ready.
3167 */
3168
Willy Tarreau890988f2014-04-10 11:59:33 +02003169 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3170 /* This is the first call */
3171 if (msg->msg_state < HTTP_MSG_BODY)
3172 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003173
Willy Tarreau890988f2014-04-10 11:59:33 +02003174 if (msg->msg_state < HTTP_MSG_100_SENT) {
3175 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3176 * send an HTTP/1.1 100 Continue intermediate response.
3177 */
3178 if (msg->flags & HTTP_MSGF_VER_11) {
3179 struct hdr_ctx ctx;
3180 ctx.idx = 0;
3181 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003182 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003183 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003184 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003185 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003186 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003187 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003188 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003189 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003190
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003191 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003192 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003193 * must save the body in msg->next because it survives buffer
3194 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003195 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003196 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003197
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003198 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003199 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3200 else
3201 msg->msg_state = HTTP_MSG_DATA;
3202 }
3203
Willy Tarreau890988f2014-04-10 11:59:33 +02003204 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3205 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003206 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003207 goto missing_data;
3208
3209 /* OK we have everything we need now */
3210 goto http_end;
3211 }
3212
3213 /* OK here we're parsing a chunked-encoded message */
3214
Willy Tarreau522d6c02009-12-06 18:49:18 +01003215 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003216 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003217 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003218 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003219 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003220 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003221 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003222
Willy Tarreau115acb92009-12-26 13:56:06 +01003223 if (!ret)
3224 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003225 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003226 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003227 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003228 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003229 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003230 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003231 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003232
3233 msg->chunk_len = chunk;
3234 msg->body_len += chunk;
3235
3236 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003237 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003238 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003239 }
3240
Willy Tarreaud98cf932009-12-27 22:54:55 +01003241 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003242 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3243 * for at least a whole chunk or the whole content length bytes after
3244 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003245 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003246 if (msg->msg_state == HTTP_MSG_TRAILERS)
3247 goto http_end;
3248
Willy Tarreaue115b492015-05-01 23:05:14 +02003249 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003250 goto http_end;
3251
3252 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003253 /* we get here if we need to wait for more data. If the buffer is full,
3254 * we have the maximum we can expect.
3255 */
Willy Tarreau23752332018-06-15 14:54:53 +02003256 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003257 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003258
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003259 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003260 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003261 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003262
Willy Tarreaue7dff022015-04-03 01:14:29 +02003263 if (!(s->flags & SF_ERR_MASK))
3264 s->flags |= SF_ERR_CLITO;
3265 if (!(s->flags & SF_FINST_MASK))
3266 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003267 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003268 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003269
3270 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003271 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003272 /* Not enough data. We'll re-use the http-request
3273 * timeout here. Ideally, we should set the timeout
3274 * relative to the accept() date. We just set the
3275 * request timeout once at the beginning of the
3276 * request.
3277 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003278 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003279 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003280 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003281 return 0;
3282 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003283
3284 http_end:
3285 /* The situation will not evolve, so let's give up on the analysis. */
3286 s->logs.tv_request = now; /* update the request timer to reflect full request */
3287 req->analysers &= ~an_bit;
3288 req->analyse_exp = TICK_ETERNITY;
3289 return 1;
3290
3291 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003292 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003293 txn->req.msg_state = HTTP_MSG_ERROR;
3294 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003295 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003296
Willy Tarreaue7dff022015-04-03 01:14:29 +02003297 if (!(s->flags & SF_ERR_MASK))
3298 s->flags |= SF_ERR_PRXCOND;
3299 if (!(s->flags & SF_FINST_MASK))
3300 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003301
Willy Tarreau522d6c02009-12-06 18:49:18 +01003302 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003303 req->analysers &= AN_REQ_FLT_END;
Olivier Houcharda798bf52019-03-08 18:52:00 +01003304 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003305 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003306 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003307 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003308}
3309
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003310/* send a server's name with an outgoing request over an established connection.
3311 * Note: this function is designed to be called once the request has been scheduled
3312 * for being forwarded. This is the reason why it rewinds the buffer before
3313 * proceeding.
3314 */
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003315int http_send_name_header(struct stream *s, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003316
3317 struct hdr_ctx ctx;
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003318 struct http_txn *txn = s->txn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003319 char *hdr_name = be->server_id_hdr_name;
3320 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003321 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003322 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003323 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003324
Christopher Faulet64159df2018-10-24 21:15:35 +02003325 if (IS_HTX_STRM(s))
3326 return htx_send_name_header(s, be, srv_name);
William Lallemandd9e90662012-01-30 17:27:17 +01003327 ctx.idx = 0;
3328
Willy Tarreau211cdec2014-04-17 20:18:08 +02003329 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003330 if (old_o) {
3331 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003332 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003333 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003334 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003335 }
3336
Willy Tarreauf37954d2018-06-15 18:31:02 +02003337 old_i = ci_data(chn);
3338 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 -05003339 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003340 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003341 }
3342
3343 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003344 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003345 memcpy(hdr_val, hdr_name, hdr_name_len);
3346 hdr_val += hdr_name_len;
3347 *hdr_val++ = ':';
3348 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003349 hdr_val += strlcpy2(hdr_val, srv_name,
3350 trash.area + trash.size - hdr_val);
3351 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3352 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003353
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003354 if (old_o) {
3355 /* If this was a forwarded request, we must readjust the amount of
3356 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003357 * variations. Note that the current state is >= HTTP_MSG_BODY,
3358 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003359 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003360 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003361 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003362 txn->req.next -= old_o;
3363 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003364 }
3365
Mark Lamourinec2247f02012-01-04 13:02:01 -05003366 return 0;
3367}
3368
Willy Tarreau610ecce2010-01-04 21:15:02 +01003369/* Terminate current transaction and prepare a new one. This is very tricky
3370 * right now but it works.
3371 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003372void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003373{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003374 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003375 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003376 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003377 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003378 struct connection *srv_conn;
3379 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003380 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003381
Willy Tarreau610ecce2010-01-04 21:15:02 +01003382 /* FIXME: We need a more portable way of releasing a backend's and a
3383 * server's connections. We need a safer way to reinitialize buffer
3384 * flags. We also need a more accurate method for computing per-request
3385 * data.
3386 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003387 cs = objt_cs(s->si[1].end);
3388 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003389
Willy Tarreau4213a112013-12-15 10:25:42 +01003390 /* unless we're doing keep-alive, we want to quickly close the connection
3391 * to the server.
3392 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003393 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003394 !si_conn_ready(&s->si[1]) || !srv_conn->owner) {
Willy Tarreau350f4872014-11-28 14:42:25 +01003395 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3396 si_shutr(&s->si[1]);
3397 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003398 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003399
Willy Tarreaue7dff022015-04-03 01:14:29 +02003400 if (s->flags & SF_BE_ASSIGNED) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003401 _HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003402 if (unlikely(s->srv_conn))
3403 sess_change_server(s, NULL);
3404 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003405
3406 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003407 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003408
Willy Tarreaueee5b512015-04-03 23:46:31 +02003409 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003410 int n;
3411
Willy Tarreaueee5b512015-04-03 23:46:31 +02003412 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003413 if (n < 1 || n > 5)
3414 n = 0;
3415
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003416 if (fe->mode == PR_MODE_HTTP) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003417 _HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003418 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003419 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003420 (be->mode == PR_MODE_HTTP)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003421 _HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3422 _HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003423 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003424 }
3425
3426 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003427 s->logs.bytes_in -= ci_data(&s->req);
3428 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003429
Willy Tarreau66425e32018-07-25 06:55:12 +02003430 /* we may need to know the position in the queue */
3431 pendconn_free(s);
3432
Willy Tarreau610ecce2010-01-04 21:15:02 +01003433 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003434 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003435 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003436 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003437 s->do_log(s);
3438 }
3439
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003440 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003441 stream_stop_content_counters(s);
3442 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003443
Willy Tarreau9efd7452018-05-31 14:48:54 +02003444 /* reset the profiling counter */
3445 s->task->calls = 0;
3446 s->task->cpu_time = 0;
3447 s->task->lat_time = 0;
3448 s->task->call_date = (profiling & HA_PROF_TASKS) ? now_mono_time() : 0;
3449
Willy Tarreau610ecce2010-01-04 21:15:02 +01003450 s->logs.accept_date = date; /* user-visible date for logging */
3451 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003452 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3453 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003454 tv_zero(&s->logs.tv_request);
3455 s->logs.t_queue = -1;
3456 s->logs.t_connect = -1;
3457 s->logs.t_data = -1;
3458 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003459 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3460 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003461
Willy Tarreauf37954d2018-06-15 18:31:02 +02003462 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3463 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003464
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003465 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003466 if (s->flags & SF_CURR_SESS) {
3467 s->flags &= ~SF_CURR_SESS;
Olivier Houcharda798bf52019-03-08 18:52:00 +01003468 _HA_ATOMIC_SUB(&__objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003469 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003470 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003471 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003472 }
3473
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003474 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003475
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003476
3477 /* If we're doing keepalive, first call the mux detach() method
3478 * to let it know we want to detach without freing the connection.
3479 * We then can call si_release_endpoint() to destroy the conn_stream
Willy Tarreau4213a112013-12-15 10:25:42 +01003480 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003481 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houcharda70e1762018-12-13 18:01:00 +01003482 !si_conn_ready(&s->si[1]) ||
3483 (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 +02003484 srv_conn = NULL;
Olivier Houchard985f1392018-11-30 17:31:52 +01003485 else if (!srv_conn->owner) {
3486 srv_conn->owner = s->sess;
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003487 /* Add it unconditionally to the session list, it'll be removed
3488 * later if needed by session_check_idle_conn(), once we'll
3489 * have released the endpoint and know if it no longer has
3490 * attached streams, and so an idling connection
3491 */
Olivier Houchard351411f2018-12-27 17:20:54 +01003492 if (!session_add_conn(s->sess, srv_conn, s->target)) {
3493 srv_conn->owner = NULL;
3494 /* Try to add the connection to the server idle list.
3495 * If it fails, as the connection no longer has an
3496 * owner, it will be destroy later by
3497 * si_release_endpoint(), anyway
3498 */
3499 srv_add_to_idle_list(objt_server(srv_conn->target), srv_conn);
3500 srv_conn = NULL;
3501
3502 }
Olivier Houchard985f1392018-11-30 17:31:52 +01003503 }
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003504 si_release_endpoint(&s->si[1]);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003505 if (srv_conn && srv_conn->owner == s->sess) {
3506 if (session_check_idle_conn(s->sess, srv_conn) != 0)
3507 srv_conn = NULL;
3508 }
3509
Willy Tarreau4213a112013-12-15 10:25:42 +01003510
Willy Tarreau350f4872014-11-28 14:42:25 +01003511 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3512 s->si[1].err_type = SI_ET_NONE;
3513 s->si[1].conn_retries = 0; /* used for logging too */
3514 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003515 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 +01003516 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 +02003517 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 +02003518 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3519 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3520 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003521
Patrick Hemmere3faf022018-08-22 10:02:00 -04003522 hlua_ctx_destroy(s->hlua);
3523 s->hlua = NULL;
3524
Willy Tarreaueee5b512015-04-03 23:46:31 +02003525 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003526 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003527 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003528
3529 if (prev_status == 401 || prev_status == 407) {
3530 /* In HTTP keep-alive mode, if we receive a 401, we still have
3531 * a chance of being able to send the visitor again to the same
3532 * server over the same connection. This is required by some
3533 * broken protocols such as NTLM, and anyway whenever there is
3534 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003535 * it's better to do it (at least it helps with debugging), at
3536 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003537 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003538 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003539 }
3540
Willy Tarreau53f96852016-02-02 18:50:47 +01003541 /* Never ever allow to reuse a connection from a non-reuse backend */
3542 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3543 srv_conn->flags |= CO_FL_PRIVATE;
3544
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003545 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003546 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003547
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003548 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003549 s->req.flags |= CF_NEVER_WAIT;
3550 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003551 }
3552
Willy Tarreau714ea782015-11-25 20:11:11 +01003553 /* we're removing the analysers, we MUST re-enable events detection.
3554 * We don't enable close on the response channel since it's either
3555 * already closed, or in keep-alive with an idle connection handler.
3556 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003557 channel_auto_read(&s->req);
3558 channel_auto_close(&s->req);
3559 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003560
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003561 /* we're in keep-alive with an idle connection, monitor it if not already done */
3562 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003563 srv = objt_server(srv_conn->target);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003564 if (srv) {
3565 if (srv_conn->flags & CO_FL_PRIVATE)
3566 LIST_ADD(&srv->priv_conns[tid], &srv_conn->list);
3567 else if (prev_flags & TX_NOT_FIRST)
3568 /* note: we check the request, not the connection, but
3569 * this is valid for strategies SAFE and AGGR, and in
3570 * case of ALWS, we don't care anyway.
3571 */
3572 LIST_ADD(&srv->safe_conns[tid], &srv_conn->list);
3573 else
3574 LIST_ADD(&srv->idle_conns[tid], &srv_conn->list);
3575 }
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003576 }
Christopher Faulete6006242017-03-10 11:52:44 +01003577 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3578 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003579}
3580
3581
3582/* This function updates the request state machine according to the response
3583 * state machine and buffer flags. It returns 1 if it changes anything (flag
3584 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3585 * it is only used to find when a request/response couple is complete. Both
3586 * this function and its equivalent should loop until both return zero. It
3587 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3588 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003589int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003590{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003591 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003592 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003593 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003594 unsigned int old_state = txn->req.msg_state;
3595
Christopher Faulet4be98032017-07-18 10:48:24 +02003596 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003597 return 0;
3598
3599 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003600 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003601 * We can shut the read side unless we want to abort_on_close,
3602 * or we have a POST request. The issue with POST requests is
3603 * that some browsers still send a CRLF after the request, and
3604 * this CRLF must be read so that it does not remain in the kernel
3605 * buffers, otherwise a close could cause an RST on some systems
3606 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003607 * Note that if we're using keep-alive on the client side, we'd
3608 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003609 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003610 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003611 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003612 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3613 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003614 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3615 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003616 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003617 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003618
Willy Tarreau40f151a2012-12-20 12:10:09 +01003619 /* if the server closes the connection, we want to immediately react
3620 * and close the socket to save packets and syscalls.
3621 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003622 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003623
Willy Tarreau7f876a12015-11-18 11:59:55 +01003624 /* In any case we've finished parsing the request so we must
3625 * disable Nagle when sending data because 1) we're not going
3626 * to shut this side, and 2) the server is waiting for us to
3627 * send pending data.
3628 */
3629 chn->flags |= CF_NEVER_WAIT;
3630
Willy Tarreau610ecce2010-01-04 21:15:02 +01003631 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3632 goto wait_other_side;
3633
3634 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3635 /* The server has not finished to respond, so we
3636 * don't want to move in order not to upset it.
3637 */
3638 goto wait_other_side;
3639 }
3640
Willy Tarreau610ecce2010-01-04 21:15:02 +01003641 /* When we get here, it means that both the request and the
3642 * response have finished receiving. Depending on the connection
3643 * mode, we'll have to wait for the last bytes to leave in either
3644 * direction, and sometimes for a close to be effective.
3645 */
3646
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003647 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3648 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003649 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3650 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003651 }
3652 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3653 /* Option forceclose is set, or either side wants to close,
3654 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003655 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003656 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003657 *
3658 * However, there is an exception if the response
3659 * length is undefined. In this case, we need to wait
3660 * the close from the server. The response will be
3661 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003662 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003663 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3664 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3665 goto check_channel_flags;
3666
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003667 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3668 channel_shutr_now(chn);
3669 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003670 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003671 }
3672 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003673 /* The last possible modes are keep-alive and tunnel. Tunnel mode
3674 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003675 */
Willy Tarreau4213a112013-12-15 10:25:42 +01003676 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
3677 channel_auto_read(chn);
3678 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003679 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003680 }
3681
Christopher Faulet4be98032017-07-18 10:48:24 +02003682 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003683 }
3684
3685 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3686 http_msg_closing:
3687 /* nothing else to forward, just waiting for the output buffer
3688 * to be empty and for the shutw_now to take effect.
3689 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003690 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003691 txn->req.msg_state = HTTP_MSG_CLOSED;
3692 goto http_msg_closed;
3693 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003694 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003695 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003696 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003697 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003698 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003699 }
3700
3701 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3702 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01003703 /* if we don't know whether the server will close, we need to hard close */
3704 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
3705 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3706
Willy Tarreau3988d932013-12-27 23:03:08 +01003707 /* see above in MSG_DONE why we only do this in these states */
3708 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3709 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003710 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3711 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01003712 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003713 goto wait_other_side;
3714 }
3715
Christopher Faulet4be98032017-07-18 10:48:24 +02003716 check_channel_flags:
3717 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
3718 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
3719 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02003720 txn->req.msg_state = HTTP_MSG_CLOSING;
3721 goto http_msg_closing;
3722 }
3723
3724
Willy Tarreau610ecce2010-01-04 21:15:02 +01003725 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003726 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003727}
3728
3729
3730/* This function updates the response state machine according to the request
3731 * state machine and buffer flags. It returns 1 if it changes anything (flag
3732 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3733 * it is only used to find when a request/response couple is complete. Both
3734 * this function and its equivalent should loop until both return zero. It
3735 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3736 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003737int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003738{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003739 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003740 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003741 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003742 unsigned int old_state = txn->rsp.msg_state;
3743
Christopher Faulet4be98032017-07-18 10:48:24 +02003744 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003745 return 0;
3746
3747 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3748 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003749 * still monitor the server connection for a possible close
3750 * while the request is being uploaded, so we don't disable
3751 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003752 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003753 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003754
3755 if (txn->req.msg_state == HTTP_MSG_ERROR)
3756 goto wait_other_side;
3757
3758 if (txn->req.msg_state < HTTP_MSG_DONE) {
3759 /* The client seems to still be sending data, probably
3760 * because we got an error response during an upload.
3761 * We have the choice of either breaking the connection
3762 * or letting it pass through. Let's do the later.
3763 */
3764 goto wait_other_side;
3765 }
3766
Willy Tarreau610ecce2010-01-04 21:15:02 +01003767 /* When we get here, it means that both the request and the
3768 * response have finished receiving. Depending on the connection
3769 * mode, we'll have to wait for the last bytes to leave in either
3770 * direction, and sometimes for a close to be effective.
3771 */
3772
3773 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3774 /* Server-close mode : shut read and wait for the request
3775 * side to close its output buffer. The caller will detect
3776 * when we're in DONE and the other is in CLOSED and will
3777 * catch that for the final cleanup.
3778 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003779 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
3780 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003781 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003782 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3783 /* Option forceclose is set, or either side wants to close,
3784 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003785 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003786 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003787 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01003788 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003789 channel_shutr_now(chn);
3790 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003791 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003792 }
3793 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003794 /* The last possible modes are keep-alive and tunnel. Tunnel will
3795 * need to forward remaining data. Keep-alive will need to monitor
3796 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003797 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003798 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02003799 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01003800 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
3801 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003802 }
3803
Christopher Faulet4be98032017-07-18 10:48:24 +02003804 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003805 }
3806
3807 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3808 http_msg_closing:
3809 /* nothing else to forward, just waiting for the output buffer
3810 * to be empty and for the shutw_now to take effect.
3811 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003812 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003813 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3814 goto http_msg_closed;
3815 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003816 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02003817 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003818 txn->rsp.msg_state = HTTP_MSG_ERROR;
Olivier Houcharda798bf52019-03-08 18:52:00 +01003819 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003820 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01003821 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003822 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003823 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003824 }
3825
3826 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3827 http_msg_closed:
3828 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01003829 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003830 channel_auto_close(chn);
3831 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003832 goto wait_other_side;
3833 }
3834
Christopher Faulet4be98032017-07-18 10:48:24 +02003835 check_channel_flags:
3836 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
3837 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
3838 /* if we've just closed an output, let's switch */
3839 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3840 goto http_msg_closing;
3841 }
3842
Willy Tarreau610ecce2010-01-04 21:15:02 +01003843 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02003844 /* We force the response to leave immediately if we're waiting for the
3845 * other side, since there is no pending shutdown to push it out.
3846 */
3847 if (!channel_is_empty(chn))
3848 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003849 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003850}
3851
3852
Christopher Faulet894da4c2017-07-18 11:29:07 +02003853/* Resync the request and response state machines. */
3854void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003855{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003856 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02003857#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01003858 int old_req_state = txn->req.msg_state;
3859 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02003860#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01003861
Willy Tarreau610ecce2010-01-04 21:15:02 +01003862 http_sync_req_state(s);
3863 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003864 if (!http_sync_res_state(s))
3865 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003866 if (!http_sync_req_state(s))
3867 break;
3868 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02003869
Christopher Faulet894da4c2017-07-18 11:29:07 +02003870 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
3871 "req->analysers=0x%08x res->analysers=0x%08x\n",
3872 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02003873 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
3874 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02003875 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02003876
3877
Willy Tarreau610ecce2010-01-04 21:15:02 +01003878 /* OK, both state machines agree on a compatible state.
3879 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01003880 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3881 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02003882 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
3883 * means we must abort the request.
3884 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
3885 * corresponding channel.
3886 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
3887 * on the response with server-close mode means we've completed one
3888 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003889 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02003890 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3891 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01003892 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003893 channel_auto_close(&s->req);
3894 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003895 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003896 channel_auto_close(&s->res);
3897 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003898 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02003899 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3900 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01003901 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01003902 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003903 channel_auto_close(&s->res);
3904 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003905 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003906 channel_abort(&s->req);
3907 channel_auto_close(&s->req);
3908 channel_auto_read(&s->req);
3909 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003910 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02003911 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3912 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3913 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3914 s->req.analysers &= AN_REQ_FLT_END;
3915 if (HAS_REQ_DATA_FILTERS(s))
3916 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
3917 }
3918 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3919 s->res.analysers &= AN_RES_FLT_END;
3920 if (HAS_RSP_DATA_FILTERS(s))
3921 s->res.analysers |= AN_RES_FLT_XFER_DATA;
3922 }
3923 channel_auto_close(&s->req);
3924 channel_auto_read(&s->req);
3925 channel_auto_close(&s->res);
3926 channel_auto_read(&s->res);
3927 }
Willy Tarreau4213a112013-12-15 10:25:42 +01003928 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
3929 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003930 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01003931 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3932 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3933 /* server-close/keep-alive: terminate this transaction,
3934 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02003935 * a fresh-new transaction, but only once we're sure there's
3936 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02003937 * another request. They must not hold any pending output data
3938 * and the response buffer must realigned
3939 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01003940 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003941 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02003942 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02003943 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02003944 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02003945 else {
3946 s->req.analysers = AN_REQ_FLT_END;
3947 s->res.analysers = AN_RES_FLT_END;
3948 txn->flags |= TX_WAIT_CLEANUP;
3949 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003950 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003951}
3952
Willy Tarreaud98cf932009-12-27 22:54:55 +01003953/* This function is an analyser which forwards request body (including chunk
3954 * sizes if any). It is called as soon as we must forward, even if we forward
3955 * zero byte. The only situation where it must not be called is when we're in
3956 * tunnel mode and we want to forward till the close. It's used both to forward
3957 * remaining data and to resync after end of body. It expects the msg_state to
3958 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02003959 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01003960 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02003961 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003962 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003963int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01003964{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003965 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003966 struct http_txn *txn = s->txn;
3967 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01003968 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003969
Christopher Faulete0768eb2018-10-03 16:38:02 +02003970 if (IS_HTX_STRM(s))
3971 return htx_request_forward_body(s, req, an_bit);
3972
Christopher Faulet45073512018-07-20 10:16:29 +02003973 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 +02003974 now_ms, __FUNCTION__,
3975 s,
3976 req,
3977 req->rex, req->wex,
3978 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003979 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02003980 req->analysers);
3981
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003982 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3983 return 0;
3984
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003985 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003986 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02003987 /* Output closed while we were sending data. We must abort and
3988 * wake the other side up.
3989 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003990 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02003991 msg->msg_state = HTTP_MSG_ERROR;
3992 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003993 return 1;
3994 }
3995
Willy Tarreaud98cf932009-12-27 22:54:55 +01003996 /* Note that we don't have to send 100-continue back because we don't
3997 * need the data to complete our job, and it's up to the server to
3998 * decide whether to return 100, 417 or anything else in return of
3999 * an "Expect: 100-continue" header.
4000 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004001 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004002 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4003 ? HTTP_MSG_CHUNK_SIZE
4004 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004005
4006 /* TODO/filters: when http-buffer-request option is set or if a
4007 * rule on url_param exists, the first chunk size could be
4008 * already parsed. In that case, msg->next is after the chunk
4009 * size (including the CRLF after the size). So this case should
4010 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004011 }
4012
Willy Tarreau7ba23542014-04-17 21:50:00 +02004013 /* Some post-connect processing might want us to refrain from starting to
4014 * forward data. Currently, the only reason for this is "balance url_param"
4015 * whichs need to parse/process the request after we've enabled forwarding.
4016 */
4017 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004018 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004019 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004020 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004021 channel_dont_close(req); /* don't fail on early shutr */
4022 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004023 }
4024 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4025 }
4026
Willy Tarreau80a92c02014-03-12 10:41:13 +01004027 /* in most states, we should abort in case of early close */
4028 channel_auto_close(req);
4029
Willy Tarreauefdf0942014-04-24 20:08:57 +02004030 if (req->to_forward) {
4031 /* We can't process the buffer's contents yet */
4032 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004033 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004034 }
4035
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004036 if (msg->msg_state < HTTP_MSG_DONE) {
4037 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4038 ? http_msg_forward_chunked_body(s, msg)
4039 : http_msg_forward_body(s, msg));
4040 if (!ret)
4041 goto missing_data_or_waiting;
4042 if (ret < 0)
4043 goto return_bad_req;
4044 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004045
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004046 /* other states, DONE...TUNNEL */
4047 /* we don't want to forward closes on DONE except in tunnel mode. */
4048 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4049 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004050
Christopher Faulet894da4c2017-07-18 11:29:07 +02004051 http_resync_states(s);
4052 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004053 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4054 if (req->flags & CF_SHUTW) {
4055 /* request errors are most likely due to the
4056 * server aborting the transfer. */
4057 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004058 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004059 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004060 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004061 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004062 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004063 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004064 }
4065
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004066 /* If "option abortonclose" is set on the backend, we want to monitor
4067 * the client's connection and forward any shutdown notification to the
4068 * server, which will decide whether to close or to go on processing the
4069 * request. We only do that in tunnel mode, and not in other modes since
4070 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004071 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004072 channel_auto_read(req);
4073 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4074 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4075 s->si[1].flags |= SI_FL_NOLINGER;
4076 channel_auto_close(req);
4077 }
4078 else if (s->txn->meth == HTTP_METH_POST) {
4079 /* POST requests may require to read extra CRLF sent by broken
4080 * browsers and which could cause an RST to be sent upon close
4081 * on some systems (eg: Linux). */
4082 channel_auto_read(req);
4083 }
4084 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004085
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004086 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004087 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004088 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004089 if (!(s->flags & SF_ERR_MASK))
4090 s->flags |= SF_ERR_CLICL;
4091 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004092 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004093 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004094 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004095 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004096 }
4097
Olivier Houcharda798bf52019-03-08 18:52:00 +01004098 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4099 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004100 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004101 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004102
4103 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004104 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004105
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004106 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004107 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004108 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004109 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004110
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004111 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004112 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004113 * And when content-length is used, we never want to let the possible
4114 * shutdown be forwarded to the other side, as the state machine will
4115 * take care of it once the client responds. It's also important to
4116 * prevent TIME_WAITs from accumulating on the backend side, and for
4117 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004118 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004119 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004120 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004121
Willy Tarreau5c620922011-05-11 19:56:11 +02004122 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004123 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004124 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004125 * modes are already handled by the stream sock layer. We must not do
4126 * this in content-length mode because it could present the MSG_MORE
4127 * flag with the last block of forwarded data, which would cause an
4128 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004129 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004130 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004131 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004132
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004133 return 0;
4134
Willy Tarreaud98cf932009-12-27 22:54:55 +01004135 return_bad_req: /* let's centralize all bad requests */
Olivier Houcharda798bf52019-03-08 18:52:00 +01004136 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004137 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01004138 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004139
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004140 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004141 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004142 txn->req.msg_state = HTTP_MSG_ERROR;
4143 if (txn->status) {
4144 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004145 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004146 } else {
4147 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004148 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004149 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004150 req->analysers &= AN_REQ_FLT_END;
4151 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 +01004152
Willy Tarreaue7dff022015-04-03 01:14:29 +02004153 if (!(s->flags & SF_ERR_MASK))
4154 s->flags |= SF_ERR_PRXCOND;
4155 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004156 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004157 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004158 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004159 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004160 }
4161 return 0;
4162
4163 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004164 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004165 txn->req.msg_state = HTTP_MSG_ERROR;
4166 if (txn->status) {
4167 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004168 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004169 } else {
4170 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004171 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004172 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004173 req->analysers &= AN_REQ_FLT_END;
4174 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 +01004175
Olivier Houcharda798bf52019-03-08 18:52:00 +01004176 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4177 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004178 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004179 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004180
Willy Tarreaue7dff022015-04-03 01:14:29 +02004181 if (!(s->flags & SF_ERR_MASK))
4182 s->flags |= SF_ERR_SRVCL;
4183 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004184 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004185 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004186 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004187 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004188 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004189 return 0;
4190}
4191
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004192/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4193 * processing can continue on next analysers, or zero if it either needs more
4194 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004195 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004196 * when it has nothing left to do, and may remove any analyser when it wants to
4197 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004198 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004199int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004200{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004201 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004202 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004203 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004204 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004205 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004206 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004207 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004208 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004209
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004210 srv_conn = cs_conn(objt_cs(s->si[1].end));
4211
Christopher Faulete0768eb2018-10-03 16:38:02 +02004212 if (IS_HTX_STRM(s))
4213 return htx_wait_for_response(s, rep, an_bit);
4214
Christopher Faulet45073512018-07-20 10:16:29 +02004215 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 +02004216 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004217 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004218 rep,
4219 rep->rex, rep->wex,
4220 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004221 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004222 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004223
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004224 /*
4225 * Now parse the partial (or complete) lines.
4226 * We will check the response syntax, and also join multi-line
4227 * headers. An index of all the lines will be elaborated while
4228 * parsing.
4229 *
4230 * For the parsing, we use a 28 states FSM.
4231 *
4232 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004233 * ci_head(rep) = beginning of response
4234 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4235 * ci_tail(rep) = end of input data
4236 * msg->eol = end of current header or line (LF or CRLF)
4237 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004238 */
4239
Willy Tarreau628c40c2014-04-24 19:11:26 +02004240 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004241 /* There's a protected area at the end of the buffer for rewriting
4242 * purposes. We don't want to start to parse the request if the
4243 * protected area is affected, because we may have to move processed
4244 * data later, which is much more complicated.
4245 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004246 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004247 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004248 /* some data has still not left the buffer, wake us once that's done */
4249 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4250 goto abort_response;
4251 channel_dont_close(rep);
4252 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004253 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004254 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004255 }
4256
Willy Tarreau188e2302018-06-15 11:11:53 +02004257 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004258 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004259 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004260
Willy Tarreauf37954d2018-06-15 18:31:02 +02004261 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004262 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004263 }
4264
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004265 /* 1: we might have to print this header in debug mode */
4266 if (unlikely((global.mode & MODE_DEBUG) &&
4267 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004268 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004269 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004270
Willy Tarreauf37954d2018-06-15 18:31:02 +02004271 sol = ci_head(rep);
4272 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004273 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004274
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004275 sol += hdr_idx_first_pos(&txn->hdr_idx);
4276 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004277
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004278 while (cur_idx) {
4279 eol = sol + txn->hdr_idx.v[cur_idx].len;
4280 debug_hdr("srvhdr", s, sol, eol);
4281 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4282 cur_idx = txn->hdr_idx.v[cur_idx].next;
4283 }
4284 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004285
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004286 /*
4287 * Now we quickly check if we have found a full valid response.
4288 * If not so, we check the FD and buffer states before leaving.
4289 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004290 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004291 * responses are checked first.
4292 *
4293 * Depending on whether the client is still there or not, we
4294 * may send an error response back or not. Note that normally
4295 * we should only check for HTTP status there, and check I/O
4296 * errors somewhere else.
4297 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004298
Willy Tarreau655dce92009-11-08 13:10:58 +01004299 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004300 /* Invalid response */
4301 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4302 /* we detected a parsing error. We want to archive this response
4303 * in the dedicated proxy area for later troubleshooting.
4304 */
4305 hdr_response_bad:
4306 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004307 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004308
Olivier Houcharda798bf52019-03-08 18:52:00 +01004309 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004310 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004311 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004312 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004313 }
Willy Tarreau64648412010-03-05 10:41:54 +01004314 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004315 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004316 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004317 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004318 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004319 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004320 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004321
Willy Tarreaue7dff022015-04-03 01:14:29 +02004322 if (!(s->flags & SF_ERR_MASK))
4323 s->flags |= SF_ERR_PRXCOND;
4324 if (!(s->flags & SF_FINST_MASK))
4325 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004326
4327 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004328 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004329
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004330 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004331 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004332 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004333 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004334 goto hdr_response_bad;
4335 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004336
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004337 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004338 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004339 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004340 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004341 else if (txn->flags & TX_NOT_FIRST)
4342 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004343
Olivier Houcharda798bf52019-03-08 18:52:00 +01004344 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004345 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004346 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004347 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004348 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004349
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004350 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004351 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004352 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004353
4354 /* Check to see if the server refused the early data.
4355 * If so, just send a 425
4356 */
4357 if (objt_cs(s->si[1].end)) {
4358 struct connection *conn = objt_cs(s->si[1].end)->conn;
4359
4360 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4361 txn->status = 425;
4362 }
4363
Willy Tarreau350f4872014-11-28 14:42:25 +01004364 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004365 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004366 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004367
Willy Tarreaue7dff022015-04-03 01:14:29 +02004368 if (!(s->flags & SF_ERR_MASK))
4369 s->flags |= SF_ERR_SRVCL;
4370 if (!(s->flags & SF_FINST_MASK))
4371 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004372 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004373 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004374
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004375 /* read timeout : return a 504 to the client. */
4376 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004377 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004378 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004379
Olivier Houcharda798bf52019-03-08 18:52:00 +01004380 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004381 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004382 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004383 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004384 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004385
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004386 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004387 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004388 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004389 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004390 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004391 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004392
Willy Tarreaue7dff022015-04-03 01:14:29 +02004393 if (!(s->flags & SF_ERR_MASK))
4394 s->flags |= SF_ERR_SRVTO;
4395 if (!(s->flags & SF_FINST_MASK))
4396 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004397 return 0;
4398 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004399
Willy Tarreauf003d372012-11-26 13:35:37 +01004400 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004401 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004402 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4403 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004404 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004405 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004406
Christopher Faulet0184ea72017-01-05 14:06:34 +01004407 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004408 channel_auto_close(rep);
4409
4410 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004411 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004412 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004413
Willy Tarreaue7dff022015-04-03 01:14:29 +02004414 if (!(s->flags & SF_ERR_MASK))
4415 s->flags |= SF_ERR_CLICL;
4416 if (!(s->flags & SF_FINST_MASK))
4417 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004418
Willy Tarreau87b09662015-04-03 00:22:06 +02004419 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004420 return 0;
4421 }
4422
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004423 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004424 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004425 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004426 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004427 else if (txn->flags & TX_NOT_FIRST)
4428 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004429
Olivier Houcharda798bf52019-03-08 18:52:00 +01004430 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004431 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004432 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004433 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004434 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004435
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004436 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004437 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004438 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004439 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004440 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004441 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004442
Willy Tarreaue7dff022015-04-03 01:14:29 +02004443 if (!(s->flags & SF_ERR_MASK))
4444 s->flags |= SF_ERR_SRVCL;
4445 if (!(s->flags & SF_FINST_MASK))
4446 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004447 return 0;
4448 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004449
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004450 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004451 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004452 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004453 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004454 else if (txn->flags & TX_NOT_FIRST)
4455 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004456
Olivier Houcharda798bf52019-03-08 18:52:00 +01004457 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004458 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004459 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004460
Willy Tarreaue7dff022015-04-03 01:14:29 +02004461 if (!(s->flags & SF_ERR_MASK))
4462 s->flags |= SF_ERR_CLICL;
4463 if (!(s->flags & SF_FINST_MASK))
4464 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004465
Willy Tarreau87b09662015-04-03 00:22:06 +02004466 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004467 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004468 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004469
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004470 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004471 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004472 return 0;
4473 }
4474
4475 /* More interesting part now : we know that we have a complete
4476 * response which at least looks like HTTP. We have an indicator
4477 * of each header's length, so we can parse them quickly.
4478 */
4479
4480 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004481 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004482
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004483 /*
4484 * 1: get the status code
4485 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004486 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004487 if (n < 1 || n > 5)
4488 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004489 /* when the client triggers a 4xx from the server, it's most often due
4490 * to a missing object or permission. These events should be tracked
4491 * because if they happen often, it may indicate a brute force or a
4492 * vulnerability scan.
4493 */
4494 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004495 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004496
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004497 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004498 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004499
Willy Tarreau91852eb2015-05-01 13:26:00 +02004500 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4501 * exactly one digit "." one digit. This check may be disabled using
4502 * option accept-invalid-http-response.
4503 */
4504 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4505 if (msg->sl.st.v_l != 8) {
4506 msg->err_pos = 0;
4507 goto hdr_response_bad;
4508 }
4509
Willy Tarreauf37954d2018-06-15 18:31:02 +02004510 if (ci_head(rep)[4] != '/' ||
4511 !isdigit((unsigned char)ci_head(rep)[5]) ||
4512 ci_head(rep)[6] != '.' ||
4513 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004514 msg->err_pos = 4;
4515 goto hdr_response_bad;
4516 }
4517 }
4518
Willy Tarreau5b154472009-12-21 20:11:07 +01004519 /* check if the response is HTTP/1.1 or above */
4520 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004521 ((ci_head(rep)[5] > '1') ||
4522 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004523 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004524
4525 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004526 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 +01004527
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004528 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004529 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004530
Willy Tarreauf37954d2018-06-15 18:31:02 +02004531 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004532
Willy Tarreau39650402010-03-15 19:44:39 +01004533 /* Adjust server's health based on status code. Note: status codes 501
4534 * and 505 are triggered on demand by client request, so we must not
4535 * count them as server failures.
4536 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004537 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004538 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004539 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004540 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004541 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004542 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004543
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004544 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004545 * We may be facing a 100-continue response, or any other informational
4546 * 1xx response which is non-final, in which case this is not the right
4547 * response, and we're waiting for the next one. Let's allow this response
4548 * to go to the client and wait for the next one. There's an exception for
4549 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004550 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004551 if (txn->status < 200 &&
4552 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004553 hdr_idx_init(&txn->hdr_idx);
4554 msg->next -= channel_forward(rep, msg->next);
4555 msg->msg_state = HTTP_MSG_RPBEFORE;
4556 txn->status = 0;
4557 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004558 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004559 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004560 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004561
Willy Tarreaua14ad722017-07-07 11:36:32 +02004562 /*
4563 * 2: check for cacheability.
4564 */
4565
4566 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004567 case 200:
4568 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004569 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004570 case 206:
4571 case 300:
4572 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004573 case 404:
4574 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004575 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004576 case 414:
4577 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004578 break;
4579 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004580 /* RFC7231#6.1:
4581 * Responses with status codes that are defined as
4582 * cacheable by default (e.g., 200, 203, 204, 206,
4583 * 300, 301, 404, 405, 410, 414, and 501 in this
4584 * specification) can be reused by a cache with
4585 * heuristic expiration unless otherwise indicated
4586 * by the method definition or explicit cache
4587 * controls [RFC7234]; all other status codes are
4588 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004589 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004590 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004591 break;
4592 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004593
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004594 /*
4595 * 3: we may need to capture headers
4596 */
4597 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004598 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004599 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4600 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004601
Willy Tarreau557f1992015-05-01 10:05:17 +02004602 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4603 * by RFC7230#3.3.3 :
4604 *
4605 * The length of a message body is determined by one of the following
4606 * (in order of precedence):
4607 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004608 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4609 * the connection will become a tunnel immediately after the empty
4610 * line that concludes the header fields. A client MUST ignore
4611 * any Content-Length or Transfer-Encoding header fields received
4612 * in such a message. Any 101 response (Switching Protocols) is
4613 * managed in the same manner.
4614 *
4615 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004616 * (Informational), 204 (No Content), or 304 (Not Modified) status
4617 * code is always terminated by the first empty line after the
4618 * header fields, regardless of the header fields present in the
4619 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004620 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004621 * 3. If a Transfer-Encoding header field is present and the chunked
4622 * transfer coding (Section 4.1) is the final encoding, the message
4623 * body length is determined by reading and decoding the chunked
4624 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004625 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004626 * If a Transfer-Encoding header field is present in a response and
4627 * the chunked transfer coding is not the final encoding, the
4628 * message body length is determined by reading the connection until
4629 * it is closed by the server. If a Transfer-Encoding header field
4630 * is present in a request and the chunked transfer coding is not
4631 * the final encoding, the message body length cannot be determined
4632 * reliably; the server MUST respond with the 400 (Bad Request)
4633 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004634 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004635 * If a message is received with both a Transfer-Encoding and a
4636 * Content-Length header field, the Transfer-Encoding overrides the
4637 * Content-Length. Such a message might indicate an attempt to
4638 * perform request smuggling (Section 9.5) or response splitting
4639 * (Section 9.4) and ought to be handled as an error. A sender MUST
4640 * remove the received Content-Length field prior to forwarding such
4641 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004642 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004643 * 4. If a message is received without Transfer-Encoding and with
4644 * either multiple Content-Length header fields having differing
4645 * field-values or a single Content-Length header field having an
4646 * invalid value, then the message framing is invalid and the
4647 * recipient MUST treat it as an unrecoverable error. If this is a
4648 * request message, the server MUST respond with a 400 (Bad Request)
4649 * status code and then close the connection. If this is a response
4650 * message received by a proxy, the proxy MUST close the connection
4651 * to the server, discard the received response, and send a 502 (Bad
4652 * Gateway) response to the client. If this is a response message
4653 * received by a user agent, the user agent MUST close the
4654 * connection to the server and discard the received response.
4655 *
4656 * 5. If a valid Content-Length header field is present without
4657 * Transfer-Encoding, its decimal value defines the expected message
4658 * body length in octets. If the sender closes the connection or
4659 * the recipient times out before the indicated number of octets are
4660 * received, the recipient MUST consider the message to be
4661 * incomplete and close the connection.
4662 *
4663 * 6. If this is a request message and none of the above are true, then
4664 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004665 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004666 * 7. Otherwise, this is a response message without a declared message
4667 * body length, so the message body length is determined by the
4668 * number of octets received prior to the server closing the
4669 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004670 */
4671
4672 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004673 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004674 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004675 * FIXME: should we parse anyway and return an error on chunked encoding ?
4676 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004677 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4678 txn->status == 101)) {
4679 /* Either we've established an explicit tunnel, or we're
4680 * switching the protocol. In both cases, we're very unlikely
4681 * to understand the next protocols. We have to switch to tunnel
4682 * mode, so that we transfer the request and responses then let
4683 * this protocol pass unmodified. When we later implement specific
4684 * parsers for such protocols, we'll want to check the Upgrade
4685 * header which contains information about that protocol for
4686 * responses with status 101 (eg: see RFC2817 about TLS).
4687 */
4688 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4689 msg->flags |= HTTP_MSGF_XFER_LEN;
4690 goto end;
4691 }
4692
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004693 if (txn->meth == HTTP_METH_HEAD ||
4694 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004695 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004696 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004697 goto skip_content_length;
4698 }
4699
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004700 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004701 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004702 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004703 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004704 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4705 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004706 /* bad transfer-encoding (chunked followed by something else) */
4707 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004708 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004709 break;
4710 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004711 }
4712
Willy Tarreau1c913912015-04-30 10:57:51 +02004713 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004714 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02004715 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004716 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02004717 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4718 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02004719 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004720 signed long long cl;
4721
Willy Tarreauad14f752011-09-02 20:33:27 +02004722 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004723 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004724 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004725 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004726
Willy Tarreauad14f752011-09-02 20:33:27 +02004727 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004728 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004729 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004730 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004731
Willy Tarreauad14f752011-09-02 20:33:27 +02004732 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004733 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004734 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004735 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004736
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004737 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004738 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004739 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004740 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004741
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004742 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004743 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004744 }
4745
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004746 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
4747 * 407 (Proxy-Authenticate) responses and set the connection to private
4748 */
4749 if (srv_conn && txn->status == 401) {
4750 /* check for Negotiate/NTLM WWW-Authenticate headers */
4751 ctx.idx = 0;
4752 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
4753 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
4754 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
4755 srv_conn->flags |= CO_FL_PRIVATE;
4756 }
4757 } else if (srv_conn && txn->status == 407) {
4758 /* check for Negotiate/NTLM Proxy-Authenticate headers */
4759 ctx.idx = 0;
4760 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
4761 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
4762 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
4763 srv_conn->flags |= CO_FL_PRIVATE;
4764 }
4765 }
4766
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004767 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01004768 /* Now we have to check if we need to modify the Connection header.
4769 * This is more difficult on the response than it is on the request,
4770 * because we can have two different HTTP versions and we don't know
4771 * how the client will interprete a response. For instance, let's say
4772 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4773 * HTTP/1.1 response without any header. Maybe it will bound itself to
4774 * HTTP/1.0 because it only knows about it, and will consider the lack
4775 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4776 * the lack of header as a keep-alive. Thus we will use two flags
4777 * indicating how a request MAY be understood by the client. In case
4778 * of multiple possibilities, we'll fix the header to be explicit. If
4779 * ambiguous cases such as both close and keepalive are seen, then we
4780 * will fall back to explicit close. Note that we won't take risks with
4781 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004782 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004783 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004784 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02004785 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01004786 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004787
Willy Tarreau60466522010-01-18 19:08:45 +01004788 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02004789 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01004790 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004791
Willy Tarreau60466522010-01-18 19:08:45 +01004792 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02004793 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01004794 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004795 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004796 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004797 }
Willy Tarreau60466522010-01-18 19:08:45 +01004798 else { /* SCL / KAL */
4799 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004800 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004801 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004802 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004803
Willy Tarreau60466522010-01-18 19:08:45 +01004804 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004805 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004806
Willy Tarreau60466522010-01-18 19:08:45 +01004807 /* Some keep-alive responses are converted to Server-close if
4808 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004809 */
Willy Tarreau60466522010-01-18 19:08:45 +01004810 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4811 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004812 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004813 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004814 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004815 }
4816
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004817 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02004818 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02004819 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02004820
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004821 /* end of job, return OK */
4822 rep->analysers &= ~an_bit;
4823 rep->analyse_exp = TICK_ETERNITY;
4824 channel_auto_close(rep);
4825 return 1;
4826
4827 abort_keep_alive:
4828 /* A keep-alive request to the server failed on a network error.
4829 * The client is required to retry. We need to close without returning
4830 * any other information so that the client retries.
4831 */
4832 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004833 rep->analysers &= AN_RES_FLT_END;
4834 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004835 channel_auto_close(rep);
4836 s->logs.logwait = 0;
4837 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004838 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01004839 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004840 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004841 return 0;
4842}
4843
4844/* This function performs all the processing enabled for the current response.
4845 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004846 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004847 * other functions. It works like process_request (see indications above).
4848 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004849int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004850{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004851 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004852 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004853 struct http_msg *msg = &txn->rsp;
4854 struct proxy *cur_proxy;
4855 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004856 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004857
Christopher Faulete0768eb2018-10-03 16:38:02 +02004858 if (IS_HTX_STRM(s))
4859 return htx_process_res_common(s, rep, an_bit, px);
4860
Christopher Faulet45073512018-07-20 10:16:29 +02004861 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 +02004862 now_ms, __FUNCTION__,
4863 s,
4864 rep,
4865 rep->rex, rep->wex,
4866 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004867 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004868 rep->analysers);
4869
4870 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
4871 return 0;
4872
Willy Tarreau70730dd2014-04-24 18:06:27 +02004873 /* The stats applet needs to adjust the Connection header but we don't
4874 * apply any filter there.
4875 */
Willy Tarreau612adb82015-03-10 15:25:54 +01004876 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
4877 rep->analysers &= ~an_bit;
4878 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02004879 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01004880 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02004881
Willy Tarreau58975672014-04-24 21:13:57 +02004882 /*
4883 * We will have to evaluate the filters.
4884 * As opposed to version 1.2, now they will be evaluated in the
4885 * filters order and not in the header order. This means that
4886 * each filter has to be validated among all headers.
4887 *
4888 * Filters are tried with ->be first, then with ->fe if it is
4889 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004890 *
4891 * Maybe we are in resume condiion. In this case I choose the
4892 * "struct proxy" which contains the rule list matching the resume
4893 * pointer. If none of theses "struct proxy" match, I initialise
4894 * the process with the first one.
4895 *
4896 * In fact, I check only correspondance betwwen the current list
4897 * pointer and the ->fe rule list. If it doesn't match, I initialize
4898 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02004899 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004900 if (s->current_rule_list == &sess->fe->http_res_rules)
4901 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004902 else
4903 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02004904 while (1) {
4905 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004906
Willy Tarreau58975672014-04-24 21:13:57 +02004907 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02004908 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004909 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004910
Willy Tarreau51d861a2015-05-22 17:30:48 +02004911 if (ret == HTTP_RULE_RES_BADREQ)
4912 goto return_srv_prx_502;
4913
4914 if (ret == HTTP_RULE_RES_DONE) {
4915 rep->analysers &= ~an_bit;
4916 rep->analyse_exp = TICK_ETERNITY;
4917 return 1;
4918 }
4919 }
4920
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004921 /* we need to be called again. */
4922 if (ret == HTTP_RULE_RES_YIELD) {
4923 channel_dont_close(rep);
4924 return 0;
4925 }
4926
Willy Tarreau58975672014-04-24 21:13:57 +02004927 /* try headers filters */
4928 if (rule_set->rsp_exp != NULL) {
4929 if (apply_filters_to_response(s, rep, rule_set) < 0) {
4930 return_bad_resp:
4931 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004932 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004933 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004934 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01004935 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02004936 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004937 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02004938 txn->status = 502;
4939 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01004940 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004941 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004942 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02004943 if (!(s->flags & SF_ERR_MASK))
4944 s->flags |= SF_ERR_PRXCOND;
4945 if (!(s->flags & SF_FINST_MASK))
4946 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02004947 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004948 }
Willy Tarreau58975672014-04-24 21:13:57 +02004949 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004950
Willy Tarreau58975672014-04-24 21:13:57 +02004951 /* has the response been denied ? */
4952 if (txn->flags & TX_SVDENY) {
4953 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004954 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004955
Olivier Houcharda798bf52019-03-08 18:52:00 +01004956 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
4957 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004958 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01004959 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004960
Willy Tarreau58975672014-04-24 21:13:57 +02004961 goto return_srv_prx_502;
4962 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004963
Willy Tarreau58975672014-04-24 21:13:57 +02004964 /* add response headers from the rule sets in the same order */
4965 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02004966 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004967 break;
Willy Tarreau58975672014-04-24 21:13:57 +02004968 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004969 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02004970 ret = acl_pass(ret);
4971 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4972 ret = !ret;
4973 if (!ret)
4974 continue;
4975 }
Christopher Faulet10079f52018-10-03 15:17:28 +02004976 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02004977 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004978 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004979
Willy Tarreau58975672014-04-24 21:13:57 +02004980 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004981 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02004982 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004983 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02004984 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004985
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004986 /* After this point, this anayzer can't return yield, so we can
4987 * remove the bit corresponding to this analyzer from the list.
4988 *
4989 * Note that the intermediate returns and goto found previously
4990 * reset the analyzers.
4991 */
4992 rep->analysers &= ~an_bit;
4993 rep->analyse_exp = TICK_ETERNITY;
4994
Willy Tarreau58975672014-04-24 21:13:57 +02004995 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02004996 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02004997 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004998
Willy Tarreau58975672014-04-24 21:13:57 +02004999 /*
5000 * Now check for a server cookie.
5001 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005002 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005003 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005004
Willy Tarreau58975672014-04-24 21:13:57 +02005005 /*
5006 * Check for cache-control or pragma headers if required.
5007 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005008 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005009 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005010
Willy Tarreau58975672014-04-24 21:13:57 +02005011 /*
5012 * Add server cookie in the response if needed
5013 */
5014 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5015 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005016 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005017 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5018 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5019 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5020 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5021 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005022 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005023 /* the server is known, it's not the one the client requested, or the
5024 * cookie's last seen date needs to be refreshed. We have to
5025 * insert a set-cookie here, except if we want to insert only on POST
5026 * requests and this one isn't. Note that servers which don't have cookies
5027 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005028 */
Willy Tarreau58975672014-04-24 21:13:57 +02005029 if (!objt_server(s->target)->cookie) {
5030 chunk_printf(&trash,
5031 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5032 s->be->cookie_name);
5033 }
5034 else {
5035 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005036
Willy Tarreau58975672014-04-24 21:13:57 +02005037 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5038 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005039 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5040 s30tob64((date.tv_sec+3) >> 2,
5041 trash.area + trash.data);
5042 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005043
Willy Tarreau58975672014-04-24 21:13:57 +02005044 if (s->be->cookie_maxlife) {
5045 /* emit first_date, which is either the original one or
5046 * the current date.
5047 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005048 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005049 s30tob64(txn->cookie_first_date ?
5050 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005051 (date.tv_sec+3) >> 2,
5052 trash.area + trash.data);
5053 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005054 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005055 }
Willy Tarreau58975672014-04-24 21:13:57 +02005056 chunk_appendf(&trash, "; path=/");
5057 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005058
Willy Tarreau58975672014-04-24 21:13:57 +02005059 if (s->be->cookie_domain)
5060 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005061
Willy Tarreau58975672014-04-24 21:13:57 +02005062 if (s->be->ck_opts & PR_CK_HTTPONLY)
5063 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005064
Willy Tarreau58975672014-04-24 21:13:57 +02005065 if (s->be->ck_opts & PR_CK_SECURE)
5066 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005067
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005068 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005069 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005070
Willy Tarreau58975672014-04-24 21:13:57 +02005071 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005072 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005073 /* the server did not change, only the date was updated */
5074 txn->flags |= TX_SCK_UPDATED;
5075 else
5076 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005077
Willy Tarreau58975672014-04-24 21:13:57 +02005078 /* Here, we will tell an eventual cache on the client side that we don't
5079 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5080 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5081 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005082 */
Willy Tarreau58975672014-04-24 21:13:57 +02005083 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005084
Willy Tarreau58975672014-04-24 21:13:57 +02005085 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005086
Willy Tarreau58975672014-04-24 21:13:57 +02005087 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5088 "Cache-control: private", 22) < 0))
5089 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005090 }
Willy Tarreau58975672014-04-24 21:13:57 +02005091 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005092
Willy Tarreau58975672014-04-24 21:13:57 +02005093 /*
5094 * Check if result will be cacheable with a cookie.
5095 * We'll block the response if security checks have caught
5096 * nasty things such as a cacheable cookie.
5097 */
5098 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5099 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5100 (s->be->options & PR_O_CHK_CACHE)) {
5101 /* we're in presence of a cacheable response containing
5102 * a set-cookie header. We'll block it as requested by
5103 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005104 */
Willy Tarreau58975672014-04-24 21:13:57 +02005105 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01005106 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005107
Olivier Houcharda798bf52019-03-08 18:52:00 +01005108 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5109 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005110 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01005111 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005112
Christopher Faulet767a84b2017-11-24 16:50:31 +01005113 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5114 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005115 send_log(s->be, LOG_ALERT,
5116 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5117 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5118 goto return_srv_prx_502;
5119 }
Willy Tarreau03945942009-12-22 16:50:27 +01005120
Willy Tarreau70730dd2014-04-24 18:06:27 +02005121 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005122 /*
5123 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5124 * If an "Upgrade" token is found, the header is left untouched in order
5125 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005126 * if anything but "Upgrade" is present in the Connection header. We don't
5127 * want to touch any 101 response either since it's switching to another
5128 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005129 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005130 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005131 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005132 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005133
Willy Tarreau58975672014-04-24 21:13:57 +02005134 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5135 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5136 /* we want a keep-alive response here. Keep-alive header
5137 * required if either side is not 1.1.
5138 */
5139 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5140 want_flags |= TX_CON_KAL_SET;
5141 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005142 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005143 /* we want a close response here. Close header required if
5144 * the server is 1.1, regardless of the client.
5145 */
5146 if (msg->flags & HTTP_MSGF_VER_11)
5147 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005148 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005149
Willy Tarreau58975672014-04-24 21:13:57 +02005150 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5151 http_change_connection_header(txn, msg, want_flags);
5152 }
5153
5154 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005155 /* Always enter in the body analyzer */
5156 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5157 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005158
Willy Tarreau58975672014-04-24 21:13:57 +02005159 /* if the user wants to log as soon as possible, without counting
5160 * bytes from the server, then this is the right moment. We have
5161 * to temporarily assign bytes_out to log what we currently have.
5162 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005163 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005164 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5165 s->logs.bytes_out = txn->rsp.eoh;
5166 s->do_log(s);
5167 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005168 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005169 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005170}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005171
Willy Tarreaud98cf932009-12-27 22:54:55 +01005172/* This function is an analyser which forwards response body (including chunk
5173 * sizes if any). It is called as soon as we must forward, even if we forward
5174 * zero byte. The only situation where it must not be called is when we're in
5175 * tunnel mode and we want to forward till the close. It's used both to forward
5176 * remaining data and to resync after end of body. It expects the msg_state to
5177 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005178 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005179 *
5180 * It is capable of compressing response data both in content-length mode and
5181 * in chunked mode. The state machines follows different flows depending on
5182 * whether content-length and chunked modes are used, since there are no
5183 * trailers in content-length :
5184 *
5185 * chk-mode cl-mode
5186 * ,----- BODY -----.
5187 * / \
5188 * V size > 0 V chk-mode
5189 * .--> SIZE -------------> DATA -------------> CRLF
5190 * | | size == 0 | last byte |
5191 * | v final crlf v inspected |
5192 * | TRAILERS -----------> DONE |
5193 * | |
5194 * `----------------------------------------------'
5195 *
5196 * Compression only happens in the DATA state, and must be flushed in final
5197 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5198 * is performed at once on final states for all bytes parsed, or when leaving
5199 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005200 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005201int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005202{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005203 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005204 struct http_txn *txn = s->txn;
5205 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005206 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005207
Christopher Faulete0768eb2018-10-03 16:38:02 +02005208 if (IS_HTX_STRM(s))
5209 return htx_response_forward_body(s, res, an_bit);
5210
Christopher Faulet45073512018-07-20 10:16:29 +02005211 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 +02005212 now_ms, __FUNCTION__,
5213 s,
5214 res,
5215 res->rex, res->wex,
5216 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005217 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005218 res->analysers);
5219
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005220 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5221 return 0;
5222
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005223 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005224 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005225 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005226 /* Output closed while we were sending data. We must abort and
5227 * wake the other side up.
5228 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005229 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005230 msg->msg_state = HTTP_MSG_ERROR;
5231 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005232 return 1;
5233 }
5234
Willy Tarreau4fe41902010-06-07 22:27:41 +02005235 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005236 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005237
Christopher Fauletd7c91962015-04-30 11:48:27 +02005238 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005239 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5240 ? HTTP_MSG_CHUNK_SIZE
5241 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005242 }
5243
Willy Tarreauefdf0942014-04-24 20:08:57 +02005244 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005245 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005246 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005247 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005248 }
5249
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005250 if (msg->msg_state < HTTP_MSG_DONE) {
5251 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5252 ? http_msg_forward_chunked_body(s, msg)
5253 : http_msg_forward_body(s, msg));
5254 if (!ret)
5255 goto missing_data_or_waiting;
5256 if (ret < 0)
5257 goto return_bad_res;
5258 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005259
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005260 /* other states, DONE...TUNNEL */
5261 /* for keep-alive we don't want to forward closes on DONE */
5262 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5263 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5264 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005265
Christopher Faulet894da4c2017-07-18 11:29:07 +02005266 http_resync_states(s);
5267 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005268 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5269 if (res->flags & CF_SHUTW) {
5270 /* response errors are most likely due to the
5271 * client aborting the transfer. */
5272 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005273 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005274 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005275 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005276 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005277 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005278 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005279 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005280 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005281
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005282 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005283 if (res->flags & CF_SHUTW)
5284 goto aborted_xfer;
5285
5286 /* stop waiting for data if the input is closed before the end. If the
5287 * client side was already closed, it means that the client has aborted,
5288 * so we don't want to count this as a server abort. Otherwise it's a
5289 * server abort.
5290 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005291 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005292 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005293 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005294 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005295 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005296 if (!(s->flags & SF_ERR_MASK))
5297 s->flags |= SF_ERR_SRVCL;
Olivier Houcharda798bf52019-03-08 18:52:00 +01005298 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005299 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01005300 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005301 goto return_bad_res_stats_ok;
5302 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005303 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005304
Willy Tarreau40dba092010-03-04 18:14:51 +01005305 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005306 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005307 goto return_bad_res;
5308
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005309 /* When TE: chunked is used, we need to get there again to parse
5310 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005311 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5312 * or if there are filters registered on the stream, we don't want to
5313 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005314 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005315 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005316 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005317 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5318 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005319 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005320
Willy Tarreau5c620922011-05-11 19:56:11 +02005321 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005322 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005323 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005324 * modes are already handled by the stream sock layer. We must not do
5325 * this in content-length mode because it could present the MSG_MORE
5326 * flag with the last block of forwarded data, which would cause an
5327 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005328 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005329 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005330 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005331
Willy Tarreau87b09662015-04-03 00:22:06 +02005332 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005333 return 0;
5334
Willy Tarreau40dba092010-03-04 18:14:51 +01005335 return_bad_res: /* let's centralize all bad responses */
Olivier Houcharda798bf52019-03-08 18:52:00 +01005336 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005337 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01005338 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005339
5340 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005341 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005342 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005343 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005344 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005345 res->analysers &= AN_RES_FLT_END;
5346 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 +01005347 if (objt_server(s->target))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01005348 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005349
Willy Tarreaue7dff022015-04-03 01:14:29 +02005350 if (!(s->flags & SF_ERR_MASK))
5351 s->flags |= SF_ERR_PRXCOND;
5352 if (!(s->flags & SF_FINST_MASK))
5353 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005354 return 0;
5355
5356 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005357 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005358 txn->rsp.msg_state = HTTP_MSG_ERROR;
5359 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005360 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005361 res->analysers &= AN_RES_FLT_END;
5362 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 +01005363
Olivier Houcharda798bf52019-03-08 18:52:00 +01005364 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5365 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005366 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01005367 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005368
Willy Tarreaue7dff022015-04-03 01:14:29 +02005369 if (!(s->flags & SF_ERR_MASK))
5370 s->flags |= SF_ERR_CLICL;
5371 if (!(s->flags & SF_FINST_MASK))
5372 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005373 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005374}
5375
5376
Christopher Faulet10079f52018-10-03 15:17:28 +02005377int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005378{
5379 struct channel *chn = msg->chn;
5380 int ret;
5381
5382 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5383
5384 if (msg->msg_state == HTTP_MSG_ENDING)
5385 goto ending;
5386
5387 /* Neither content-length, nor transfer-encoding was found, so we must
5388 * read the body until the server connection is closed. In that case, we
5389 * eat data as they come. Of course, this happens for response only. */
5390 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005391 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005392 msg->chunk_len += len;
5393 msg->body_len += len;
5394 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005395 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005396 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005397 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005398 msg->next += ret;
5399 msg->chunk_len -= ret;
5400 if (msg->chunk_len) {
5401 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005402 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005403 chn->flags |= CF_WAKE_WRITE;
5404 goto missing_data_or_waiting;
5405 }
5406
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005407 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5408 * always set for a request. */
5409 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5410 /* The server still sending data that should be filtered */
5411 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5412 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005413 msg->msg_state = HTTP_MSG_TUNNEL;
5414 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005415 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005416
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005417 msg->msg_state = HTTP_MSG_ENDING;
5418
5419 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005420 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005421 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005422 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5423 /* default_ret */ msg->next,
5424 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005425 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005426 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005427 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5428 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005429 if (msg->next)
5430 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005431
Christopher Fauletda02e172015-12-04 09:25:05 +01005432 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5433 /* default_ret */ 1,
5434 /* on_error */ goto error,
5435 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005436 if (msg->msg_state == HTTP_MSG_ENDING)
5437 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005438 return 1;
5439
5440 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005441 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005442 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5443 /* default_ret */ msg->next,
5444 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005445 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005446 msg->next -= ret;
5447 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5448 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005449 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005450 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005451 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005452 return 0;
5453 error:
5454 return -1;
5455}
5456
Christopher Faulet10079f52018-10-03 15:17:28 +02005457int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005458{
5459 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005460 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005461 int ret;
5462
5463 /* Here we have the guarantee to be in one of the following state:
5464 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5465 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5466
Christopher Fauletca874b82018-09-20 11:31:01 +02005467 if (msg->msg_state == HTTP_MSG_ENDING)
5468 goto ending;
5469
5470 /* Don't parse chunks if there is no input data */
5471 if (!ci_data(chn))
5472 goto waiting;
5473
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005474 switch_states:
5475 switch (msg->msg_state) {
5476 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005477 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005478 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005479 /* on_error */ goto error);
5480 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005481 msg->chunk_len -= ret;
5482 if (msg->chunk_len) {
5483 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005484 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005485 chn->flags |= CF_WAKE_WRITE;
5486 goto missing_data_or_waiting;
5487 }
5488
5489 /* nothing left to forward for this chunk*/
5490 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5491 /* fall through for HTTP_MSG_CHUNK_CRLF */
5492
5493 case HTTP_MSG_CHUNK_CRLF:
5494 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005495 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005496 if (ret == 0)
5497 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005498 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005499 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005500 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005501 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005502 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005503 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005504 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005505 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5506 /* fall through for HTTP_MSG_CHUNK_SIZE */
5507
5508 case HTTP_MSG_CHUNK_SIZE:
5509 /* read the chunk size and assign it to ->chunk_len,
5510 * then set ->next to point to the body and switch to
5511 * DATA or TRAILERS state.
5512 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005513 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005514 if (ret == 0)
5515 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005516 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005517 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005518 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005519 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005520 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005521 }
5522
5523 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005524 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005525 msg->chunk_len = chunk;
5526 msg->body_len += chunk;
5527
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005528 if (msg->chunk_len) {
5529 msg->msg_state = HTTP_MSG_DATA;
5530 goto switch_states;
5531 }
5532 msg->msg_state = HTTP_MSG_TRAILERS;
5533 /* fall through for HTTP_MSG_TRAILERS */
5534
5535 case HTTP_MSG_TRAILERS:
5536 ret = http_forward_trailers(msg);
5537 if (ret < 0)
5538 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005539 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5540 /* default_ret */ 1,
5541 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005542 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005543 if (!ret)
5544 goto missing_data_or_waiting;
5545 break;
5546
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005547 default:
5548 /* This should no happen in this function */
5549 goto error;
5550 }
5551
5552 msg->msg_state = HTTP_MSG_ENDING;
5553 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005554 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005555 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005556 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005557 /* default_ret */ msg->next,
5558 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005559 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005560 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005561 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5562 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005563 if (msg->next)
5564 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005565
Christopher Fauletda02e172015-12-04 09:25:05 +01005566 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005567 /* default_ret */ 1,
5568 /* on_error */ goto error,
5569 /* on_wait */ goto waiting);
5570 msg->msg_state = HTTP_MSG_DONE;
5571 return 1;
5572
5573 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005574 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005575 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005576 /* default_ret */ msg->next,
5577 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005578 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005579 msg->next -= ret;
5580 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5581 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005582 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005583 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005584 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005585 return 0;
5586
5587 chunk_parsing_error:
5588 if (msg->err_pos >= 0) {
5589 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005590 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005591 msg->msg_state, strm_fe(s));
5592 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005593 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005594 msg, msg->msg_state, s->be);
5595 }
5596 error:
5597 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005598}
5599
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005600
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005601/* Iterate the same filter through all request headers.
5602 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005603 * Since it can manage the switch to another backend, it updates the per-proxy
5604 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005605 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005606int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005607{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005608 char *cur_ptr, *cur_end, *cur_next;
5609 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005610 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005611 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005612 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005613
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005614 last_hdr = 0;
5615
Willy Tarreauf37954d2018-06-15 18:31:02 +02005616 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005617 old_idx = 0;
5618
5619 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005620 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005621 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005622 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005623 (exp->action == ACT_ALLOW ||
5624 exp->action == ACT_DENY ||
5625 exp->action == ACT_TARPIT))
5626 return 0;
5627
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005628 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005629 if (!cur_idx)
5630 break;
5631
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005632 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005633 cur_ptr = cur_next;
5634 cur_end = cur_ptr + cur_hdr->len;
5635 cur_next = cur_end + cur_hdr->cr + 1;
5636
5637 /* Now we have one header between cur_ptr and cur_end,
5638 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005639 */
5640
Willy Tarreau15a53a42015-01-21 13:39:42 +01005641 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005642 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005643 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005644 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005645 last_hdr = 1;
5646 break;
5647
5648 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005649 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005650 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005651 break;
5652
5653 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005654 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005655 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005656 break;
5657
5658 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005659 len = exp_replace(trash.area,
5660 trash.size, cur_ptr,
5661 exp->replace, pmatch);
5662 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005663 return -1;
5664
Willy Tarreau6e27be12018-08-22 04:46:47 +02005665 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5666
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005667 /* FIXME: if the user adds a newline in the replacement, the
5668 * index will not be recalculated for now, and the new line
5669 * will not be counted as a new header.
5670 */
5671
5672 cur_end += delta;
5673 cur_next += delta;
5674 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005675 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005676 break;
5677
5678 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005679 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005680 cur_next += delta;
5681
Willy Tarreaufa355d42009-11-29 18:12:29 +01005682 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005683 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5684 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005685 cur_hdr->len = 0;
5686 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005687 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005688 break;
5689
5690 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005691 }
5692
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005693 /* keep the link from this header to next one in case of later
5694 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005695 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005696 old_idx = cur_idx;
5697 }
5698 return 0;
5699}
5700
5701
5702/* Apply the filter to the request line.
5703 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5704 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005705 * Since it can manage the switch to another backend, it updates the per-proxy
5706 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005707 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005708int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005709{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005710 char *cur_ptr, *cur_end;
5711 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005712 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005713 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005714
Willy Tarreau3d300592007-03-18 18:34:41 +01005715 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005716 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005717 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005718 (exp->action == ACT_ALLOW ||
5719 exp->action == ACT_DENY ||
5720 exp->action == ACT_TARPIT))
5721 return 0;
5722 else if (exp->action == ACT_REMOVE)
5723 return 0;
5724
5725 done = 0;
5726
Willy Tarreauf37954d2018-06-15 18:31:02 +02005727 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005728 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005729
5730 /* Now we have the request line between cur_ptr and cur_end */
5731
Willy Tarreau15a53a42015-01-21 13:39:42 +01005732 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005733 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005734 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005735 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005736 done = 1;
5737 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005738
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005739 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005740 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005741 done = 1;
5742 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005743
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005744 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005745 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005746 done = 1;
5747 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005748
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005749 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005750 len = exp_replace(trash.area, trash.size,
5751 cur_ptr, exp->replace, pmatch);
5752 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005753 return -1;
5754
Willy Tarreau6e27be12018-08-22 04:46:47 +02005755 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5756
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005757 /* FIXME: if the user adds a newline in the replacement, the
5758 * index will not be recalculated for now, and the new line
5759 * will not be counted as a new header.
5760 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005761
Willy Tarreaufa355d42009-11-29 18:12:29 +01005762 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005763 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005764 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005765 HTTP_MSG_RQMETH,
5766 cur_ptr, cur_end + 1,
5767 NULL, NULL);
5768 if (unlikely(!cur_end))
5769 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005770
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005771 /* we have a full request and we know that we have either a CR
5772 * or an LF at <ptr>.
5773 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005774 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5775 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005776 /* there is no point trying this regex on headers */
5777 return 1;
5778 }
5779 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005780 return done;
5781}
Willy Tarreau97de6242006-12-27 17:18:38 +01005782
Willy Tarreau58f10d72006-12-04 02:26:12 +01005783
Willy Tarreau58f10d72006-12-04 02:26:12 +01005784
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005785/*
Willy Tarreau87b09662015-04-03 00:22:06 +02005786 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005787 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005788 * unparsable request. Since it can manage the switch to another backend, it
5789 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005790 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005791int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005792{
Willy Tarreau192252e2015-04-04 01:47:55 +02005793 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005794 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005795 struct hdr_exp *exp;
5796
5797 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005798 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005799
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005800 /*
5801 * The interleaving of transformations and verdicts
5802 * makes it difficult to decide to continue or stop
5803 * the evaluation.
5804 */
5805
Willy Tarreau6c123b12010-01-28 20:22:06 +01005806 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5807 break;
5808
Willy Tarreau3d300592007-03-18 18:34:41 +01005809 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005810 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005811 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005812 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005813
5814 /* if this filter had a condition, evaluate it now and skip to
5815 * next filter if the condition does not match.
5816 */
5817 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005818 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01005819 ret = acl_pass(ret);
5820 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5821 ret = !ret;
5822
5823 if (!ret)
5824 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005825 }
5826
5827 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005828 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005829 if (unlikely(ret < 0))
5830 return -1;
5831
5832 if (likely(ret == 0)) {
5833 /* The filter did not match the request, it can be
5834 * iterated through all headers.
5835 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01005836 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
5837 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005838 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005839 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005840 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005841}
5842
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005843
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005844/* Delete a value in a header between delimiters <from> and <next> in buffer
5845 * <buf>. The number of characters displaced is returned, and the pointer to
5846 * the first delimiter is updated if required. The function tries as much as
5847 * possible to respect the following principles :
5848 * - replace <from> delimiter by the <next> one unless <from> points to a
5849 * colon, in which case <next> is simply removed
5850 * - set exactly one space character after the new first delimiter, unless
5851 * there are not enough characters in the block being moved to do so.
5852 * - remove unneeded spaces before the previous delimiter and after the new
5853 * one.
5854 *
5855 * It is the caller's responsibility to ensure that :
5856 * - <from> points to a valid delimiter or the colon ;
5857 * - <next> points to a valid delimiter or the final CR/LF ;
5858 * - there are non-space chars before <from> ;
5859 * - there is a CR/LF at or after <next>.
5860 */
Willy Tarreauc5a4fd52018-12-11 11:42:27 +01005861static int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005862{
5863 char *prev = *from;
5864
5865 if (*prev == ':') {
5866 /* We're removing the first value, preserve the colon and add a
5867 * space if possible.
5868 */
Willy Tarreau2235b262016-11-05 15:50:20 +01005869 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005870 next++;
5871 prev++;
5872 if (prev < next)
5873 *prev++ = ' ';
5874
Willy Tarreau2235b262016-11-05 15:50:20 +01005875 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005876 next++;
5877 } else {
5878 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01005879 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005880 prev--;
5881 *from = prev;
5882
5883 /* copy the delimiter and if possible a space if we're
5884 * not at the end of the line.
5885 */
Willy Tarreau2235b262016-11-05 15:50:20 +01005886 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005887 *prev++ = *next++;
5888 if (prev + 1 < next)
5889 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01005890 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005891 next++;
5892 }
5893 }
Willy Tarreaue3128022018-07-12 15:55:34 +02005894 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005895}
5896
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005897/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005898 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005899 * desirable to call it only when needed. This code is quite complex because
5900 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5901 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005902 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005903void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005904{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005905 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005906 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005907 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005908 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005909 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5910 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005911
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005912 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005913 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005914 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005915
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005916 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005917 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005918 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005919
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005920 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005921 hdr_beg = hdr_next;
5922 hdr_end = hdr_beg + cur_hdr->len;
5923 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005924
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005925 /* We have one full header between hdr_beg and hdr_end, and the
5926 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005927 * "Cookie:" headers.
5928 */
5929
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005930 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005931 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005932 old_idx = cur_idx;
5933 continue;
5934 }
5935
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005936 del_from = NULL; /* nothing to be deleted */
5937 preserve_hdr = 0; /* assume we may kill the whole header */
5938
Willy Tarreau58f10d72006-12-04 02:26:12 +01005939 /* Now look for cookies. Conforming to RFC2109, we have to support
5940 * attributes whose name begin with a '$', and associate them with
5941 * the right cookie, if we want to delete this cookie.
5942 * So there are 3 cases for each cookie read :
5943 * 1) it's a special attribute, beginning with a '$' : ignore it.
5944 * 2) it's a server id cookie that we *MAY* want to delete : save
5945 * some pointers on it (last semi-colon, beginning of cookie...)
5946 * 3) it's an application cookie : we *MAY* have to delete a previous
5947 * "special" cookie.
5948 * At the end of loop, if a "special" cookie remains, we may have to
5949 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005950 * *MUST* delete it.
5951 *
5952 * Note: RFC2965 is unclear about the processing of spaces around
5953 * the equal sign in the ATTR=VALUE form. A careful inspection of
5954 * the RFC explicitly allows spaces before it, and not within the
5955 * tokens (attrs or values). An inspection of RFC2109 allows that
5956 * too but section 10.1.3 lets one think that spaces may be allowed
5957 * after the equal sign too, resulting in some (rare) buggy
5958 * implementations trying to do that. So let's do what servers do.
5959 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5960 * allowed quoted strings in values, with any possible character
5961 * after a backslash, including control chars and delimitors, which
5962 * causes parsing to become ambiguous. Browsers also allow spaces
5963 * within values even without quotes.
5964 *
5965 * We have to keep multiple pointers in order to support cookie
5966 * removal at the beginning, middle or end of header without
5967 * corrupting the header. All of these headers are valid :
5968 *
5969 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5970 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5971 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5972 * | | | | | | | | |
5973 * | | | | | | | | hdr_end <--+
5974 * | | | | | | | +--> next
5975 * | | | | | | +----> val_end
5976 * | | | | | +-----------> val_beg
5977 * | | | | +--------------> equal
5978 * | | | +----------------> att_end
5979 * | | +---------------------> att_beg
5980 * | +--------------------------> prev
5981 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005982 */
5983
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005984 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
5985 /* Iterate through all cookies on this line */
5986
5987 /* find att_beg */
5988 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01005989 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005990 att_beg++;
5991
5992 /* find att_end : this is the first character after the last non
5993 * space before the equal. It may be equal to hdr_end.
5994 */
5995 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005996
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005997 while (equal < hdr_end) {
5998 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01005999 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006000 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006001 continue;
6002 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006003 }
6004
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006005 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6006 * is between <att_beg> and <equal>, both may be identical.
6007 */
6008
6009 /* look for end of cookie if there is an equal sign */
6010 if (equal < hdr_end && *equal == '=') {
6011 /* look for the beginning of the value */
6012 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006013 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006014 val_beg++;
6015
6016 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006017 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006018
6019 /* make val_end point to the first white space or delimitor after the value */
6020 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006021 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006022 val_end--;
6023 } else {
6024 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006025 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006026
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006027 /* We have nothing to do with attributes beginning with '$'. However,
6028 * they will automatically be removed if a header before them is removed,
6029 * since they're supposed to be linked together.
6030 */
6031 if (*att_beg == '$')
6032 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006033
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006034 /* Ignore cookies with no equal sign */
6035 if (equal == next) {
6036 /* This is not our cookie, so we must preserve it. But if we already
6037 * scheduled another cookie for removal, we cannot remove the
6038 * complete header, but we can remove the previous block itself.
6039 */
6040 preserve_hdr = 1;
6041 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006042 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006043 val_end += delta;
6044 next += delta;
6045 hdr_end += delta;
6046 hdr_next += delta;
6047 cur_hdr->len += delta;
6048 http_msg_move_end(&txn->req, delta);
6049 prev = del_from;
6050 del_from = NULL;
6051 }
6052 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006053 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006054
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006055 /* if there are spaces around the equal sign, we need to
6056 * strip them otherwise we'll get trouble for cookie captures,
6057 * or even for rewrites. Since this happens extremely rarely,
6058 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006059 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006060 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6061 int stripped_before = 0;
6062 int stripped_after = 0;
6063
6064 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006065 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006066 equal += stripped_before;
6067 val_beg += stripped_before;
6068 }
6069
6070 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006071 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006072 val_beg += stripped_after;
6073 stripped_before += stripped_after;
6074 }
6075
6076 val_end += stripped_before;
6077 next += stripped_before;
6078 hdr_end += stripped_before;
6079 hdr_next += stripped_before;
6080 cur_hdr->len += stripped_before;
6081 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006082 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006083 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006084
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006085 /* First, let's see if we want to capture this cookie. We check
6086 * that we don't already have a client side cookie, because we
6087 * can only capture one. Also as an optimisation, we ignore
6088 * cookies shorter than the declared name.
6089 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006090 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6091 (val_end - att_beg >= sess->fe->capture_namelen) &&
6092 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006093 int log_len = val_end - att_beg;
6094
Willy Tarreaubafbe012017-11-24 17:34:44 +01006095 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006096 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006097 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006098 if (log_len > sess->fe->capture_len)
6099 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006100 memcpy(txn->cli_cookie, att_beg, log_len);
6101 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006102 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006103 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006104
Willy Tarreaubca99692010-10-06 19:25:55 +02006105 /* Persistence cookies in passive, rewrite or insert mode have the
6106 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006107 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006108 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006109 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006110 * For cookies in prefix mode, the form is :
6111 *
6112 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006113 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006114 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6115 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6116 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006117 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006118
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006119 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6120 * have the server ID between val_beg and delim, and the original cookie between
6121 * delim+1 and val_end. Otherwise, delim==val_end :
6122 *
6123 * Cookie: NAME=SRV; # in all but prefix modes
6124 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6125 * | || || | |+-> next
6126 * | || || | +--> val_end
6127 * | || || +---------> delim
6128 * | || |+------------> val_beg
6129 * | || +-------------> att_end = equal
6130 * | |+-----------------> att_beg
6131 * | +------------------> prev
6132 * +-------------------------> hdr_beg
6133 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006134
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006135 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006136 for (delim = val_beg; delim < val_end; delim++)
6137 if (*delim == COOKIE_DELIM)
6138 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006139 } else {
6140 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006141 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006142 /* Now check if the cookie contains a date field, which would
6143 * appear after a vertical bar ('|') just after the server name
6144 * and before the delimiter.
6145 */
6146 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6147 if (vbar1) {
6148 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006149 * right is the last seen date. It is a base64 encoded
6150 * 30-bit value representing the UNIX date since the
6151 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006152 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006153 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006154 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006155 if (val_end - vbar1 >= 5) {
6156 val = b64tos30(vbar1);
6157 if (val > 0)
6158 txn->cookie_last_date = val << 2;
6159 }
6160 /* look for a second vertical bar */
6161 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6162 if (vbar1 && (val_end - vbar1 > 5)) {
6163 val = b64tos30(vbar1 + 1);
6164 if (val > 0)
6165 txn->cookie_first_date = val << 2;
6166 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006167 }
6168 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006169
Willy Tarreauf64d1412010-10-07 20:06:11 +02006170 /* if the cookie has an expiration date and the proxy wants to check
6171 * it, then we do that now. We first check if the cookie is too old,
6172 * then only if it has expired. We detect strict overflow because the
6173 * time resolution here is not great (4 seconds). Cookies with dates
6174 * in the future are ignored if their offset is beyond one day. This
6175 * allows an admin to fix timezone issues without expiring everyone
6176 * and at the same time avoids keeping unwanted side effects for too
6177 * long.
6178 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006179 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6180 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006181 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006182 txn->flags &= ~TX_CK_MASK;
6183 txn->flags |= TX_CK_OLD;
6184 delim = val_beg; // let's pretend we have not found the cookie
6185 txn->cookie_first_date = 0;
6186 txn->cookie_last_date = 0;
6187 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006188 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6189 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006190 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006191 txn->flags &= ~TX_CK_MASK;
6192 txn->flags |= TX_CK_EXPIRED;
6193 delim = val_beg; // let's pretend we have not found the cookie
6194 txn->cookie_first_date = 0;
6195 txn->cookie_last_date = 0;
6196 }
6197
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006198 /* Here, we'll look for the first running server which supports the cookie.
6199 * This allows to share a same cookie between several servers, for example
6200 * to dedicate backup servers to specific servers only.
6201 * However, to prevent clients from sticking to cookie-less backup server
6202 * when they have incidentely learned an empty cookie, we simply ignore
6203 * empty cookies and mark them as invalid.
6204 * The same behaviour is applied when persistence must be ignored.
6205 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006206 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006207 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006208
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006209 while (srv) {
6210 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6211 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006212 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006213 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006214 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006215 /* we found the server and we can use it */
6216 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006217 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006218 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006219 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006220 break;
6221 } else {
6222 /* we found a server, but it's down,
6223 * mark it as such and go on in case
6224 * another one is available.
6225 */
6226 txn->flags &= ~TX_CK_MASK;
6227 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006228 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006229 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006230 srv = srv->next;
6231 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006232
Willy Tarreauf64d1412010-10-07 20:06:11 +02006233 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006234 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006235 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006236 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006237 txn->flags |= TX_CK_UNUSED;
6238 else
6239 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006240 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006241
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006242 /* depending on the cookie mode, we may have to either :
6243 * - delete the complete cookie if we're in insert+indirect mode, so that
6244 * the server never sees it ;
6245 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlant5ba80252018-11-15 09:25:36 -08006246 * application cookie so that it does not get accidently removed later,
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006247 * if we're in cookie prefix mode
6248 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006249 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006250 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006251
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006252 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006253 val_end += delta;
6254 next += delta;
6255 hdr_end += delta;
6256 hdr_next += delta;
6257 cur_hdr->len += delta;
6258 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006259
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006260 del_from = NULL;
6261 preserve_hdr = 1; /* we want to keep this cookie */
6262 }
6263 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006264 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006265 del_from = prev;
6266 }
6267 } else {
6268 /* This is not our cookie, so we must preserve it. But if we already
6269 * scheduled another cookie for removal, we cannot remove the
6270 * complete header, but we can remove the previous block itself.
6271 */
6272 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006273
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006274 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006275 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006276 if (att_beg >= del_from)
6277 att_beg += delta;
6278 if (att_end >= del_from)
6279 att_end += delta;
6280 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006281 val_end += delta;
6282 next += delta;
6283 hdr_end += delta;
6284 hdr_next += delta;
6285 cur_hdr->len += delta;
6286 http_msg_move_end(&txn->req, delta);
6287 prev = del_from;
6288 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006289 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006290 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006291
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006292 /* continue with next cookie on this header line */
6293 att_beg = next;
6294 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006295
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006296 /* There are no more cookies on this line.
6297 * We may still have one (or several) marked for deletion at the
6298 * end of the line. We must do this now in two ways :
6299 * - if some cookies must be preserved, we only delete from the
6300 * mark to the end of line ;
6301 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006302 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006303 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006304 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006305 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006306 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006307 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006308 cur_hdr->len += delta;
6309 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006310 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006311
6312 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006313 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6314 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006315 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006316 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006317 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006318 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006319 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006320 }
6321
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006322 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006323 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006324 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006325}
6326
6327
Willy Tarreaua15645d2007-03-18 16:22:39 +01006328/* Iterate the same filter through all response headers contained in <rtr>.
6329 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6330 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006331int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006332{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006333 char *cur_ptr, *cur_end, *cur_next;
6334 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006335 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006336 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006337 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006338
6339 last_hdr = 0;
6340
Willy Tarreauf37954d2018-06-15 18:31:02 +02006341 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006342 old_idx = 0;
6343
6344 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006345 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006346 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006347 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006348 (exp->action == ACT_ALLOW ||
6349 exp->action == ACT_DENY))
6350 return 0;
6351
6352 cur_idx = txn->hdr_idx.v[old_idx].next;
6353 if (!cur_idx)
6354 break;
6355
6356 cur_hdr = &txn->hdr_idx.v[cur_idx];
6357 cur_ptr = cur_next;
6358 cur_end = cur_ptr + cur_hdr->len;
6359 cur_next = cur_end + cur_hdr->cr + 1;
6360
6361 /* Now we have one header between cur_ptr and cur_end,
6362 * and the next header starts at cur_next.
6363 */
6364
Willy Tarreau15a53a42015-01-21 13:39:42 +01006365 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006366 switch (exp->action) {
6367 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006368 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006369 last_hdr = 1;
6370 break;
6371
6372 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006373 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006374 last_hdr = 1;
6375 break;
6376
6377 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006378 len = exp_replace(trash.area,
6379 trash.size, cur_ptr,
6380 exp->replace, pmatch);
6381 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006382 return -1;
6383
Willy Tarreau6e27be12018-08-22 04:46:47 +02006384 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6385
Willy Tarreaua15645d2007-03-18 16:22:39 +01006386 /* FIXME: if the user adds a newline in the replacement, the
6387 * index will not be recalculated for now, and the new line
6388 * will not be counted as a new header.
6389 */
6390
6391 cur_end += delta;
6392 cur_next += delta;
6393 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006394 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006395 break;
6396
6397 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006398 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006399 cur_next += delta;
6400
Willy Tarreaufa355d42009-11-29 18:12:29 +01006401 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006402 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6403 txn->hdr_idx.used--;
6404 cur_hdr->len = 0;
6405 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006406 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006407 break;
6408
6409 }
6410 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006411
6412 /* keep the link from this header to next one in case of later
6413 * removal of next header.
6414 */
6415 old_idx = cur_idx;
6416 }
6417 return 0;
6418}
6419
6420
6421/* Apply the filter to the status line in the response buffer <rtr>.
6422 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6423 * or -1 if a replacement resulted in an invalid status line.
6424 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006425int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006426{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006427 char *cur_ptr, *cur_end;
6428 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006429 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006430 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006431
Willy Tarreau3d300592007-03-18 18:34:41 +01006432 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006433 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006434 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006435 (exp->action == ACT_ALLOW ||
6436 exp->action == ACT_DENY))
6437 return 0;
6438 else if (exp->action == ACT_REMOVE)
6439 return 0;
6440
6441 done = 0;
6442
Willy Tarreauf37954d2018-06-15 18:31:02 +02006443 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006444 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006445
6446 /* Now we have the status line between cur_ptr and cur_end */
6447
Willy Tarreau15a53a42015-01-21 13:39:42 +01006448 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006449 switch (exp->action) {
6450 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006451 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006452 done = 1;
6453 break;
6454
6455 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006456 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006457 done = 1;
6458 break;
6459
6460 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006461 len = exp_replace(trash.area, trash.size,
6462 cur_ptr, exp->replace, pmatch);
6463 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006464 return -1;
6465
Willy Tarreau6e27be12018-08-22 04:46:47 +02006466 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6467
Willy Tarreaua15645d2007-03-18 16:22:39 +01006468 /* FIXME: if the user adds a newline in the replacement, the
6469 * index will not be recalculated for now, and the new line
6470 * will not be counted as a new header.
6471 */
6472
Willy Tarreaufa355d42009-11-29 18:12:29 +01006473 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006474 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006475 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006476 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006477 cur_ptr, cur_end + 1,
6478 NULL, NULL);
6479 if (unlikely(!cur_end))
6480 return -1;
6481
6482 /* we have a full respnse and we know that we have either a CR
6483 * or an LF at <ptr>.
6484 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006485 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006486 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006487 /* there is no point trying this regex on headers */
6488 return 1;
6489 }
6490 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006491 return done;
6492}
6493
6494
6495
6496/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006497 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006498 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6499 * unparsable response.
6500 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006501int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006502{
Willy Tarreau192252e2015-04-04 01:47:55 +02006503 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006504 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006505 struct hdr_exp *exp;
6506
6507 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006508 int ret;
6509
6510 /*
6511 * The interleaving of transformations and verdicts
6512 * makes it difficult to decide to continue or stop
6513 * the evaluation.
6514 */
6515
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006516 if (txn->flags & TX_SVDENY)
6517 break;
6518
Willy Tarreau3d300592007-03-18 18:34:41 +01006519 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006520 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6521 exp->action == ACT_PASS)) {
6522 exp = exp->next;
6523 continue;
6524 }
6525
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006526 /* if this filter had a condition, evaluate it now and skip to
6527 * next filter if the condition does not match.
6528 */
6529 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006530 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006531 ret = acl_pass(ret);
6532 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6533 ret = !ret;
6534 if (!ret)
6535 continue;
6536 }
6537
Willy Tarreaua15645d2007-03-18 16:22:39 +01006538 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006539 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006540 if (unlikely(ret < 0))
6541 return -1;
6542
6543 if (likely(ret == 0)) {
6544 /* The filter did not match the response, it can be
6545 * iterated through all headers.
6546 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006547 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6548 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006549 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006550 }
6551 return 0;
6552}
6553
6554
Willy Tarreaua15645d2007-03-18 16:22:39 +01006555/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006556 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006557 * desirable to call it only when needed. This function is also used when we
6558 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006559 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006560void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006561{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006562 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006563 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006564 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006565 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006566 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006567 char *hdr_beg, *hdr_end, *hdr_next;
6568 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006569
Willy Tarreaua15645d2007-03-18 16:22:39 +01006570 /* Iterate through the headers.
6571 * we start with the start line.
6572 */
6573 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006574 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006575
6576 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6577 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006578 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006579
6580 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006581 hdr_beg = hdr_next;
6582 hdr_end = hdr_beg + cur_hdr->len;
6583 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006584
Willy Tarreau24581ba2010-08-31 22:39:35 +02006585 /* We have one full header between hdr_beg and hdr_end, and the
6586 * next header starts at hdr_next. We're only interested in
6587 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006588 */
6589
Willy Tarreau24581ba2010-08-31 22:39:35 +02006590 is_cookie2 = 0;
6591 prev = hdr_beg + 10;
6592 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006593 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006594 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6595 if (!val) {
6596 old_idx = cur_idx;
6597 continue;
6598 }
6599 is_cookie2 = 1;
6600 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006601 }
6602
Willy Tarreau24581ba2010-08-31 22:39:35 +02006603 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6604 * <prev> points to the colon.
6605 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006606 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006607
Willy Tarreau24581ba2010-08-31 22:39:35 +02006608 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6609 * check-cache is enabled) and we are not interested in checking
6610 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006611 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006612 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006613 return;
6614
Willy Tarreau24581ba2010-08-31 22:39:35 +02006615 /* OK so now we know we have to process this response cookie.
6616 * The format of the Set-Cookie header is slightly different
6617 * from the format of the Cookie header in that it does not
6618 * support the comma as a cookie delimiter (thus the header
6619 * cannot be folded) because the Expires attribute described in
6620 * the original Netscape's spec may contain an unquoted date
6621 * with a comma inside. We have to live with this because
6622 * many browsers don't support Max-Age and some browsers don't
6623 * support quoted strings. However the Set-Cookie2 header is
6624 * clean.
6625 *
6626 * We have to keep multiple pointers in order to support cookie
6627 * removal at the beginning, middle or end of header without
6628 * corrupting the header (in case of set-cookie2). A special
6629 * pointer, <scav> points to the beginning of the set-cookie-av
6630 * fields after the first semi-colon. The <next> pointer points
6631 * either to the end of line (set-cookie) or next unquoted comma
6632 * (set-cookie2). All of these headers are valid :
6633 *
6634 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6635 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6636 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6637 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6638 * | | | | | | | | | |
6639 * | | | | | | | | +-> next hdr_end <--+
6640 * | | | | | | | +------------> scav
6641 * | | | | | | +--------------> val_end
6642 * | | | | | +--------------------> val_beg
6643 * | | | | +----------------------> equal
6644 * | | | +------------------------> att_end
6645 * | | +----------------------------> att_beg
6646 * | +------------------------------> prev
6647 * +-----------------------------------------> hdr_beg
6648 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006649
Willy Tarreau24581ba2010-08-31 22:39:35 +02006650 for (; prev < hdr_end; prev = next) {
6651 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006652
Willy Tarreau24581ba2010-08-31 22:39:35 +02006653 /* find att_beg */
6654 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006655 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006656 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006657
Willy Tarreau24581ba2010-08-31 22:39:35 +02006658 /* find att_end : this is the first character after the last non
6659 * space before the equal. It may be equal to hdr_end.
6660 */
6661 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006662
Willy Tarreau24581ba2010-08-31 22:39:35 +02006663 while (equal < hdr_end) {
6664 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6665 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006666 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006667 continue;
6668 att_end = equal;
6669 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006670
Willy Tarreau24581ba2010-08-31 22:39:35 +02006671 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6672 * is between <att_beg> and <equal>, both may be identical.
6673 */
6674
6675 /* look for end of cookie if there is an equal sign */
6676 if (equal < hdr_end && *equal == '=') {
6677 /* look for the beginning of the value */
6678 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006679 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006680 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006681
Willy Tarreau24581ba2010-08-31 22:39:35 +02006682 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006683 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006684
6685 /* make val_end point to the first white space or delimitor after the value */
6686 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006687 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006688 val_end--;
6689 } else {
6690 /* <equal> points to next comma, semi-colon or EOL */
6691 val_beg = val_end = next = equal;
6692 }
6693
6694 if (next < hdr_end) {
6695 /* Set-Cookie2 supports multiple cookies, and <next> points to
6696 * a colon or semi-colon before the end. So skip all attr-value
6697 * pairs and look for the next comma. For Set-Cookie, since
6698 * commas are permitted in values, skip to the end.
6699 */
6700 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02006701 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006702 else
6703 next = hdr_end;
6704 }
6705
6706 /* Now everything is as on the diagram above */
6707
6708 /* Ignore cookies with no equal sign */
6709 if (equal == val_end)
6710 continue;
6711
6712 /* If there are spaces around the equal sign, we need to
6713 * strip them otherwise we'll get trouble for cookie captures,
6714 * or even for rewrites. Since this happens extremely rarely,
6715 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006716 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006717 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6718 int stripped_before = 0;
6719 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006720
Willy Tarreau24581ba2010-08-31 22:39:35 +02006721 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006722 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006723 equal += stripped_before;
6724 val_beg += stripped_before;
6725 }
6726
6727 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006728 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006729 val_beg += stripped_after;
6730 stripped_before += stripped_after;
6731 }
6732
6733 val_end += stripped_before;
6734 next += stripped_before;
6735 hdr_end += stripped_before;
6736 hdr_next += stripped_before;
6737 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006738 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006739 }
6740
6741 /* First, let's see if we want to capture this cookie. We check
6742 * that we don't already have a server side cookie, because we
6743 * can only capture one. Also as an optimisation, we ignore
6744 * cookies shorter than the declared name.
6745 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006746 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006747 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006748 (val_end - att_beg >= sess->fe->capture_namelen) &&
6749 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006750 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006751 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006752 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01006753 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006754 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006755 if (log_len > sess->fe->capture_len)
6756 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01006757 memcpy(txn->srv_cookie, att_beg, log_len);
6758 txn->srv_cookie[log_len] = 0;
6759 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006760 }
6761
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006762 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006763 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006764 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006765 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6766 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006767 /* assume passive cookie by default */
6768 txn->flags &= ~TX_SCK_MASK;
6769 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006770
6771 /* If the cookie is in insert mode on a known server, we'll delete
6772 * this occurrence because we'll insert another one later.
6773 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006774 * a direct access.
6775 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006776 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006777 /* The "preserve" flag was set, we don't want to touch the
6778 * server's cookie.
6779 */
6780 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006781 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006782 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006783 /* this cookie must be deleted */
6784 if (*prev == ':' && next == hdr_end) {
6785 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006786 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006787 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6788 txn->hdr_idx.used--;
6789 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006790 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006791 hdr_next += delta;
6792 http_msg_move_end(&txn->rsp, delta);
6793 /* note: while both invalid now, <next> and <hdr_end>
6794 * are still equal, so the for() will stop as expected.
6795 */
6796 } else {
6797 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006798 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006799 next = prev;
6800 hdr_end += delta;
6801 hdr_next += delta;
6802 cur_hdr->len += delta;
6803 http_msg_move_end(&txn->rsp, delta);
6804 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006805 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006806 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006807 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006808 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006809 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006810 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006811 * with this server since we know it.
6812 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006813 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006814 next += delta;
6815 hdr_end += delta;
6816 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006817 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006818 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006819
Willy Tarreauf1348312010-10-07 15:54:11 +02006820 txn->flags &= ~TX_SCK_MASK;
6821 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006822 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006823 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006824 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006825 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006826 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006827 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006828 next += delta;
6829 hdr_end += delta;
6830 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006831 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006832 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006833
Willy Tarreau827aee92011-03-10 16:55:02 +01006834 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006835 txn->flags &= ~TX_SCK_MASK;
6836 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006837 }
6838 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006839 /* that's done for this cookie, check the next one on the same
6840 * line when next != hdr_end (only if is_cookie2).
6841 */
6842 }
6843 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006844 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006845 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006846}
6847
6848
Willy Tarreaua15645d2007-03-18 16:22:39 +01006849/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006850 * Parses the Cache-Control and Pragma request header fields to determine if
6851 * the request may be served from the cache and/or if it is cacheable. Updates
6852 * s->txn->flags.
6853 */
6854void check_request_for_cacheability(struct stream *s, struct channel *chn)
6855{
6856 struct http_txn *txn = s->txn;
6857 char *p1, *p2;
6858 char *cur_ptr, *cur_end, *cur_next;
6859 int pragma_found;
6860 int cc_found;
6861 int cur_idx;
6862
Christopher Faulet25a02f62018-10-24 12:00:25 +02006863 if (IS_HTX_STRM(s))
6864 return htx_check_request_for_cacheability(s, chn);
6865
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006866 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
6867 return; /* nothing more to do here */
6868
6869 cur_idx = 0;
6870 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006871 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006872
6873 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6874 struct hdr_idx_elem *cur_hdr;
6875 int val;
6876
6877 cur_hdr = &txn->hdr_idx.v[cur_idx];
6878 cur_ptr = cur_next;
6879 cur_end = cur_ptr + cur_hdr->len;
6880 cur_next = cur_end + cur_hdr->cr + 1;
6881
6882 /* We have one full header between cur_ptr and cur_end, and the
6883 * next header starts at cur_next.
6884 */
6885
6886 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6887 if (val) {
6888 if ((cur_end - (cur_ptr + val) >= 8) &&
6889 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6890 pragma_found = 1;
6891 continue;
6892 }
6893 }
6894
William Lallemand8a16fe02018-05-22 11:04:33 +02006895 /* Don't use the cache and don't try to store if we found the
6896 * Authorization header */
6897 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
6898 if (val) {
6899 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6900 txn->flags |= TX_CACHE_IGNORE;
6901 continue;
6902 }
6903
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006904 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6905 if (!val)
6906 continue;
6907
6908 /* OK, right now we know we have a cache-control header at cur_ptr */
6909 cc_found = 1;
6910 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
6911
6912 if (p1 >= cur_end) /* no more info */
6913 continue;
6914
6915 /* p1 is at the beginning of the value */
6916 p2 = p1;
6917 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
6918 p2++;
6919
6920 /* we have a complete value between p1 and p2. We don't check the
6921 * values after max-age, max-stale nor min-fresh, we simply don't
6922 * use the cache when they're specified.
6923 */
6924 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
6925 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
6926 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
6927 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
6928 txn->flags |= TX_CACHE_IGNORE;
6929 continue;
6930 }
6931
6932 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
6933 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6934 continue;
6935 }
6936 }
6937
6938 /* RFC7234#5.4:
6939 * When the Cache-Control header field is also present and
6940 * understood in a request, Pragma is ignored.
6941 * When the Cache-Control header field is not present in a
6942 * request, caches MUST consider the no-cache request
6943 * pragma-directive as having the same effect as if
6944 * "Cache-Control: no-cache" were present.
6945 */
6946 if (!cc_found && pragma_found)
6947 txn->flags |= TX_CACHE_IGNORE;
6948}
6949
6950/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01006951 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006952 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006953void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006954{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006955 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006956 char *p1, *p2;
6957
6958 char *cur_ptr, *cur_end, *cur_next;
6959 int cur_idx;
6960
Christopher Faulet25a02f62018-10-24 12:00:25 +02006961
6962 if (IS_HTX_STRM(s))
6963 return htx_check_response_for_cacheability(s, rtr);
6964
Willy Tarreau12b32f22017-12-21 16:08:09 +01006965 if (txn->status < 200) {
6966 /* do not try to cache interim responses! */
6967 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006968 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01006969 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006970
6971 /* Iterate through the headers.
6972 * we start with the start line.
6973 */
6974 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006975 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006976
6977 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6978 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006979 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006980
6981 cur_hdr = &txn->hdr_idx.v[cur_idx];
6982 cur_ptr = cur_next;
6983 cur_end = cur_ptr + cur_hdr->len;
6984 cur_next = cur_end + cur_hdr->cr + 1;
6985
6986 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01006987 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006988 */
6989
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006990 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6991 if (val) {
6992 if ((cur_end - (cur_ptr + val) >= 8) &&
6993 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6994 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6995 return;
6996 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006997 }
6998
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006999 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7000 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007001 continue;
7002
7003 /* OK, right now we know we have a cache-control header at cur_ptr */
7004
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007005 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007006
7007 if (p1 >= cur_end) /* no more info */
7008 continue;
7009
7010 /* p1 is at the beginning of the value */
7011 p2 = p1;
7012
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007013 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007014 p2++;
7015
7016 /* we have a complete value between p1 and p2 */
7017 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007018 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7019 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7020 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7021 continue;
7022 }
7023
Willy Tarreaua15645d2007-03-18 16:22:39 +01007024 /* we have something of the form no-cache="set-cookie" */
7025 if ((cur_end - p1 >= 21) &&
7026 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7027 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007028 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007029 continue;
7030 }
7031
7032 /* OK, so we know that either p2 points to the end of string or to a comma */
7033 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007034 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007035 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007036 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007037 return;
7038 }
7039
7040 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007041 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007042 continue;
7043 }
7044 }
7045}
7046
Willy Tarreau58f10d72006-12-04 02:26:12 +01007047
Willy Tarreaub2513902006-12-17 14:52:38 +01007048/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007049 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007050 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007051 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007052 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007053 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007054 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007055 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007056 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007057int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007058{
7059 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007060 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007061 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007062
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007063 if (!uri_auth)
7064 return 0;
7065
Cyril Bonté70be45d2010-10-12 00:14:35 +02007066 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007067 return 0;
7068
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007069 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007070 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007071 return 0;
7072
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007073 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007074 return 0;
7075
Willy Tarreaub2513902006-12-17 14:52:38 +01007076 return 1;
7077}
7078
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007079/* Append the description of what is present in error snapshot <es> into <out>.
7080 * The description must be small enough to always fit in a trash. The output
7081 * buffer may be the trash so the trash must not be used inside this function.
7082 */
7083void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7084{
Christopher Fauleted26fb82018-11-29 22:53:30 +01007085 chunk_appendf(out,
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007086 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7087 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7088 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7089 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7090 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7091 es->ctx.http.m_flags, es->ctx.http.m_clen,
7092 es->ctx.http.m_blen, es->ctx.http.b_flags);
7093}
7094
Willy Tarreau4076a152009-04-02 15:18:36 +02007095/*
7096 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007097 * By default it tries to report the error position as msg->err_pos. However if
7098 * this one is not set, it will then report msg->next, which is the last known
7099 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007100 * displays buffers as a contiguous area starting at buf->p. The direction is
7101 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007102 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007103void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007104 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007105 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007106{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007107 union error_snapshot_ctx ctx;
7108 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007109
Willy Tarreauef3ca732018-09-07 15:47:35 +02007110 /* http-specific part now */
7111 ctx.http.sid = s->uniq_id;
7112 ctx.http.state = state;
7113 ctx.http.b_flags = msg->chn->flags;
7114 ctx.http.s_flags = s->flags;
7115 ctx.http.t_flags = s->txn->flags;
7116 ctx.http.m_flags = msg->flags;
7117 ctx.http.m_clen = msg->chunk_len;
7118 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007119
Willy Tarreauef3ca732018-09-07 15:47:35 +02007120 ofs = msg->chn->total - ci_data(msg->chn);
7121 if (ofs < 0)
7122 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007123
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007124 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007125 other_end, s->target,
7126 strm_sess(s), &msg->chn->buf,
7127 ofs, co_data(msg->chn),
7128 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7129 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007130}
Willy Tarreaub2513902006-12-17 14:52:38 +01007131
Willy Tarreaubaaee002006-06-26 02:48:02 +02007132/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007133 * Print a debug line with a header. Always stop at the first CR or LF char,
7134 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7135 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007136 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007137void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007138{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007139 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007140 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007141
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007142 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007143 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007144 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007145 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 +02007146
7147 for (max = 0; start + max < end; max++)
7148 if (start[max] == '\r' || start[max] == '\n')
7149 break;
7150
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007151 UBOUND(max, trash.size - trash.data - 3);
7152 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7153 trash.area[trash.data++] = '\n';
7154 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007155}
7156
Willy Tarreaueee5b512015-04-03 23:46:31 +02007157
7158/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7159 * The hdr_idx is allocated as well. In case of allocation failure, everything
7160 * allocated is freed and NULL is returned. Otherwise the new transaction is
7161 * assigned to the stream and returned.
7162 */
7163struct http_txn *http_alloc_txn(struct stream *s)
7164{
7165 struct http_txn *txn = s->txn;
7166
7167 if (txn)
7168 return txn;
7169
Willy Tarreaubafbe012017-11-24 17:34:44 +01007170 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007171 if (!txn)
7172 return txn;
7173
7174 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007175 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007176 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007177 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007178 return NULL;
7179 }
7180
7181 s->txn = txn;
7182 return txn;
7183}
7184
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007185void http_txn_reset_req(struct http_txn *txn)
7186{
7187 txn->req.flags = 0;
7188 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7189 txn->req.next = 0;
7190 txn->req.chunk_len = 0LL;
7191 txn->req.body_len = 0LL;
7192 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7193}
7194
7195void http_txn_reset_res(struct http_txn *txn)
7196{
7197 txn->rsp.flags = 0;
7198 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7199 txn->rsp.next = 0;
7200 txn->rsp.chunk_len = 0LL;
7201 txn->rsp.body_len = 0LL;
7202 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7203}
7204
Willy Tarreau0937bc42009-12-22 15:03:09 +01007205/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007206 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007207 * the required fields are properly allocated and that we only need to (re)init
7208 * them. This should be used before processing any new request.
7209 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007210void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007211{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007212 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007213 struct proxy *fe = strm_fe(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02007214 struct conn_stream *cs = objt_cs(s->si[0].end);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007215
Christopher Fauletf2824e62018-10-01 12:12:37 +02007216 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
7217 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
7218 : 0);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007219 txn->status = -1;
Willy Tarreauc9036c02019-01-11 19:38:25 +01007220 *(unsigned int *)txn->cache_hash = 0;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007221
Willy Tarreauf64d1412010-10-07 20:06:11 +02007222 txn->cookie_first_date = 0;
7223 txn->cookie_last_date = 0;
7224
Willy Tarreaueee5b512015-04-03 23:46:31 +02007225 txn->srv_cookie = NULL;
7226 txn->cli_cookie = NULL;
7227 txn->uri = NULL;
7228
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007229 http_txn_reset_req(txn);
7230 http_txn_reset_res(txn);
7231
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007232 txn->req.chn = &s->req;
7233 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007234
7235 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007236
7237 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7238 if (fe->options2 & PR_O2_REQBUG_OK)
7239 txn->req.err_pos = -1; /* let buggy requests pass */
7240
Willy Tarreau0937bc42009-12-22 15:03:09 +01007241 if (txn->hdr_idx.v)
7242 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007243
7244 vars_init(&s->vars_txn, SCOPE_TXN);
7245 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007246}
7247
7248/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007249void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007250{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007251 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007252 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007253
7254 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007255 pool_free(pool_head_requri, txn->uri);
7256 pool_free(pool_head_capture, txn->cli_cookie);
7257 pool_free(pool_head_capture, txn->srv_cookie);
7258 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007259
William Lallemanda73203e2012-03-12 12:48:57 +01007260 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007261 txn->uri = NULL;
7262 txn->srv_cookie = NULL;
7263 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007264
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007265 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007266 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007267 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007268 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007269 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007270 }
7271
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007272 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007273 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007274 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007275 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007276 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007277 }
7278
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007279 if (!LIST_ISEMPTY(&s->vars_txn.head))
7280 vars_prune(&s->vars_txn, s->sess, s);
7281 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7282 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007283}
7284
7285/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007286void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007287{
7288 http_end_txn(s);
7289 http_init_txn(s);
7290
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007291 /* reinitialise the current rule list pointer to NULL. We are sure that
7292 * any rulelist match the NULL pointer.
7293 */
7294 s->current_rule_list = NULL;
7295
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007296 s->be = strm_fe(s);
7297 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007298 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007299 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007300 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007301 /* re-init store persistence */
7302 s->store_count = 0;
Olivier Houcharda798bf52019-03-08 18:52:00 +01007303 s->uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007304
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007305 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007306
Willy Tarreau739cfba2010-01-25 23:11:14 +01007307 /* We must trim any excess data from the response buffer, because we
7308 * may have blocked an invalid response from a server that we don't
Joseph Herlant5ba80252018-11-15 09:25:36 -08007309 * want to accidently forward once we disable the analysers, nor do
Willy Tarreau739cfba2010-01-25 23:11:14 +01007310 * we want those data to come along with next response. A typical
7311 * example of such data would be from a buggy server responding to
7312 * a HEAD with some data, or sending more than the advertised
7313 * content-length.
7314 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007315 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007316 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007317
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007318 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007319 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007320
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007321 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007322 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007323
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007324 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007325 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007326
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007327 s->req.rex = TICK_ETERNITY;
7328 s->req.wex = TICK_ETERNITY;
7329 s->req.analyse_exp = TICK_ETERNITY;
7330 s->res.rex = TICK_ETERNITY;
7331 s->res.wex = TICK_ETERNITY;
7332 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007333 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007334}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007335
Willy Tarreau79e57332018-10-02 16:01:16 +02007336/* This function executes one of the set-{method,path,query,uri} actions. It
7337 * takes the string from the variable 'replace' with length 'len', then modifies
7338 * the relevant part of the request line accordingly. Then it updates various
7339 * pointers to the next elements which were moved, and the total buffer length.
7340 * It finds the action to be performed in p[2], previously filled by function
7341 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7342 * error, though this can be revisited when this code is finally exploited.
7343 *
7344 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7345 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007346 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007347 * In query string case, the mark question '?' must be set at the start of the
7348 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007349 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007350int http_replace_req_line(int action, const char *replace, int len,
7351 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007352{
Willy Tarreau79e57332018-10-02 16:01:16 +02007353 struct http_txn *txn = s->txn;
7354 char *cur_ptr, *cur_end;
7355 int offset = 0;
7356 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007357
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007358 if (IS_HTX_STRM(s))
7359 return htx_req_replace_stline(action, replace, len, px, s);
7360
Willy Tarreau79e57332018-10-02 16:01:16 +02007361 switch (action) {
7362 case 0: // method
7363 cur_ptr = ci_head(&s->req);
7364 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007365
Willy Tarreau79e57332018-10-02 16:01:16 +02007366 /* adjust req line offsets and lengths */
7367 delta = len - offset - (cur_end - cur_ptr);
7368 txn->req.sl.rq.m_l += delta;
7369 txn->req.sl.rq.u += delta;
7370 txn->req.sl.rq.v += delta;
7371 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007372
Willy Tarreau79e57332018-10-02 16:01:16 +02007373 case 1: // path
7374 cur_ptr = http_txn_get_path(txn);
7375 if (!cur_ptr)
7376 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007377
Willy Tarreau79e57332018-10-02 16:01:16 +02007378 cur_end = cur_ptr;
7379 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7380 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007381
Willy Tarreau79e57332018-10-02 16:01:16 +02007382 /* adjust req line offsets and lengths */
7383 delta = len - offset - (cur_end - cur_ptr);
7384 txn->req.sl.rq.u_l += delta;
7385 txn->req.sl.rq.v += delta;
7386 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007387
Willy Tarreau79e57332018-10-02 16:01:16 +02007388 case 2: // query
7389 offset = 1;
7390 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7391 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7392 while (cur_ptr < cur_end && *cur_ptr != '?')
7393 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007394
Willy Tarreau79e57332018-10-02 16:01:16 +02007395 /* skip the question mark or indicate that we must insert it
7396 * (but only if the format string is not empty then).
7397 */
7398 if (cur_ptr < cur_end)
7399 cur_ptr++;
7400 else if (len > 1)
7401 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007402
Willy Tarreau79e57332018-10-02 16:01:16 +02007403 /* adjust req line offsets and lengths */
7404 delta = len - offset - (cur_end - cur_ptr);
7405 txn->req.sl.rq.u_l += delta;
7406 txn->req.sl.rq.v += delta;
7407 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007408
Willy Tarreau79e57332018-10-02 16:01:16 +02007409 case 3: // uri
7410 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7411 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007412
Willy Tarreau79e57332018-10-02 16:01:16 +02007413 /* adjust req line offsets and lengths */
7414 delta = len - offset - (cur_end - cur_ptr);
7415 txn->req.sl.rq.u_l += delta;
7416 txn->req.sl.rq.v += delta;
7417 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007418
Willy Tarreau79e57332018-10-02 16:01:16 +02007419 default:
7420 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007421 }
7422
Willy Tarreau79e57332018-10-02 16:01:16 +02007423 /* commit changes and adjust end of message */
7424 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7425 txn->req.sl.rq.l += delta;
7426 txn->hdr_idx.v[0].len += delta;
7427 http_msg_move_end(&txn->req, delta);
7428 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007429}
7430
Willy Tarreau79e57332018-10-02 16:01:16 +02007431/* This function replace the HTTP status code and the associated message. The
7432 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007433 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007434void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007435{
Willy Tarreau79e57332018-10-02 16:01:16 +02007436 struct http_txn *txn = s->txn;
7437 char *cur_ptr, *cur_end;
7438 int delta;
7439 char *res;
7440 int c_l;
7441 const char *msg = reason;
7442 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007443
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007444 if (IS_HTX_STRM(s))
7445 return htx_res_set_status(status, reason, s);
7446
Willy Tarreau79e57332018-10-02 16:01:16 +02007447 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007448
Willy Tarreau79e57332018-10-02 16:01:16 +02007449 res = ultoa_o(status, trash.area, trash.size);
7450 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007451
Willy Tarreau79e57332018-10-02 16:01:16 +02007452 trash.area[c_l] = ' ';
7453 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007454
Willy Tarreau79e57332018-10-02 16:01:16 +02007455 /* Do we have a custom reason format string? */
7456 if (msg == NULL)
7457 msg = http_get_reason(status);
7458 msg_len = strlen(msg);
7459 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7460 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007461
Willy Tarreau79e57332018-10-02 16:01:16 +02007462 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7463 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007464
Willy Tarreau79e57332018-10-02 16:01:16 +02007465 /* commit changes and adjust message */
7466 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7467 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007468
Willy Tarreau79e57332018-10-02 16:01:16 +02007469 /* adjust res line offsets and lengths */
7470 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7471 txn->rsp.sl.st.c_l = c_l;
7472 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007473
Willy Tarreau79e57332018-10-02 16:01:16 +02007474 delta = trash.data - (cur_end - cur_ptr);
7475 txn->rsp.sl.st.l += delta;
7476 txn->hdr_idx.v[0].len += delta;
7477 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007478}
7479
Willy Tarreau58f10d72006-12-04 02:26:12 +01007480/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007481 * Local variables:
7482 * c-indent-level: 8
7483 * c-basic-offset: 8
7484 * End:
7485 */