blob: 1d75b056a5ddc7a406e954a2f0142a0f34fceb48 [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
Christopher Faulet87451fd2019-03-01 11:16:34 +01001384/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. */
1385static void http_handle_expect_hdr(struct stream *s, struct channel *req, struct http_msg *msg)
1386{
1387 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
1388 * then we must send an HTTP/1.1 100 Continue intermediate response.
1389 */
1390 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
1391 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
1392 struct hdr_ctx ctx;
1393 ctx.idx = 0;
1394 /* Expect is allowed in 1.1, look for it */
1395 if (http_find_header2("Expect", 6, ci_head(req), &s->txn->hdr_idx, &ctx) &&
1396 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
1397 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
1398 http_remove_header2(&s->txn->req, &s->txn->hdr_idx, &ctx);
1399 }
1400 }
1401}
1402
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001403/*
1404 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
1405 * built according to <fmt> log line format.
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001406 * If <early_hints> is NULL, it is allocated and the HTTP 103 response first
1407 * line is inserted before the header. If an error occurred <early_hints> is
1408 * released and NULL is returned. On success the updated buffer is returned.
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001409 */
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001410static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints,
1411 const char* name, unsigned int name_len,
1412 struct list *fmt)
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001413{
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001414 if (!early_hints) {
1415 early_hints = alloc_trash_chunk();
1416 if (!early_hints)
1417 goto fail;
1418 if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len))
1419 goto fail;
1420 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001421
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001422 if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2))
1423 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001424
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001425 early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt);
1426 if (!chunk_memcat(early_hints, "\r\n", 2))
1427 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001428
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001429 return early_hints;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001430
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001431 fail:
1432 free_trash_chunk(early_hints);
1433 return NULL;
1434}
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001435
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001436/* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the
1437 * response is added. If an error occurred or if another response was already
1438 * sent, this function does nothing.
1439 */
1440static void http_send_early_hints(struct stream *s, struct buffer *early_hints)
1441{
1442 struct channel *chn = s->txn->rsp.chn;
1443 char *cur_ptr = ci_head(chn);
1444 int ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001445
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001446 /* If a response was already sent, skip early hints */
1447 if (s->txn->status > 0)
1448 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001449
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001450 if (!chunk_memcat(early_hints, "\r\n", 2))
1451 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001452
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001453 ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints));
1454 c_adv(chn, ret);
1455 chn->total += ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001456}
1457
Willy Tarreau87b09662015-04-03 00:22:06 +02001458/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001459 * transaction <txn>. Returns the verdict of the first rule that prevents
1460 * further processing of the request (auth, deny, ...), and defaults to
1461 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1462 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001463 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1464 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1465 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001466 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001467enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001468http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001469{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001470 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001471 struct http_txn *txn = s->txn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001472 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001473 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001474 const char *auth_realm;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001475 struct buffer *early_hints = NULL;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001476 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001477 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001478 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001479
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001480 /* If "the current_rule_list" match the executed rule list, we are in
1481 * resume condition. If a resume is needed it is always in the action
1482 * and never in the ACL or converters. In this case, we initialise the
1483 * current rule, and go to the action execution point.
1484 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001485 if (s->current_rule) {
1486 rule = s->current_rule;
1487 s->current_rule = NULL;
1488 if (s->current_rule_list == rules)
1489 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001490 }
1491 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001492
Willy Tarreauff011f22011-01-06 17:51:27 +01001493 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001494
Willy Tarreau96257ec2012-12-27 10:46:37 +01001495 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001496 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001497 int ret;
1498
Willy Tarreau192252e2015-04-04 01:47:55 +02001499 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001500 ret = acl_pass(ret);
1501
Willy Tarreauff011f22011-01-06 17:51:27 +01001502 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001503 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001504
1505 if (!ret) /* condition not matched */
1506 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001507 }
1508
Willy Tarreauacc98002015-09-27 23:34:39 +02001509 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001510resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001511 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001512 case ACT_ACTION_ALLOW:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001513 rule_ret = HTTP_RULE_RES_STOP;
1514 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001515
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001516 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001517 if (deny_status)
1518 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001519 rule_ret = HTTP_RULE_RES_DENY;
1520 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001521
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001522 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001523 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001524 if (deny_status)
1525 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001526 rule_ret = HTTP_RULE_RES_DENY;
1527 goto end;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001528
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001529 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001530 /* Be sure to send any pending HTTP 103 response first */
1531 if (early_hints) {
1532 http_send_early_hints(s, early_hints);
1533 free_trash_chunk(early_hints);
1534 early_hints = NULL;
1535 }
Willy Tarreauae3c0102014-04-28 23:22:08 +02001536 /* Auth might be performed on regular http-req rules as well as on stats */
1537 auth_realm = rule->arg.auth.realm;
1538 if (!auth_realm) {
1539 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1540 auth_realm = STATS_DEFAULT_REALM;
1541 else
1542 auth_realm = px->id;
1543 }
1544 /* send 401/407 depending on whether we use a proxy or not. We still
1545 * count one error, because normal browsing won't significantly
1546 * increase the counter but brute force attempts will.
1547 */
1548 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1549 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001550 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001551 stream_inc_http_err_ctr(s);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001552 rule_ret = HTTP_RULE_RES_ABRT;
1553 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001554
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001555 case ACT_HTTP_REDIR:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001556 /* Be sure to send any pending HTTP 103 response first */
1557 if (early_hints) {
1558 http_send_early_hints(s, early_hints);
1559 free_trash_chunk(early_hints);
1560 early_hints = NULL;
1561 }
Christopher Fauletea827bd2018-11-15 15:34:11 +01001562 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau0b748332014-04-29 00:13:29 +02001563 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Fauletea827bd2018-11-15 15:34:11 +01001564 rule_ret = HTTP_RULE_RES_BADREQ;
1565 goto end;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001566
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001567 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001568 s->task->nice = rule->arg.nice;
1569 break;
1570
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001571 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001572 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001573 break;
1574
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001575 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001576 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Willy Tarreau51347ed2013-06-11 19:34:13 +02001577 break;
1578
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001579 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001580 s->logs.level = rule->arg.loglevel;
1581 break;
1582
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001583 case ACT_HTTP_REPLACE_HDR:
1584 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001585 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1586 rule->arg.hdr_add.name_len,
1587 &rule->arg.hdr_add.fmt,
Christopher Fauletea827bd2018-11-15 15:34:11 +01001588 &rule->arg.hdr_add.re, rule->action)) {
1589 rule_ret = HTTP_RULE_RES_BADREQ;
1590 goto end;
1591 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001592 break;
1593
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001594 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001595 ctx.idx = 0;
1596 /* remove all occurrences of the header */
1597 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001598 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001599 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001600 }
Willy Tarreau85603282015-01-21 20:39:27 +01001601 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001602
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001603 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001604 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001605 /* The scope of the trash buffer must be limited to this function. The
1606 * build_logline() function can execute a lot of other function which
1607 * can use the trash buffer. So for limiting the scope of this global
1608 * buffer, we build first the header value using build_logline, and
1609 * after we store the header name.
1610 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001611 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001612
1613 replace = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001614 if (!replace) {
1615 rule_ret = HTTP_RULE_RES_BADREQ;
1616 goto end;
1617 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001618
Thierry Fournier4b788f72016-06-01 13:35:36 +02001619 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001620 len += build_logline(s, replace->area + len,
1621 replace->size - len,
1622 &rule->arg.hdr_add.fmt);
1623 memcpy(replace->area, rule->arg.hdr_add.name,
1624 rule->arg.hdr_add.name_len);
1625 replace->area[rule->arg.hdr_add.name_len] = ':';
1626 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1627 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001628
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001629 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001630 /* remove all occurrences of the header */
1631 ctx.idx = 0;
1632 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001633 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001634 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1635 }
1636 }
1637
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001638 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001639 static unsigned char rate_limit = 0;
1640
1641 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001642 replace->area[rule->arg.hdr_add.name_len] = 0;
1643 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,
1644 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001645 }
1646
Olivier Houcharda798bf52019-03-08 18:52:00 +01001647 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001648 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001649 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001650 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001651 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001652 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001653
1654 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001655 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001656 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001657
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001658 case ACT_HTTP_DEL_ACL:
1659 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001660 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001661 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001662
1663 /* collect reference */
1664 ref = pat_ref_lookup(rule->arg.map.ref);
1665 if (!ref)
1666 continue;
1667
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001668 /* allocate key */
1669 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001670 if (!key) {
1671 rule_ret = HTTP_RULE_RES_BADREQ;
1672 goto end;
1673 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001674
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001675 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001676 key->data = build_logline(s, key->area, key->size,
1677 &rule->arg.map.key);
1678 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001679
1680 /* perform update */
1681 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001682 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001683 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001684 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001685
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001686 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001687 break;
1688 }
1689
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001690 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001691 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001692 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001693
1694 /* collect reference */
1695 ref = pat_ref_lookup(rule->arg.map.ref);
1696 if (!ref)
1697 continue;
1698
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001699 /* allocate key */
1700 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001701 if (!key) {
1702 rule_ret = HTTP_RULE_RES_BADREQ;
1703 goto end;
1704 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001705
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001706 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001707 key->data = build_logline(s, key->area, key->size,
1708 &rule->arg.map.key);
1709 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001710
1711 /* perform update */
1712 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001713 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001714 if (pat_ref_find_elt(ref, key->area) == NULL)
1715 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001716 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001717
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001718 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001719 break;
1720 }
1721
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001722 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001723 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001724 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001725
1726 /* collect reference */
1727 ref = pat_ref_lookup(rule->arg.map.ref);
1728 if (!ref)
1729 continue;
1730
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001731 /* allocate key */
1732 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001733 if (!key) {
1734 rule_ret = HTTP_RULE_RES_BADREQ;
1735 goto end;
1736 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001737
1738 /* allocate value */
1739 value = alloc_trash_chunk();
1740 if (!value) {
1741 free_trash_chunk(key);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001742 rule_ret = HTTP_RULE_RES_BADREQ;
1743 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001744 }
1745
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001746 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001747 key->data = build_logline(s, key->area, key->size,
1748 &rule->arg.map.key);
1749 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001750
1751 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001752 value->data = build_logline(s, value->area,
1753 value->size,
1754 &rule->arg.map.value);
1755 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001756
1757 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001758 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001759 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001760 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001761 else
1762 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001763 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001764
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001765 free_trash_chunk(key);
1766 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001767 break;
1768 }
William Lallemand73025dd2014-04-24 14:38:37 +02001769
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001770 case ACT_HTTP_EARLY_HINT:
1771 if (!(txn->req.flags & HTTP_MSGF_VER_11))
1772 break;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001773 early_hints = http_apply_early_hint_rule(s, early_hints,
1774 rule->arg.early_hint.name,
1775 rule->arg.early_hint.name_len,
1776 &rule->arg.early_hint.fmt);
1777 if (!early_hints) {
Christopher Fauletea827bd2018-11-15 15:34:11 +01001778 rule_ret = HTTP_RULE_RES_DONE;
1779 goto end;
1780 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001781 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02001782 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01001783 if ((s->req.flags & CF_READ_ERROR) ||
1784 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
1785 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
1786 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02001787 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02001788
Willy Tarreauacc98002015-09-27 23:34:39 +02001789 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02001790 case ACT_RET_ERR:
1791 case ACT_RET_CONT:
1792 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02001793 case ACT_RET_STOP:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001794 rule_ret = HTTP_RULE_RES_DONE;
1795 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02001796 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02001797 s->current_rule = rule;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001798 rule_ret = HTTP_RULE_RES_YIELD;
1799 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001800 }
William Lallemand73025dd2014-04-24 14:38:37 +02001801 break;
1802
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001803 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02001804 /* Note: only the first valid tracking parameter of each
1805 * applies.
1806 */
1807
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001808 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02001809 struct stktable *t;
1810 struct stksess *ts;
1811 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001812 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02001813
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02001814 t = rule->arg.trk_ctr.table.t;
1815 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 +02001816
1817 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001818 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02001819
1820 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001821 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1822 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1823 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001824 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02001825
Emeric Brun819fc6f2017-06-13 19:37:32 +02001826 if (ptr1)
1827 stktable_data_cast(ptr1, http_req_cnt)++;
1828
1829 if (ptr2)
1830 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
1831 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
1832
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001833 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01001834
1835 /* If data was modified, we need to touch to re-schedule sync */
1836 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001837 }
Willy Tarreau09448f72014-06-25 18:12:15 +02001838
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001839 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001840 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001841 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02001842 }
1843 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02001844 break;
1845
Joseph Herlant5ba80252018-11-15 09:25:36 -08001846 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02001847 default:
1848 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001849 }
1850 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01001851
Christopher Fauletea827bd2018-11-15 15:34:11 +01001852 end:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001853 if (early_hints) {
1854 http_send_early_hints(s, early_hints);
1855 free_trash_chunk(early_hints);
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001856 }
1857
Willy Tarreau96257ec2012-12-27 10:46:37 +01001858 /* we reached the end of the rules, nothing to report */
Christopher Fauletea827bd2018-11-15 15:34:11 +01001859 return rule_ret;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001860}
1861
Willy Tarreau71241ab2012-12-27 11:30:54 +01001862
Willy Tarreau51d861a2015-05-22 17:30:48 +02001863/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
1864 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
1865 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
1866 * is returned, the process can continue the evaluation of next rule list. If
1867 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
1868 * is returned, it means the operation could not be processed and a server error
1869 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
1870 * deny rule. If *YIELD is returned, the caller must call again the function
1871 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001872 */
Christopher Faulet10079f52018-10-03 15:17:28 +02001873enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001874http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001875{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001876 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001877 struct http_txn *txn = s->txn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001878 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001879 struct hdr_ctx ctx;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001880 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001881 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001882
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001883 /* If "the current_rule_list" match the executed rule list, we are in
1884 * resume condition. If a resume is needed it is always in the action
1885 * and never in the ACL or converters. In this case, we initialise the
1886 * current rule, and go to the action execution point.
1887 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001888 if (s->current_rule) {
1889 rule = s->current_rule;
1890 s->current_rule = NULL;
1891 if (s->current_rule_list == rules)
1892 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001893 }
1894 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001895
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001896 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001897
1898 /* check optional condition */
1899 if (rule->cond) {
1900 int ret;
1901
Willy Tarreau192252e2015-04-04 01:47:55 +02001902 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001903 ret = acl_pass(ret);
1904
1905 if (rule->cond->pol == ACL_COND_UNLESS)
1906 ret = !ret;
1907
1908 if (!ret) /* condition not matched */
1909 continue;
1910 }
1911
Willy Tarreauacc98002015-09-27 23:34:39 +02001912 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001913resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001914 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001915 case ACT_ACTION_ALLOW:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001916 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
1917 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001918
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001919 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001920 txn->flags |= TX_SVDENY;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001921 rule_ret = HTTP_RULE_RES_STOP;
1922 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001923
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001924 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001925 s->task->nice = rule->arg.nice;
1926 break;
1927
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001928 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001929 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001930 break;
1931
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001932 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001933 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Willy Tarreau51347ed2013-06-11 19:34:13 +02001934 break;
1935
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001936 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001937 s->logs.level = rule->arg.loglevel;
1938 break;
1939
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001940 case ACT_HTTP_REPLACE_HDR:
1941 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001942 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
1943 rule->arg.hdr_add.name_len,
1944 &rule->arg.hdr_add.fmt,
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001945 &rule->arg.hdr_add.re, rule->action)) {
1946 rule_ret = HTTP_RULE_RES_BADREQ;
1947 goto end;
1948 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001949 break;
1950
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001951 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001952 ctx.idx = 0;
1953 /* remove all occurrences of the header */
1954 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001955 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001956 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
1957 }
Willy Tarreau85603282015-01-21 20:39:27 +01001958 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001959
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001960 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001961 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02001962 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001963
1964 replace = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001965 if (!replace) {
1966 rule_ret = HTTP_RULE_RES_BADREQ;
1967 goto end;
1968 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001969
1970 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001971 memcpy(replace->area, rule->arg.hdr_add.name,
1972 rule->arg.hdr_add.name_len);
1973 replace->data = rule->arg.hdr_add.name_len;
1974 replace->area[replace->data++] = ':';
1975 replace->area[replace->data++] = ' ';
1976 replace->data += build_logline(s,
1977 replace->area + replace->data,
1978 replace->size - replace->data,
1979 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01001980
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001981 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001982 /* remove all occurrences of the header */
1983 ctx.idx = 0;
1984 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001985 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001986 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
1987 }
1988 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001989
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001990 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001991 static unsigned char rate_limit = 0;
1992
1993 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001994 replace->area[rule->arg.hdr_add.name_len] = 0;
1995 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,
1996 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001997 }
1998
Olivier Houcharda798bf52019-03-08 18:52:00 +01001999 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002000 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002001 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002002 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002003 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002004 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01002005 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002006 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002007
2008 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002009 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002010 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002011
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002012 case ACT_HTTP_DEL_ACL:
2013 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002014 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002015 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002016
2017 /* collect reference */
2018 ref = pat_ref_lookup(rule->arg.map.ref);
2019 if (!ref)
2020 continue;
2021
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002022 /* allocate key */
2023 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002024 if (!key) {
2025 rule_ret = HTTP_RULE_RES_BADREQ;
2026 goto end;
2027 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002028
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002029 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002030 key->data = build_logline(s, key->area, key->size,
2031 &rule->arg.map.key);
2032 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002033
2034 /* perform update */
2035 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002036 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002037 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002038 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002039
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002040 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002041 break;
2042 }
2043
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002044 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002045 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002046 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002047
2048 /* collect reference */
2049 ref = pat_ref_lookup(rule->arg.map.ref);
2050 if (!ref)
2051 continue;
2052
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002053 /* allocate key */
2054 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002055 if (!key) {
2056 rule_ret = HTTP_RULE_RES_BADREQ;
2057 goto end;
2058 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002059
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002060 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002061 key->data = build_logline(s, key->area, key->size,
2062 &rule->arg.map.key);
2063 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002064
2065 /* perform update */
2066 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002067 if (pat_ref_find_elt(ref, key->area) == NULL)
2068 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002069
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002070 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002071 break;
2072 }
2073
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002074 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002075 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002076 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002077
2078 /* collect reference */
2079 ref = pat_ref_lookup(rule->arg.map.ref);
2080 if (!ref)
2081 continue;
2082
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002083 /* allocate key */
2084 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002085 if (!key) {
2086 rule_ret = HTTP_RULE_RES_BADREQ;
2087 goto end;
2088 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002089
2090 /* allocate value */
2091 value = alloc_trash_chunk();
2092 if (!value) {
2093 free_trash_chunk(key);
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002094 rule_ret = HTTP_RULE_RES_BADREQ;
2095 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002096 }
2097
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002098 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002099 key->data = build_logline(s, key->area, key->size,
2100 &rule->arg.map.key);
2101 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002102
2103 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002104 value->data = build_logline(s, value->area,
2105 value->size,
2106 &rule->arg.map.value);
2107 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002108
2109 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002110 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002111 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002112 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002113 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002114 else
2115 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002116 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002117 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002118 free_trash_chunk(key);
2119 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002120 break;
2121 }
William Lallemand73025dd2014-04-24 14:38:37 +02002122
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002123 case ACT_HTTP_REDIR:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002124 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002125 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002126 rule_ret = HTTP_RULE_RES_BADREQ;
2127 goto end;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002128
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002129 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2130 /* Note: only the first valid tracking parameter of each
2131 * applies.
2132 */
2133
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002134 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002135 struct stktable *t;
2136 struct stksess *ts;
2137 struct stktable_key *key;
2138 void *ptr;
2139
2140 t = rule->arg.trk_ctr.table.t;
2141 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2142
2143 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002144 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002145
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002146 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002147
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002148 /* let's count a new HTTP request as it's the first time we do it */
2149 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2150 if (ptr)
2151 stktable_data_cast(ptr, http_req_cnt)++;
2152
2153 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2154 if (ptr)
2155 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2156 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2157
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002158 /* When the client triggers a 4xx from the server, it's most often due
2159 * to a missing object or permission. These events should be tracked
2160 * because if they happen often, it may indicate a brute force or a
2161 * vulnerability scan. Normally this is done when receiving the response
2162 * but here we're tracking after this ought to have been done so we have
2163 * to do it on purpose.
2164 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002165 if ((unsigned)(txn->status - 400) < 100) {
2166 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2167 if (ptr)
2168 stktable_data_cast(ptr, http_err_cnt)++;
2169
2170 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2171 if (ptr)
2172 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2173 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2174 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002175
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002176 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002177
Emeric Brun0fed0b02017-11-29 16:15:07 +01002178 /* If data was modified, we need to touch to re-schedule sync */
2179 stktable_touch_local(t, ts, 0);
2180
Emeric Brun819fc6f2017-06-13 19:37:32 +02002181 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2182 if (sess->fe != s->be)
2183 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2184
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002185 }
2186 }
2187 break;
2188
Thierry FOURNIER42148732015-09-02 17:17:33 +02002189 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002190 if ((s->req.flags & CF_READ_ERROR) ||
2191 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2192 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2193 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002194 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002195
Willy Tarreauacc98002015-09-27 23:34:39 +02002196 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002197 case ACT_RET_ERR:
2198 case ACT_RET_CONT:
2199 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002200 case ACT_RET_STOP:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002201 rule_ret = HTTP_RULE_RES_STOP;
2202 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002203 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002204 s->current_rule = rule;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002205 rule_ret = HTTP_RULE_RES_YIELD;
2206 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002207 }
William Lallemand73025dd2014-04-24 14:38:37 +02002208 break;
2209
Joseph Herlant5ba80252018-11-15 09:25:36 -08002210 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002211 default:
2212 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002213 }
2214 }
2215
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002216 end:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002217 /* we reached the end of the rules, nothing to report */
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002218 return rule_ret;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002219}
2220
2221
Willy Tarreau71241ab2012-12-27 11:30:54 +01002222/* Perform an HTTP redirect based on the information in <rule>. The function
2223 * returns non-zero on success, or zero in case of a, irrecoverable error such
2224 * as too large a request to build a valid response.
2225 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002226int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002227{
Willy Tarreaub329a312015-05-22 16:27:37 +02002228 struct http_msg *req = &txn->req;
2229 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002230 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002231 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002232 int ret = 0;
2233
Christopher Fauletf2824e62018-10-01 12:12:37 +02002234 if (IS_HTX_STRM(s))
2235 return htx_apply_redirect_rule(rule, s, txn);
2236
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002237 chunk = alloc_trash_chunk();
2238 if (!chunk)
2239 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002240
2241 /* build redirect message */
2242 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002243 case 308:
2244 msg_fmt = HTTP_308;
2245 break;
2246 case 307:
2247 msg_fmt = HTTP_307;
2248 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002249 case 303:
2250 msg_fmt = HTTP_303;
2251 break;
2252 case 301:
2253 msg_fmt = HTTP_301;
2254 break;
2255 case 302:
2256 default:
2257 msg_fmt = HTTP_302;
2258 break;
2259 }
2260
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002261 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2262 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002263
2264 switch(rule->type) {
2265 case REDIRECT_TYPE_SCHEME: {
2266 const char *path;
2267 const char *host;
2268 struct hdr_ctx ctx;
2269 int pathlen;
2270 int hostlen;
2271
2272 host = "";
2273 hostlen = 0;
2274 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002275 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002276 host = ctx.line + ctx.val;
2277 hostlen = ctx.vlen;
2278 }
2279
Willy Tarreau6b952c82018-09-10 17:45:34 +02002280 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002281 /* build message using path */
2282 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002283 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002284 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2285 int qs = 0;
2286 while (qs < pathlen) {
2287 if (path[qs] == '?') {
2288 pathlen = qs;
2289 break;
2290 }
2291 qs++;
2292 }
2293 }
2294 } else {
2295 path = "/";
2296 pathlen = 1;
2297 }
2298
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002299 if (rule->rdr_str) { /* this is an old "redirect" rule */
2300 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002301 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002302 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002303
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002304 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002305 memcpy(chunk->area + chunk->data, rule->rdr_str,
2306 rule->rdr_len);
2307 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002308 }
2309 else {
2310 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002311 chunk->data += build_logline(s,
2312 chunk->area + chunk->data,
2313 chunk->size - chunk->data,
2314 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002315
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002316 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002317 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002318 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002319 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002320 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002321 memcpy(chunk->area + chunk->data, "://", 3);
2322 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002323
2324 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002325 memcpy(chunk->area + chunk->data, host, hostlen);
2326 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002327
2328 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002329 memcpy(chunk->area + chunk->data, path, pathlen);
2330 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002331
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002332 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002333 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002334 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002335 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002336 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002337 chunk->area[chunk->data] = '/';
2338 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002339 }
2340
2341 break;
2342 }
2343 case REDIRECT_TYPE_PREFIX: {
2344 const char *path;
2345 int pathlen;
2346
Willy Tarreau6b952c82018-09-10 17:45:34 +02002347 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002348 /* build message using path */
2349 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002350 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002351 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2352 int qs = 0;
2353 while (qs < pathlen) {
2354 if (path[qs] == '?') {
2355 pathlen = qs;
2356 break;
2357 }
2358 qs++;
2359 }
2360 }
2361 } else {
2362 path = "/";
2363 pathlen = 1;
2364 }
2365
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002366 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002367 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002368 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002369
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002370 /* add prefix. Note that if prefix == "/", we don't want to
2371 * add anything, otherwise it makes it hard for the user to
2372 * configure a self-redirection.
2373 */
2374 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002375 memcpy(chunk->area + chunk->data,
2376 rule->rdr_str, rule->rdr_len);
2377 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002378 }
2379 }
2380 else {
2381 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002382 chunk->data += build_logline(s,
2383 chunk->area + chunk->data,
2384 chunk->size - chunk->data,
2385 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002386
2387 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002388 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002389 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002390 }
2391
2392 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002393 memcpy(chunk->area + chunk->data, path, pathlen);
2394 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002395
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002396 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002397 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002398 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002399 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002400 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002401 chunk->area[chunk->data] = '/';
2402 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002403 }
2404
2405 break;
2406 }
2407 case REDIRECT_TYPE_LOCATION:
2408 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002409 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002410 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002411 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002412
2413 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002414 memcpy(chunk->area + chunk->data, rule->rdr_str,
2415 rule->rdr_len);
2416 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002417 }
2418 else {
2419 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002420 chunk->data += build_logline(s,
2421 chunk->area + chunk->data,
2422 chunk->size - chunk->data,
2423 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002424
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002425 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002426 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002427 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002428 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002429 break;
2430 }
2431
2432 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002433 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2434 chunk->data += 14;
2435 memcpy(chunk->area + chunk->data, rule->cookie_str,
2436 rule->cookie_len);
2437 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002438 }
2439
Willy Tarreau19b14122017-02-28 09:48:11 +01002440 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002441 txn->status = rule->code;
2442 /* let's log the request time */
2443 s->logs.tv_request = now;
2444
Christopher Fauletbe821b92017-03-30 11:21:53 +02002445 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002446 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2447 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2448 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002449 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002450 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002451 memcpy(chunk->area + chunk->data,
2452 "\r\nProxy-Connection: keep-alive", 30);
2453 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002454 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002455 memcpy(chunk->area + chunk->data,
2456 "\r\nConnection: keep-alive", 24);
2457 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002458 }
2459 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002460 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2461 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002462 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002463 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002464 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002465 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002466 req->next -= req->sov;
2467 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002468 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002469 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002470 req->msg_state = HTTP_MSG_CLOSED;
2471 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002472 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002473 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002474 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002475 /* let the server side turn to SI_ST_CLO */
2476 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002477 } else {
2478 /* keep-alive not possible */
2479 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002480 memcpy(chunk->area + chunk->data,
2481 "\r\nProxy-Connection: close\r\n\r\n", 29);
2482 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002483 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002484 memcpy(chunk->area + chunk->data,
2485 "\r\nConnection: close\r\n\r\n", 23);
2486 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002487 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002488 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002489 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002490 }
2491
Willy Tarreaue7dff022015-04-03 01:14:29 +02002492 if (!(s->flags & SF_ERR_MASK))
2493 s->flags |= SF_ERR_LOCAL;
2494 if (!(s->flags & SF_FINST_MASK))
2495 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002496
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002497 ret = 1;
2498 leave:
2499 free_trash_chunk(chunk);
2500 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002501}
2502
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002503/* This stream analyser runs all HTTP request processing which is common to
2504 * frontends and backends, which means blocking ACLs, filters, connection-close,
2505 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002506 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002507 * either needs more data or wants to immediately abort the request (eg: deny,
2508 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002509 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002510int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002511{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002512 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002513 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002514 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002515 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002516 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002517 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002518 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002519 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002520
Christopher Faulete0768eb2018-10-03 16:38:02 +02002521 if (IS_HTX_STRM(s))
2522 return htx_process_req_common(s, req, an_bit, px);
2523
Willy Tarreau655dce92009-11-08 13:10:58 +01002524 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002525 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002526 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002527 }
2528
Christopher Faulet45073512018-07-20 10:16:29 +02002529 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 +02002530 now_ms, __FUNCTION__,
2531 s,
2532 req,
2533 req->rex, req->wex,
2534 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002535 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002536 req->analysers);
2537
Willy Tarreau65410832014-04-28 21:25:43 +02002538 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002539 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002540
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002541 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002542 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002543 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002544
Willy Tarreau0b748332014-04-29 00:13:29 +02002545 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002546 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2547 goto return_prx_yield;
2548
Willy Tarreau0b748332014-04-29 00:13:29 +02002549 case HTTP_RULE_RES_CONT:
2550 case HTTP_RULE_RES_STOP: /* nothing to do */
2551 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002552
Willy Tarreau0b748332014-04-29 00:13:29 +02002553 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2554 if (txn->flags & TX_CLTARPIT)
2555 goto tarpit;
2556 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002557
Willy Tarreau0b748332014-04-29 00:13:29 +02002558 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2559 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002560
Willy Tarreau0b748332014-04-29 00:13:29 +02002561 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002562 goto done;
2563
Willy Tarreau0b748332014-04-29 00:13:29 +02002564 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2565 goto return_bad_req;
2566 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002567 }
2568
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002569 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2570 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002571 struct hdr_ctx ctx;
2572
2573 ctx.idx = 0;
2574 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002575 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002576 if (unlikely(http_header_add_tail2(&txn->req,
2577 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002578 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002579 goto return_bad_req;
2580 }
2581 }
2582
2583 }
2584
Willy Tarreau52542592014-04-28 18:33:26 +02002585 /* OK at this stage, we know that the request was accepted according to
2586 * the http-request rules, we can check for the stats. Note that the
2587 * URI is detected *before* the req* rules in order not to be affected
2588 * by a possible reqrep, while they are processed *after* so that a
2589 * reqdeny can still block them. This clearly needs to change in 1.6!
2590 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002591 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002592 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01002593 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002594 txn->status = 500;
2595 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002596 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002597
Willy Tarreaue7dff022015-04-03 01:14:29 +02002598 if (!(s->flags & SF_ERR_MASK))
2599 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002600 goto return_prx_cond;
2601 }
2602
2603 /* parse the whole stats request and extract the relevant information */
2604 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002605 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002606 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002607
Willy Tarreau0b748332014-04-29 00:13:29 +02002608 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2609 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002610
Willy Tarreau0b748332014-04-29 00:13:29 +02002611 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2612 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002613 }
2614
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002615 /* evaluate the req* rules except reqadd */
2616 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002617 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002618 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002619
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002620 if (txn->flags & TX_CLDENY)
2621 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002622
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002623 if (txn->flags & TX_CLTARPIT) {
2624 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002625 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002626 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002627 }
Willy Tarreau06619262006-12-17 08:37:22 +01002628
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002629 /* add request headers from the rule sets in the same order */
2630 list_for_each_entry(wl, &px->req_add, list) {
2631 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002632 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002633 ret = acl_pass(ret);
2634 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2635 ret = !ret;
2636 if (!ret)
2637 continue;
2638 }
2639
Christopher Faulet10079f52018-10-03 15:17:28 +02002640 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002641 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002642 }
2643
Willy Tarreau52542592014-04-28 18:33:26 +02002644
2645 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002646 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2647 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002648 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002649 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +01002650 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002651
Willy Tarreaue7dff022015-04-03 01:14:29 +02002652 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2653 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2654 if (!(s->flags & SF_FINST_MASK))
2655 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002656
Willy Tarreau70730dd2014-04-24 18:06:27 +02002657 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002658 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2659 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002660 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002661 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002662 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002663
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002664 /* check whether we have some ACLs set to redirect this request */
2665 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002666 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002667 int ret;
2668
Willy Tarreau192252e2015-04-04 01:47:55 +02002669 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002670 ret = acl_pass(ret);
2671 if (rule->cond->pol == ACL_COND_UNLESS)
2672 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002673 if (!ret)
2674 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01002675 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002676 if (!http_apply_redirect_rule(rule, s, txn))
2677 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002678 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002679 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002680
Willy Tarreau2be39392010-01-03 17:24:51 +01002681 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2682 * If this happens, then the data will not come immediately, so we must
2683 * send all what we have without waiting. Note that due to the small gain
2684 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002685 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01002686 * itself once used.
2687 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002688 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01002689
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002690 done: /* done with this analyser, continue with next ones that the calling
2691 * points will have set, if any.
2692 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002693 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002694 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
2695 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002696 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002697
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002698 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02002699 /* Allow cookie logging
2700 */
2701 if (s->be->cookie_name || sess->fe->capture_name)
2702 manage_client_side_cookies(s, req);
2703
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002704 /* When a connection is tarpitted, we use the tarpit timeout,
2705 * which may be the same as the connect timeout if unspecified.
2706 * If unset, then set it to zero because we really want it to
2707 * eventually expire. We build the tarpit as an analyser.
2708 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002709 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002710
2711 /* wipe the request out so that we can drop the connection early
2712 * if the client closes first.
2713 */
2714 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002715
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002716 txn->status = http_err_codes[deny_status];
2717
Christopher Faulet0184ea72017-01-05 14:06:34 +01002718 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002719 req->analysers |= AN_REQ_HTTP_TARPIT;
2720 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2721 if (!req->analyse_exp)
2722 req->analyse_exp = tick_add(now_ms, 0);
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);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002729 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002730
2731 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002732
2733 /* Allow cookie logging
2734 */
2735 if (s->be->cookie_name || sess->fe->capture_name)
2736 manage_client_side_cookies(s, req);
2737
Willy Tarreau0b748332014-04-29 00:13:29 +02002738 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002739 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002740 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002741 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02002742 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +01002743 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002744 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002745 _HA_ATOMIC_ADD(&s->be->be_counters.denied_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->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002748 goto return_prx_cond;
2749
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002750 return_bad_req:
2751 /* We centralize bad requests processing here */
2752 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2753 /* we detected a parsing error. We want to archive this request
2754 * in the dedicated proxy area for later troubleshooting.
2755 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02002756 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002757 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002758
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002759 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002760 txn->req.msg_state = HTTP_MSG_ERROR;
2761 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002762 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002763
Olivier Houcharda798bf52019-03-08 18:52:00 +01002764 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002765 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002766 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002767
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002768 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002769 if (!(s->flags & SF_ERR_MASK))
2770 s->flags |= SF_ERR_PRXCOND;
2771 if (!(s->flags & SF_FINST_MASK))
2772 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002773
Christopher Faulet0184ea72017-01-05 14:06:34 +01002774 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002775 req->analyse_exp = TICK_ETERNITY;
2776 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002777
2778 return_prx_yield:
2779 channel_dont_connect(req);
2780 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002781}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002782
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002783/* This function performs all the processing enabled for the current request.
2784 * It returns 1 if the processing can continue on next analysers, or zero if it
2785 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002786 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002787 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002788int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002789{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002790 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002791 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002792 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02002793 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002794
Christopher Faulete0768eb2018-10-03 16:38:02 +02002795 if (IS_HTX_STRM(s))
2796 return htx_process_request(s, req, an_bit);
2797
Willy Tarreau655dce92009-11-08 13:10:58 +01002798 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002799 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002800 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002801 return 0;
2802 }
2803
Christopher Faulet45073512018-07-20 10:16:29 +02002804 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 +02002805 now_ms, __FUNCTION__,
2806 s,
2807 req,
2808 req->rex, req->wex,
2809 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002810 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002811 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002812
Willy Tarreau59234e92008-11-30 23:51:27 +01002813 /*
2814 * Right now, we know that we have processed the entire headers
2815 * and that unwanted requests have been filtered out. We can do
2816 * whatever we want with the remaining request. Also, now we
2817 * may have separate values for ->fe, ->be.
2818 */
Willy Tarreau06619262006-12-17 08:37:22 +01002819
Willy Tarreau59234e92008-11-30 23:51:27 +01002820 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002821 * If HTTP PROXY is set we simply get remote server address parsing
2822 * incoming request. Note that this requires that a connection is
2823 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01002824 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002825 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002826 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002827 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002828
Willy Tarreau9471b8c2013-12-15 13:31:35 +01002829 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002830 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002831 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002832 txn->req.msg_state = HTTP_MSG_ERROR;
2833 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002834 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002835 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002836
Willy Tarreaue7dff022015-04-03 01:14:29 +02002837 if (!(s->flags & SF_ERR_MASK))
2838 s->flags |= SF_ERR_RESOURCE;
2839 if (!(s->flags & SF_FINST_MASK))
2840 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002841
2842 return 0;
2843 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002844
Willy Tarreau6b952c82018-09-10 17:45:34 +02002845 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02002846 if (url2sa(ci_head(req) + msg->sl.rq.u,
2847 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02002848 &conn->addr.to, NULL) == -1)
2849 goto return_bad_req;
2850
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002851 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02002852 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
2853 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002854 * u_l characters by a single "/".
2855 */
2856 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002857 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002858 char *cur_end = cur_ptr + txn->req.sl.rq.l;
2859 int delta;
2860
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002861 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002862 http_msg_move_end(&txn->req, delta);
2863 cur_end += delta;
2864 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
2865 goto return_bad_req;
2866 }
2867 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002868 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002869 char *cur_end = cur_ptr + txn->req.sl.rq.l;
2870 int delta;
2871
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002872 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002873 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002874 http_msg_move_end(&txn->req, delta);
2875 cur_end += delta;
2876 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
2877 goto return_bad_req;
2878 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002879 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002880
Willy Tarreau59234e92008-11-30 23:51:27 +01002881 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002882 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002883 * Note that doing so might move headers in the request, but
2884 * the fields will stay coherent and the URI will not move.
2885 * This should only be performed in the backend.
2886 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002887 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002888 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002889
William Lallemanda73203e2012-03-12 12:48:57 +01002890 /* add unique-id if "header-unique-id" is specified */
2891
Thierry Fournierf4011dd2016-03-29 17:23:51 +02002892 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01002893 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002894 goto return_bad_req;
2895 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002896 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002897 }
William Lallemanda73203e2012-03-12 12:48:57 +01002898
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002899 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02002900 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01002901 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002902 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01002903 goto return_bad_req;
2904 }
2905
Cyril Bontéb21570a2009-11-29 20:04:48 +01002906 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002907 * 9: add X-Forwarded-For if either the frontend or the backend
2908 * asks for it.
2909 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002910 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02002911 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002912 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
2913 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
2914 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002915 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02002916 /* The header is set to be added only if none is present
2917 * and we found it, so don't do anything.
2918 */
2919 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002920 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002921 /* Add an X-Forwarded-For header unless the source IP is
2922 * in the 'except' network range.
2923 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002924 if ((!sess->fe->except_mask.s_addr ||
2925 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
2926 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01002927 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002928 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01002929 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002930 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002931 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002932 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002933
2934 /* Note: we rely on the backend to get the header name to be used for
2935 * x-forwarded-for, because the header is really meant for the backends.
2936 * However, if the backend did not specify any option, we have to rely
2937 * on the frontend's header name.
2938 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002939 if (s->be->fwdfor_hdr_len) {
2940 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002941 memcpy(trash.area,
2942 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002943 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002944 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002945 memcpy(trash.area,
2946 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002947 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002948 len += snprintf(trash.area + len,
2949 trash.size - len,
2950 ": %d.%d.%d.%d", pn[0], pn[1],
2951 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002952
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002953 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01002954 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002955 }
2956 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002957 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002958 /* FIXME: for the sake of completeness, we should also support
2959 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002960 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002961 int len;
2962 char pn[INET6_ADDRSTRLEN];
2963 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002964 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01002965 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002966
Willy Tarreau59234e92008-11-30 23:51:27 +01002967 /* Note: we rely on the backend to get the header name to be used for
2968 * x-forwarded-for, because the header is really meant for the backends.
2969 * However, if the backend did not specify any option, we have to rely
2970 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002971 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002972 if (s->be->fwdfor_hdr_len) {
2973 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002974 memcpy(trash.area, s->be->fwdfor_hdr_name,
2975 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01002976 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002977 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002978 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
2979 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002980 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002981 len += snprintf(trash.area + len, trash.size - len,
2982 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002983
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002984 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01002985 goto return_bad_req;
2986 }
2987 }
2988
2989 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02002990 * 10: add X-Original-To if either the frontend or the backend
2991 * asks for it.
2992 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002993 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02002994
2995 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002996 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02002997 /* Add an X-Original-To header unless the destination IP is
2998 * in the 'except' network range.
2999 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003000 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003001
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003002 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003003 ((!sess->fe->except_mask_to.s_addr ||
3004 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3005 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003006 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003007 (((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 +02003008 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003009 int len;
3010 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003011 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003012
3013 /* Note: we rely on the backend to get the header name to be used for
3014 * x-original-to, because the header is really meant for the backends.
3015 * However, if the backend did not specify any option, we have to rely
3016 * on the frontend's header name.
3017 */
3018 if (s->be->orgto_hdr_len) {
3019 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003020 memcpy(trash.area,
3021 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003022 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003023 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003024 memcpy(trash.area,
3025 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003026 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003027 len += snprintf(trash.area + len,
3028 trash.size - len,
3029 ": %d.%d.%d.%d", pn[0], pn[1],
3030 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003031
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003032 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003033 goto return_bad_req;
3034 }
3035 }
3036 }
3037
Willy Tarreau50fc7772012-11-11 22:19:57 +01003038 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3039 * If an "Upgrade" token is found, the header is left untouched in order not to have
3040 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3041 * "Upgrade" is present in the Connection header.
3042 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003043 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003044 unsigned int want_flags = 0;
3045
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003046 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003047 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003048 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003049 want_flags |= TX_CON_CLO_SET;
3050 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003051 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003052 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003053 want_flags |= TX_CON_KAL_SET;
3054 }
3055
3056 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003057 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003058 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003059
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003060
Willy Tarreau522d6c02009-12-06 18:49:18 +01003061 /* If we have no server assigned yet and we're balancing on url_param
3062 * with a POST request, we may be interested in checking the body for
3063 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003064 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003065 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +01003066 s->txn->meth == HTTP_METH_POST &&
3067 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003068 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003069 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003070 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003071 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003072
Christopher Fauletbe821b92017-03-30 11:21:53 +02003073 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3074 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +01003075
Christopher Fauletbe821b92017-03-30 11:21:53 +02003076 /* We expect some data from the client. Unless we know for sure
3077 * we already have a full request, we have to re-enable quick-ack
3078 * in case we previously disabled it, otherwise we might cause
3079 * the client to delay further data.
3080 */
3081 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletbe821b92017-03-30 11:21:53 +02003082 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003083 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau1a18b542018-12-11 16:37:42 +01003084 conn_set_quickack(cli_conn, 1);
Willy Tarreau03945942009-12-22 16:50:27 +01003085
Willy Tarreau59234e92008-11-30 23:51:27 +01003086 /*************************************************************
3087 * OK, that's finished for the headers. We have done what we *
3088 * could. Let's switch to the DATA state. *
3089 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003090 req->analyse_exp = TICK_ETERNITY;
3091 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003092
Willy Tarreau59234e92008-11-30 23:51:27 +01003093 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003094 /* OK let's go on with the BODY now */
3095 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003096
Willy Tarreau59234e92008-11-30 23:51:27 +01003097 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003098 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003099 /* we detected a parsing error. We want to archive this request
3100 * in the dedicated proxy area for later troubleshooting.
3101 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003102 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003103 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003104
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003105 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003106 txn->req.msg_state = HTTP_MSG_ERROR;
3107 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003108 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003109 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003110
Olivier Houcharda798bf52019-03-08 18:52:00 +01003111 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003112 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003113 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003114
Willy Tarreaue7dff022015-04-03 01:14:29 +02003115 if (!(s->flags & SF_ERR_MASK))
3116 s->flags |= SF_ERR_PRXCOND;
3117 if (!(s->flags & SF_FINST_MASK))
3118 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003119 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003120}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003121
Willy Tarreau60b85b02008-11-30 23:28:40 +01003122/* This function is an analyser which processes the HTTP tarpit. It always
3123 * returns zero, at the beginning because it prevents any other processing
3124 * from occurring, and at the end because it terminates the request.
3125 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003126int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003127{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003128 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003129
Christopher Faulete0768eb2018-10-03 16:38:02 +02003130 if (IS_HTX_STRM(s))
3131 return htx_process_tarpit(s, req, an_bit);
3132
Willy Tarreau60b85b02008-11-30 23:28:40 +01003133 /* This connection is being tarpitted. The CLIENT side has
3134 * already set the connect expiration date to the right
3135 * timeout. We just have to check that the client is still
3136 * there and that the timeout has not expired.
3137 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003138 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003139 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003140 !tick_is_expired(req->analyse_exp, now_ms))
3141 return 0;
3142
3143 /* We will set the queue timer to the time spent, just for
3144 * logging purposes. We fake a 500 server error, so that the
3145 * attacker will not suspect his connection has been tarpitted.
3146 * It will not cause trouble to the logs because we can exclude
3147 * the tarpitted connections by filtering on the 'PT' status flags.
3148 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003149 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3150
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003151 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003152 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003153
Christopher Faulet0184ea72017-01-05 14:06:34 +01003154 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003155 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003156
Willy Tarreaue7dff022015-04-03 01:14:29 +02003157 if (!(s->flags & SF_ERR_MASK))
3158 s->flags |= SF_ERR_PRXCOND;
3159 if (!(s->flags & SF_FINST_MASK))
3160 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003161 return 0;
3162}
3163
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003164/* This function is an analyser which waits for the HTTP request body. It waits
3165 * for either the buffer to be full, or the full advertised contents to have
3166 * reached the buffer. It must only be called after the standard HTTP request
3167 * processing has occurred, because it expects the request to be parsed and will
3168 * look for the Expect header. It may send a 100-Continue interim response. It
3169 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3170 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3171 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003172 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003173int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003174{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003175 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003176 struct http_txn *txn = s->txn;
3177 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003178
Christopher Faulete0768eb2018-10-03 16:38:02 +02003179 if (IS_HTX_STRM(s))
3180 return htx_wait_for_request_body(s, req, an_bit);
3181
Christopher Faulet87451fd2019-03-01 11:16:34 +01003182 if (msg->msg_state < HTTP_MSG_BODY)
3183 goto missing_data;
3184
Willy Tarreaud34af782008-11-30 23:36:37 +01003185 /* We have to parse the HTTP request body to find any required data.
3186 * "balance url_param check_post" should have been the only way to get
3187 * into this. We were brought here after HTTP header analysis, so all
3188 * related structures are ready.
3189 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003190 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Christopher Faulet87451fd2019-03-01 11:16:34 +01003191 http_handle_expect_hdr(s, req, msg);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003192
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003193 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003194 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003195 * must save the body in msg->next because it survives buffer
3196 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003197 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003198 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003199
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003200 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003201 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3202 else
3203 msg->msg_state = HTTP_MSG_DATA;
3204 }
3205
Willy Tarreau890988f2014-04-10 11:59:33 +02003206 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3207 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003208 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003209 goto missing_data;
3210
3211 /* OK we have everything we need now */
3212 goto http_end;
3213 }
3214
3215 /* OK here we're parsing a chunked-encoded message */
3216
Willy Tarreau522d6c02009-12-06 18:49:18 +01003217 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003218 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003219 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003220 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003221 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003222 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003223 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003224
Willy Tarreau115acb92009-12-26 13:56:06 +01003225 if (!ret)
3226 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003227 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003228 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003229 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003230 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003231 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003232 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003233 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003234
3235 msg->chunk_len = chunk;
3236 msg->body_len += chunk;
3237
3238 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003239 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003240 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003241 }
3242
Willy Tarreaud98cf932009-12-27 22:54:55 +01003243 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003244 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3245 * for at least a whole chunk or the whole content length bytes after
3246 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003247 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003248 if (msg->msg_state == HTTP_MSG_TRAILERS)
3249 goto http_end;
3250
Willy Tarreaue115b492015-05-01 23:05:14 +02003251 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003252 goto http_end;
3253
3254 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003255 /* we get here if we need to wait for more data. If the buffer is full,
3256 * we have the maximum we can expect.
3257 */
Willy Tarreau23752332018-06-15 14:54:53 +02003258 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003259 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003260
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003261 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003262 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003263 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003264
Willy Tarreaue7dff022015-04-03 01:14:29 +02003265 if (!(s->flags & SF_ERR_MASK))
3266 s->flags |= SF_ERR_CLITO;
3267 if (!(s->flags & SF_FINST_MASK))
3268 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003269 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003270 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003271
3272 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003273 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003274 /* Not enough data. We'll re-use the http-request
3275 * timeout here. Ideally, we should set the timeout
3276 * relative to the accept() date. We just set the
3277 * request timeout once at the beginning of the
3278 * request.
3279 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003280 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003281 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003282 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003283 return 0;
3284 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003285
3286 http_end:
3287 /* The situation will not evolve, so let's give up on the analysis. */
3288 s->logs.tv_request = now; /* update the request timer to reflect full request */
3289 req->analysers &= ~an_bit;
3290 req->analyse_exp = TICK_ETERNITY;
3291 return 1;
3292
3293 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003294 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003295 txn->req.msg_state = HTTP_MSG_ERROR;
3296 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003297 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003298
Willy Tarreaue7dff022015-04-03 01:14:29 +02003299 if (!(s->flags & SF_ERR_MASK))
3300 s->flags |= SF_ERR_PRXCOND;
3301 if (!(s->flags & SF_FINST_MASK))
3302 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003303
Willy Tarreau522d6c02009-12-06 18:49:18 +01003304 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003305 req->analysers &= AN_REQ_FLT_END;
Olivier Houcharda798bf52019-03-08 18:52:00 +01003306 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003307 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003308 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003309 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003310}
3311
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003312/* send a server's name with an outgoing request over an established connection.
3313 * Note: this function is designed to be called once the request has been scheduled
3314 * for being forwarded. This is the reason why it rewinds the buffer before
3315 * proceeding.
3316 */
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003317int http_send_name_header(struct stream *s, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003318
3319 struct hdr_ctx ctx;
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003320 struct http_txn *txn = s->txn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003321 char *hdr_name = be->server_id_hdr_name;
3322 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003323 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003324 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003325 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003326
Christopher Faulet64159df2018-10-24 21:15:35 +02003327 if (IS_HTX_STRM(s))
3328 return htx_send_name_header(s, be, srv_name);
William Lallemandd9e90662012-01-30 17:27:17 +01003329 ctx.idx = 0;
3330
Willy Tarreau211cdec2014-04-17 20:18:08 +02003331 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003332 if (old_o) {
3333 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003334 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003335 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003336 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003337 }
3338
Willy Tarreauf37954d2018-06-15 18:31:02 +02003339 old_i = ci_data(chn);
3340 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 -05003341 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003342 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003343 }
3344
3345 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003346 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003347 memcpy(hdr_val, hdr_name, hdr_name_len);
3348 hdr_val += hdr_name_len;
3349 *hdr_val++ = ':';
3350 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003351 hdr_val += strlcpy2(hdr_val, srv_name,
3352 trash.area + trash.size - hdr_val);
3353 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3354 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003355
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003356 if (old_o) {
3357 /* If this was a forwarded request, we must readjust the amount of
3358 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003359 * variations. Note that the current state is >= HTTP_MSG_BODY,
3360 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003361 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003362 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003363 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003364 txn->req.next -= old_o;
3365 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003366 }
3367
Mark Lamourinec2247f02012-01-04 13:02:01 -05003368 return 0;
3369}
3370
Willy Tarreau610ecce2010-01-04 21:15:02 +01003371/* Terminate current transaction and prepare a new one. This is very tricky
3372 * right now but it works.
3373 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003374void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003375{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003376 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003377 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003378 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003379 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003380 struct connection *srv_conn;
3381 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003382 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003383
Willy Tarreau610ecce2010-01-04 21:15:02 +01003384 /* FIXME: We need a more portable way of releasing a backend's and a
3385 * server's connections. We need a safer way to reinitialize buffer
3386 * flags. We also need a more accurate method for computing per-request
3387 * data.
3388 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003389 cs = objt_cs(s->si[1].end);
3390 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003391
Willy Tarreau4213a112013-12-15 10:25:42 +01003392 /* unless we're doing keep-alive, we want to quickly close the connection
3393 * to the server.
3394 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003395 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003396 !si_conn_ready(&s->si[1]) || !srv_conn->owner) {
Willy Tarreau350f4872014-11-28 14:42:25 +01003397 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3398 si_shutr(&s->si[1]);
3399 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003400 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003401
Willy Tarreaue7dff022015-04-03 01:14:29 +02003402 if (s->flags & SF_BE_ASSIGNED) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003403 _HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003404 if (unlikely(s->srv_conn))
3405 sess_change_server(s, NULL);
3406 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003407
3408 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003409 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003410
Willy Tarreaueee5b512015-04-03 23:46:31 +02003411 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003412 int n;
3413
Willy Tarreaueee5b512015-04-03 23:46:31 +02003414 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003415 if (n < 1 || n > 5)
3416 n = 0;
3417
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003418 if (fe->mode == PR_MODE_HTTP) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003419 _HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003420 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003421 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003422 (be->mode == PR_MODE_HTTP)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003423 _HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3424 _HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003425 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003426 }
3427
3428 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003429 s->logs.bytes_in -= ci_data(&s->req);
3430 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003431
Willy Tarreau66425e32018-07-25 06:55:12 +02003432 /* we may need to know the position in the queue */
3433 pendconn_free(s);
3434
Willy Tarreau610ecce2010-01-04 21:15:02 +01003435 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003436 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003437 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003438 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003439 s->do_log(s);
3440 }
3441
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003442 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003443 stream_stop_content_counters(s);
3444 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003445
Willy Tarreau9efd7452018-05-31 14:48:54 +02003446 /* reset the profiling counter */
3447 s->task->calls = 0;
3448 s->task->cpu_time = 0;
3449 s->task->lat_time = 0;
3450 s->task->call_date = (profiling & HA_PROF_TASKS) ? now_mono_time() : 0;
3451
Willy Tarreau610ecce2010-01-04 21:15:02 +01003452 s->logs.accept_date = date; /* user-visible date for logging */
3453 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003454 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3455 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003456 tv_zero(&s->logs.tv_request);
3457 s->logs.t_queue = -1;
3458 s->logs.t_connect = -1;
3459 s->logs.t_data = -1;
3460 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003461 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3462 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003463
Willy Tarreauf37954d2018-06-15 18:31:02 +02003464 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3465 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003466
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003467 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003468 if (s->flags & SF_CURR_SESS) {
3469 s->flags &= ~SF_CURR_SESS;
Olivier Houcharda798bf52019-03-08 18:52:00 +01003470 _HA_ATOMIC_SUB(&__objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003471 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003472 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003473 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003474 }
3475
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003476 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003477
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003478
3479 /* If we're doing keepalive, first call the mux detach() method
3480 * to let it know we want to detach without freing the connection.
3481 * We then can call si_release_endpoint() to destroy the conn_stream
Willy Tarreau4213a112013-12-15 10:25:42 +01003482 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003483 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houcharda70e1762018-12-13 18:01:00 +01003484 !si_conn_ready(&s->si[1]) ||
3485 (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 +02003486 srv_conn = NULL;
Olivier Houchard985f1392018-11-30 17:31:52 +01003487 else if (!srv_conn->owner) {
3488 srv_conn->owner = s->sess;
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003489 /* Add it unconditionally to the session list, it'll be removed
3490 * later if needed by session_check_idle_conn(), once we'll
3491 * have released the endpoint and know if it no longer has
3492 * attached streams, and so an idling connection
3493 */
Olivier Houchard351411f2018-12-27 17:20:54 +01003494 if (!session_add_conn(s->sess, srv_conn, s->target)) {
3495 srv_conn->owner = NULL;
3496 /* Try to add the connection to the server idle list.
3497 * If it fails, as the connection no longer has an
3498 * owner, it will be destroy later by
3499 * si_release_endpoint(), anyway
3500 */
3501 srv_add_to_idle_list(objt_server(srv_conn->target), srv_conn);
3502 srv_conn = NULL;
3503
3504 }
Olivier Houchard985f1392018-11-30 17:31:52 +01003505 }
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003506 si_release_endpoint(&s->si[1]);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003507 if (srv_conn && srv_conn->owner == s->sess) {
3508 if (session_check_idle_conn(s->sess, srv_conn) != 0)
3509 srv_conn = NULL;
3510 }
3511
Willy Tarreau4213a112013-12-15 10:25:42 +01003512
Willy Tarreau350f4872014-11-28 14:42:25 +01003513 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3514 s->si[1].err_type = SI_ET_NONE;
3515 s->si[1].conn_retries = 0; /* used for logging too */
3516 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003517 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 +01003518 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 +02003519 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 +02003520 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3521 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3522 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003523
Patrick Hemmere3faf022018-08-22 10:02:00 -04003524 hlua_ctx_destroy(s->hlua);
3525 s->hlua = NULL;
3526
Willy Tarreaueee5b512015-04-03 23:46:31 +02003527 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003528 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003529 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003530
3531 if (prev_status == 401 || prev_status == 407) {
3532 /* In HTTP keep-alive mode, if we receive a 401, we still have
3533 * a chance of being able to send the visitor again to the same
3534 * server over the same connection. This is required by some
3535 * broken protocols such as NTLM, and anyway whenever there is
3536 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003537 * it's better to do it (at least it helps with debugging), at
3538 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003539 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003540 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003541 }
3542
Willy Tarreau53f96852016-02-02 18:50:47 +01003543 /* Never ever allow to reuse a connection from a non-reuse backend */
3544 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3545 srv_conn->flags |= CO_FL_PRIVATE;
3546
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003547 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003548 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003549
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003550 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003551 s->req.flags |= CF_NEVER_WAIT;
3552 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003553 }
3554
Willy Tarreau714ea782015-11-25 20:11:11 +01003555 /* we're removing the analysers, we MUST re-enable events detection.
3556 * We don't enable close on the response channel since it's either
3557 * already closed, or in keep-alive with an idle connection handler.
3558 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003559 channel_auto_read(&s->req);
3560 channel_auto_close(&s->req);
3561 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003562
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003563 /* we're in keep-alive with an idle connection, monitor it if not already done */
3564 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003565 srv = objt_server(srv_conn->target);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003566 if (srv) {
3567 if (srv_conn->flags & CO_FL_PRIVATE)
3568 LIST_ADD(&srv->priv_conns[tid], &srv_conn->list);
3569 else if (prev_flags & TX_NOT_FIRST)
3570 /* note: we check the request, not the connection, but
3571 * this is valid for strategies SAFE and AGGR, and in
3572 * case of ALWS, we don't care anyway.
3573 */
3574 LIST_ADD(&srv->safe_conns[tid], &srv_conn->list);
3575 else
3576 LIST_ADD(&srv->idle_conns[tid], &srv_conn->list);
3577 }
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003578 }
Christopher Faulete6006242017-03-10 11:52:44 +01003579 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3580 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003581}
3582
3583
3584/* This function updates the request state machine according to the response
3585 * state machine and buffer flags. It returns 1 if it changes anything (flag
3586 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3587 * it is only used to find when a request/response couple is complete. Both
3588 * this function and its equivalent should loop until both return zero. It
3589 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3590 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003591int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003592{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003593 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003594 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003595 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003596 unsigned int old_state = txn->req.msg_state;
3597
Christopher Faulet4be98032017-07-18 10:48:24 +02003598 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003599 return 0;
3600
3601 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003602 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003603 * We can shut the read side unless we want to abort_on_close,
3604 * or we have a POST request. The issue with POST requests is
3605 * that some browsers still send a CRLF after the request, and
3606 * this CRLF must be read so that it does not remain in the kernel
3607 * buffers, otherwise a close could cause an RST on some systems
3608 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003609 * Note that if we're using keep-alive on the client side, we'd
3610 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003611 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003612 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003613 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003614 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3615 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003616 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3617 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003618 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003619 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003620
Willy Tarreau40f151a2012-12-20 12:10:09 +01003621 /* if the server closes the connection, we want to immediately react
3622 * and close the socket to save packets and syscalls.
3623 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003624 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003625
Willy Tarreau7f876a12015-11-18 11:59:55 +01003626 /* In any case we've finished parsing the request so we must
3627 * disable Nagle when sending data because 1) we're not going
3628 * to shut this side, and 2) the server is waiting for us to
3629 * send pending data.
3630 */
3631 chn->flags |= CF_NEVER_WAIT;
3632
Willy Tarreau610ecce2010-01-04 21:15:02 +01003633 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3634 goto wait_other_side;
3635
3636 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3637 /* The server has not finished to respond, so we
3638 * don't want to move in order not to upset it.
3639 */
3640 goto wait_other_side;
3641 }
3642
Willy Tarreau610ecce2010-01-04 21:15:02 +01003643 /* When we get here, it means that both the request and the
3644 * response have finished receiving. Depending on the connection
3645 * mode, we'll have to wait for the last bytes to leave in either
3646 * direction, and sometimes for a close to be effective.
3647 */
3648
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003649 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3650 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003651 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3652 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003653 }
3654 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3655 /* Option forceclose is set, or either side wants to close,
3656 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003657 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003658 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003659 *
3660 * However, there is an exception if the response
3661 * length is undefined. In this case, we need to wait
3662 * the close from the server. The response will be
3663 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003664 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003665 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3666 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3667 goto check_channel_flags;
3668
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003669 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3670 channel_shutr_now(chn);
3671 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003672 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003673 }
3674 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003675 /* The last possible modes are keep-alive and tunnel. Tunnel mode
3676 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003677 */
Willy Tarreau4213a112013-12-15 10:25:42 +01003678 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
3679 channel_auto_read(chn);
3680 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003681 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003682 }
3683
Christopher Faulet4be98032017-07-18 10:48:24 +02003684 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003685 }
3686
3687 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3688 http_msg_closing:
3689 /* nothing else to forward, just waiting for the output buffer
3690 * to be empty and for the shutw_now to take effect.
3691 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003692 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003693 txn->req.msg_state = HTTP_MSG_CLOSED;
3694 goto http_msg_closed;
3695 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003696 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003697 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003698 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003699 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003700 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003701 }
3702
3703 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3704 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01003705 /* if we don't know whether the server will close, we need to hard close */
3706 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
3707 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3708
Willy Tarreau3988d932013-12-27 23:03:08 +01003709 /* see above in MSG_DONE why we only do this in these states */
3710 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3711 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003712 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3713 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01003714 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003715 goto wait_other_side;
3716 }
3717
Christopher Faulet4be98032017-07-18 10:48:24 +02003718 check_channel_flags:
3719 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
3720 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
3721 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02003722 txn->req.msg_state = HTTP_MSG_CLOSING;
3723 goto http_msg_closing;
3724 }
3725
3726
Willy Tarreau610ecce2010-01-04 21:15:02 +01003727 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003728 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003729}
3730
3731
3732/* This function updates the response state machine according to the request
3733 * state machine and buffer flags. It returns 1 if it changes anything (flag
3734 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3735 * it is only used to find when a request/response couple is complete. Both
3736 * this function and its equivalent should loop until both return zero. It
3737 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3738 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003739int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003740{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003741 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003742 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003743 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003744 unsigned int old_state = txn->rsp.msg_state;
3745
Christopher Faulet4be98032017-07-18 10:48:24 +02003746 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003747 return 0;
3748
3749 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3750 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003751 * still monitor the server connection for a possible close
3752 * while the request is being uploaded, so we don't disable
3753 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003754 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003755 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003756
3757 if (txn->req.msg_state == HTTP_MSG_ERROR)
3758 goto wait_other_side;
3759
3760 if (txn->req.msg_state < HTTP_MSG_DONE) {
3761 /* The client seems to still be sending data, probably
3762 * because we got an error response during an upload.
3763 * We have the choice of either breaking the connection
3764 * or letting it pass through. Let's do the later.
3765 */
3766 goto wait_other_side;
3767 }
3768
Willy Tarreau610ecce2010-01-04 21:15:02 +01003769 /* When we get here, it means that both the request and the
3770 * response have finished receiving. Depending on the connection
3771 * mode, we'll have to wait for the last bytes to leave in either
3772 * direction, and sometimes for a close to be effective.
3773 */
3774
3775 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3776 /* Server-close mode : shut read and wait for the request
3777 * side to close its output buffer. The caller will detect
3778 * when we're in DONE and the other is in CLOSED and will
3779 * catch that for the final cleanup.
3780 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003781 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
3782 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003783 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003784 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3785 /* Option forceclose is set, or either side wants to close,
3786 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003787 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003788 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003789 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01003790 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003791 channel_shutr_now(chn);
3792 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003793 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003794 }
3795 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003796 /* The last possible modes are keep-alive and tunnel. Tunnel will
3797 * need to forward remaining data. Keep-alive will need to monitor
3798 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003799 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003800 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02003801 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01003802 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
3803 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003804 }
3805
Christopher Faulet4be98032017-07-18 10:48:24 +02003806 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003807 }
3808
3809 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3810 http_msg_closing:
3811 /* nothing else to forward, just waiting for the output buffer
3812 * to be empty and for the shutw_now to take effect.
3813 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003814 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003815 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3816 goto http_msg_closed;
3817 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003818 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02003819 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003820 txn->rsp.msg_state = HTTP_MSG_ERROR;
Olivier Houcharda798bf52019-03-08 18:52:00 +01003821 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003822 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01003823 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003824 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003825 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003826 }
3827
3828 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3829 http_msg_closed:
3830 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01003831 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003832 channel_auto_close(chn);
3833 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003834 goto wait_other_side;
3835 }
3836
Christopher Faulet4be98032017-07-18 10:48:24 +02003837 check_channel_flags:
3838 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
3839 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
3840 /* if we've just closed an output, let's switch */
3841 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3842 goto http_msg_closing;
3843 }
3844
Willy Tarreau610ecce2010-01-04 21:15:02 +01003845 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02003846 /* We force the response to leave immediately if we're waiting for the
3847 * other side, since there is no pending shutdown to push it out.
3848 */
3849 if (!channel_is_empty(chn))
3850 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003851 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003852}
3853
3854
Christopher Faulet894da4c2017-07-18 11:29:07 +02003855/* Resync the request and response state machines. */
3856void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003857{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003858 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02003859#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01003860 int old_req_state = txn->req.msg_state;
3861 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02003862#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01003863
Willy Tarreau610ecce2010-01-04 21:15:02 +01003864 http_sync_req_state(s);
3865 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003866 if (!http_sync_res_state(s))
3867 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003868 if (!http_sync_req_state(s))
3869 break;
3870 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02003871
Christopher Faulet894da4c2017-07-18 11:29:07 +02003872 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
3873 "req->analysers=0x%08x res->analysers=0x%08x\n",
3874 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02003875 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
3876 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02003877 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02003878
3879
Willy Tarreau610ecce2010-01-04 21:15:02 +01003880 /* OK, both state machines agree on a compatible state.
3881 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01003882 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3883 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02003884 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
3885 * means we must abort the request.
3886 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
3887 * corresponding channel.
3888 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
3889 * on the response with server-close mode means we've completed one
3890 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003891 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02003892 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3893 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01003894 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003895 channel_auto_close(&s->req);
3896 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003897 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003898 channel_auto_close(&s->res);
3899 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003900 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02003901 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3902 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01003903 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01003904 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003905 channel_auto_close(&s->res);
3906 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003907 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003908 channel_abort(&s->req);
3909 channel_auto_close(&s->req);
3910 channel_auto_read(&s->req);
3911 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003912 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02003913 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3914 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3915 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3916 s->req.analysers &= AN_REQ_FLT_END;
3917 if (HAS_REQ_DATA_FILTERS(s))
3918 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
3919 }
3920 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3921 s->res.analysers &= AN_RES_FLT_END;
3922 if (HAS_RSP_DATA_FILTERS(s))
3923 s->res.analysers |= AN_RES_FLT_XFER_DATA;
3924 }
3925 channel_auto_close(&s->req);
3926 channel_auto_read(&s->req);
3927 channel_auto_close(&s->res);
3928 channel_auto_read(&s->res);
3929 }
Willy Tarreau4213a112013-12-15 10:25:42 +01003930 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
3931 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003932 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01003933 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3934 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3935 /* server-close/keep-alive: terminate this transaction,
3936 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02003937 * a fresh-new transaction, but only once we're sure there's
3938 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02003939 * another request. They must not hold any pending output data
3940 * and the response buffer must realigned
3941 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01003942 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003943 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02003944 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02003945 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02003946 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02003947 else {
3948 s->req.analysers = AN_REQ_FLT_END;
3949 s->res.analysers = AN_RES_FLT_END;
3950 txn->flags |= TX_WAIT_CLEANUP;
3951 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003952 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003953}
3954
Willy Tarreaud98cf932009-12-27 22:54:55 +01003955/* This function is an analyser which forwards request body (including chunk
3956 * sizes if any). It is called as soon as we must forward, even if we forward
3957 * zero byte. The only situation where it must not be called is when we're in
3958 * tunnel mode and we want to forward till the close. It's used both to forward
3959 * remaining data and to resync after end of body. It expects the msg_state to
3960 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02003961 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01003962 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02003963 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003964 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003965int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01003966{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003967 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003968 struct http_txn *txn = s->txn;
3969 struct http_msg *msg = &s->txn->req;
Christopher Faulet93e02d82019-03-08 14:18:50 +01003970 short status = 0;
Christopher Faulet3e344292015-11-24 16:24:13 +01003971 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003972
Christopher Faulete0768eb2018-10-03 16:38:02 +02003973 if (IS_HTX_STRM(s))
3974 return htx_request_forward_body(s, req, an_bit);
3975
Christopher Faulet45073512018-07-20 10:16:29 +02003976 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 +02003977 now_ms, __FUNCTION__,
3978 s,
3979 req,
3980 req->rex, req->wex,
3981 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003982 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02003983 req->analysers);
3984
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003985 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3986 return 0;
3987
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003988 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003989 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02003990 /* Output closed while we were sending data. We must abort and
3991 * wake the other side up.
3992 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003993 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02003994 msg->msg_state = HTTP_MSG_ERROR;
3995 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01003996 }
3997
Willy Tarreaud98cf932009-12-27 22:54:55 +01003998 /* Note that we don't have to send 100-continue back because we don't
3999 * need the data to complete our job, and it's up to the server to
4000 * decide whether to return 100, 417 or anything else in return of
4001 * an "Expect: 100-continue" header.
4002 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004003 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004004 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4005 ? HTTP_MSG_CHUNK_SIZE
4006 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004007
4008 /* TODO/filters: when http-buffer-request option is set or if a
4009 * rule on url_param exists, the first chunk size could be
4010 * already parsed. In that case, msg->next is after the chunk
4011 * size (including the CRLF after the size). So this case should
4012 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004013 }
4014
Willy Tarreau7ba23542014-04-17 21:50:00 +02004015 /* Some post-connect processing might want us to refrain from starting to
4016 * forward data. Currently, the only reason for this is "balance url_param"
4017 * whichs need to parse/process the request after we've enabled forwarding.
4018 */
4019 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004020 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004021 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004022 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004023 channel_dont_close(req); /* don't fail on early shutr */
4024 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004025 }
4026 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4027 }
4028
Willy Tarreau80a92c02014-03-12 10:41:13 +01004029 /* in most states, we should abort in case of early close */
4030 channel_auto_close(req);
4031
Willy Tarreauefdf0942014-04-24 20:08:57 +02004032 if (req->to_forward) {
4033 /* We can't process the buffer's contents yet */
4034 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004035 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004036 }
4037
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004038 if (msg->msg_state < HTTP_MSG_DONE) {
4039 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4040 ? http_msg_forward_chunked_body(s, msg)
4041 : http_msg_forward_body(s, msg));
4042 if (!ret)
4043 goto missing_data_or_waiting;
4044 if (ret < 0)
4045 goto return_bad_req;
4046 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004047
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004048 /* other states, DONE...TUNNEL */
4049 /* we don't want to forward closes on DONE except in tunnel mode. */
4050 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4051 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004052
Christopher Faulet894da4c2017-07-18 11:29:07 +02004053 http_resync_states(s);
4054 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004055 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4056 if (req->flags & CF_SHUTW) {
4057 /* request errors are most likely due to the
4058 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01004059 goto return_srv_abort;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004060 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004061 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004062 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004063 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004064 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004065 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004066 }
4067
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004068 /* If "option abortonclose" is set on the backend, we want to monitor
4069 * the client's connection and forward any shutdown notification to the
4070 * server, which will decide whether to close or to go on processing the
4071 * request. We only do that in tunnel mode, and not in other modes since
4072 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004073 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004074 channel_auto_read(req);
4075 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4076 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4077 s->si[1].flags |= SI_FL_NOLINGER;
4078 channel_auto_close(req);
4079 }
4080 else if (s->txn->meth == HTTP_METH_POST) {
4081 /* POST requests may require to read extra CRLF sent by broken
4082 * browsers and which could cause an RST to be sent upon close
4083 * on some systems (eg: Linux). */
4084 channel_auto_read(req);
4085 }
4086 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004087
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004088 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004089 /* stop waiting for data if the input is closed before the end */
Christopher Faulet93e02d82019-03-08 14:18:50 +01004090 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
4091 goto return_cli_abort;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004092
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004093 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004094 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004095 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01004096 goto return_srv_abort;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004097
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004098 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004099 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004100 * And when content-length is used, we never want to let the possible
4101 * shutdown be forwarded to the other side, as the state machine will
4102 * take care of it once the client responds. It's also important to
4103 * prevent TIME_WAITs from accumulating on the backend side, and for
4104 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004105 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004106 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004107 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004108
Willy Tarreau5c620922011-05-11 19:56:11 +02004109 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004110 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004111 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004112 * modes are already handled by the stream sock layer. We must not do
4113 * this in content-length mode because it could present the MSG_MORE
4114 * flag with the last block of forwarded data, which would cause an
4115 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004116 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004117 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004118 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004119
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004120 return 0;
4121
Christopher Faulet93e02d82019-03-08 14:18:50 +01004122 return_cli_abort:
4123 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4124 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
4125 if (objt_server(s->target))
4126 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
4127 if (!(s->flags & SF_ERR_MASK))
4128 s->flags |= SF_ERR_CLICL;
4129 status = 400;
4130 goto return_error;
4131
4132 return_srv_abort:
4133 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4134 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
4135 if (objt_server(s->target))
4136 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
4137 if (!(s->flags & SF_ERR_MASK))
4138 s->flags |= SF_ERR_SRVCL;
4139 status = 502;
4140 goto return_error;
4141
4142 return_bad_req: /* let's centralize all bad requests */
Olivier Houcharda798bf52019-03-08 18:52:00 +01004143 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004144 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01004145 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02004146 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01004147 s->flags |= SF_ERR_CLICL;
4148 status = 400;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004149
Christopher Faulet93e02d82019-03-08 14:18:50 +01004150 return_error:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004151 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004152 txn->req.msg_state = HTTP_MSG_ERROR;
Christopher Faulet93e02d82019-03-08 14:18:50 +01004153 if (txn->status > 0) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004154 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004155 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004156 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01004157 txn->status = status;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004158 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004159 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004160 req->analysers &= AN_REQ_FLT_END;
4161 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Faulet93e02d82019-03-08 14:18:50 +01004162 if (!(s->flags & SF_FINST_MASK))
4163 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004164 return 0;
4165}
4166
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004167/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4168 * processing can continue on next analysers, or zero if it either needs more
4169 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004170 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004171 * when it has nothing left to do, and may remove any analyser when it wants to
4172 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004173 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004174int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004175{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004176 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004177 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004178 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004179 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004180 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004181 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004182 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004183 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004184
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004185 srv_conn = cs_conn(objt_cs(s->si[1].end));
4186
Christopher Faulete0768eb2018-10-03 16:38:02 +02004187 if (IS_HTX_STRM(s))
4188 return htx_wait_for_response(s, rep, an_bit);
4189
Christopher Faulet45073512018-07-20 10:16:29 +02004190 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 +02004191 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004192 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004193 rep,
4194 rep->rex, rep->wex,
4195 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004196 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004197 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004198
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004199 /*
4200 * Now parse the partial (or complete) lines.
4201 * We will check the response syntax, and also join multi-line
4202 * headers. An index of all the lines will be elaborated while
4203 * parsing.
4204 *
4205 * For the parsing, we use a 28 states FSM.
4206 *
4207 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004208 * ci_head(rep) = beginning of response
4209 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4210 * ci_tail(rep) = end of input data
4211 * msg->eol = end of current header or line (LF or CRLF)
4212 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004213 */
4214
Willy Tarreau628c40c2014-04-24 19:11:26 +02004215 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004216 /* There's a protected area at the end of the buffer for rewriting
4217 * purposes. We don't want to start to parse the request if the
4218 * protected area is affected, because we may have to move processed
4219 * data later, which is much more complicated.
4220 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004221 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004222 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004223 /* some data has still not left the buffer, wake us once that's done */
4224 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4225 goto abort_response;
4226 channel_dont_close(rep);
4227 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004228 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004229 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004230 }
4231
Willy Tarreau188e2302018-06-15 11:11:53 +02004232 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004233 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004234 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004235
Willy Tarreauf37954d2018-06-15 18:31:02 +02004236 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004237 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004238 }
4239
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004240 /* 1: we might have to print this header in debug mode */
4241 if (unlikely((global.mode & MODE_DEBUG) &&
4242 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004243 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004244 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004245
Willy Tarreauf37954d2018-06-15 18:31:02 +02004246 sol = ci_head(rep);
4247 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004248 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004249
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004250 sol += hdr_idx_first_pos(&txn->hdr_idx);
4251 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004252
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004253 while (cur_idx) {
4254 eol = sol + txn->hdr_idx.v[cur_idx].len;
4255 debug_hdr("srvhdr", s, sol, eol);
4256 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4257 cur_idx = txn->hdr_idx.v[cur_idx].next;
4258 }
4259 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004260
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004261 /*
4262 * Now we quickly check if we have found a full valid response.
4263 * If not so, we check the FD and buffer states before leaving.
4264 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004265 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004266 * responses are checked first.
4267 *
4268 * Depending on whether the client is still there or not, we
4269 * may send an error response back or not. Note that normally
4270 * we should only check for HTTP status there, and check I/O
4271 * errors somewhere else.
4272 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004273
Willy Tarreau655dce92009-11-08 13:10:58 +01004274 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004275 /* Invalid response */
4276 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4277 /* we detected a parsing error. We want to archive this response
4278 * in the dedicated proxy area for later troubleshooting.
4279 */
4280 hdr_response_bad:
4281 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004282 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004283
Olivier Houcharda798bf52019-03-08 18:52:00 +01004284 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004285 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004286 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004287 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004288 }
Willy Tarreau64648412010-03-05 10:41:54 +01004289 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004290 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004291 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004292 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004293 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004294 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004295 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004296
Willy Tarreaue7dff022015-04-03 01:14:29 +02004297 if (!(s->flags & SF_ERR_MASK))
4298 s->flags |= SF_ERR_PRXCOND;
4299 if (!(s->flags & SF_FINST_MASK))
4300 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004301
4302 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004303 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004304
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004305 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004306 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004307 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004308 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004309 goto hdr_response_bad;
4310 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004311
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004312 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004313 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004314 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004315 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004316 else if (txn->flags & TX_NOT_FIRST)
4317 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004318
Olivier Houcharda798bf52019-03-08 18:52:00 +01004319 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004320 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004321 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004322 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004323 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004324
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004325 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004326 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004327 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004328
4329 /* Check to see if the server refused the early data.
4330 * If so, just send a 425
4331 */
4332 if (objt_cs(s->si[1].end)) {
4333 struct connection *conn = objt_cs(s->si[1].end)->conn;
4334
4335 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4336 txn->status = 425;
4337 }
4338
Willy Tarreau350f4872014-11-28 14:42:25 +01004339 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004340 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004341 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004342
Willy Tarreaue7dff022015-04-03 01:14:29 +02004343 if (!(s->flags & SF_ERR_MASK))
4344 s->flags |= SF_ERR_SRVCL;
4345 if (!(s->flags & SF_FINST_MASK))
4346 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004347 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004348 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004349
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004350 /* read timeout : return a 504 to the client. */
4351 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004352 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004353 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004354
Olivier Houcharda798bf52019-03-08 18:52:00 +01004355 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004356 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004357 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004358 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004359 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004360
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004361 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004362 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004363 txn->status = 504;
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 Tarreau4076a152009-04-02 15:18:36 +02004367
Willy Tarreaue7dff022015-04-03 01:14:29 +02004368 if (!(s->flags & SF_ERR_MASK))
4369 s->flags |= SF_ERR_SRVTO;
4370 if (!(s->flags & SF_FINST_MASK))
4371 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004372 return 0;
4373 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004374
Willy Tarreauf003d372012-11-26 13:35:37 +01004375 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004376 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004377 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4378 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004379 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004380 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004381
Christopher Faulet0184ea72017-01-05 14:06:34 +01004382 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004383 channel_auto_close(rep);
4384
4385 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004386 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004387 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004388
Willy Tarreaue7dff022015-04-03 01:14:29 +02004389 if (!(s->flags & SF_ERR_MASK))
4390 s->flags |= SF_ERR_CLICL;
4391 if (!(s->flags & SF_FINST_MASK))
4392 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004393
Willy Tarreau87b09662015-04-03 00:22:06 +02004394 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004395 return 0;
4396 }
4397
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004398 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004399 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004400 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004401 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004402 else if (txn->flags & TX_NOT_FIRST)
4403 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004404
Olivier Houcharda798bf52019-03-08 18:52:00 +01004405 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004406 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004407 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004408 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004409 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004410
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004411 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004412 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004413 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004414 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004415 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004416 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004417
Willy Tarreaue7dff022015-04-03 01:14:29 +02004418 if (!(s->flags & SF_ERR_MASK))
4419 s->flags |= SF_ERR_SRVCL;
4420 if (!(s->flags & SF_FINST_MASK))
4421 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004422 return 0;
4423 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004424
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004425 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004426 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004427 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004428 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004429 else if (txn->flags & TX_NOT_FIRST)
4430 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004431
Olivier Houcharda798bf52019-03-08 18:52:00 +01004432 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004433 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004434 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004435
Willy Tarreaue7dff022015-04-03 01:14:29 +02004436 if (!(s->flags & SF_ERR_MASK))
4437 s->flags |= SF_ERR_CLICL;
4438 if (!(s->flags & SF_FINST_MASK))
4439 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004440
Willy Tarreau87b09662015-04-03 00:22:06 +02004441 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004442 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004443 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004444
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004445 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004446 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004447 return 0;
4448 }
4449
4450 /* More interesting part now : we know that we have a complete
4451 * response which at least looks like HTTP. We have an indicator
4452 * of each header's length, so we can parse them quickly.
4453 */
4454
4455 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004456 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004457
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004458 /*
4459 * 1: get the status code
4460 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004461 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004462 if (n < 1 || n > 5)
4463 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004464 /* when the client triggers a 4xx from the server, it's most often due
4465 * to a missing object or permission. These events should be tracked
4466 * because if they happen often, it may indicate a brute force or a
4467 * vulnerability scan.
4468 */
4469 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004470 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004471
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004472 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004473 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004474
Willy Tarreau91852eb2015-05-01 13:26:00 +02004475 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4476 * exactly one digit "." one digit. This check may be disabled using
4477 * option accept-invalid-http-response.
4478 */
4479 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4480 if (msg->sl.st.v_l != 8) {
4481 msg->err_pos = 0;
4482 goto hdr_response_bad;
4483 }
4484
Willy Tarreauf37954d2018-06-15 18:31:02 +02004485 if (ci_head(rep)[4] != '/' ||
4486 !isdigit((unsigned char)ci_head(rep)[5]) ||
4487 ci_head(rep)[6] != '.' ||
4488 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004489 msg->err_pos = 4;
4490 goto hdr_response_bad;
4491 }
4492 }
4493
Willy Tarreau5b154472009-12-21 20:11:07 +01004494 /* check if the response is HTTP/1.1 or above */
4495 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004496 ((ci_head(rep)[5] > '1') ||
4497 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004498 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004499
4500 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004501 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 +01004502
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004503 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004504 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004505
Willy Tarreauf37954d2018-06-15 18:31:02 +02004506 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004507
Willy Tarreau39650402010-03-15 19:44:39 +01004508 /* Adjust server's health based on status code. Note: status codes 501
4509 * and 505 are triggered on demand by client request, so we must not
4510 * count them as server failures.
4511 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004512 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004513 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004514 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004515 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004516 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004517 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004518
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004519 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004520 * We may be facing a 100-continue response, or any other informational
4521 * 1xx response which is non-final, in which case this is not the right
4522 * response, and we're waiting for the next one. Let's allow this response
4523 * to go to the client and wait for the next one. There's an exception for
4524 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004525 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004526 if (txn->status < 200 &&
4527 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004528 hdr_idx_init(&txn->hdr_idx);
4529 msg->next -= channel_forward(rep, msg->next);
4530 msg->msg_state = HTTP_MSG_RPBEFORE;
4531 txn->status = 0;
4532 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004533 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004534 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004535 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004536
Willy Tarreaua14ad722017-07-07 11:36:32 +02004537 /*
4538 * 2: check for cacheability.
4539 */
4540
4541 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004542 case 200:
4543 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004544 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004545 case 206:
4546 case 300:
4547 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004548 case 404:
4549 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004550 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004551 case 414:
4552 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004553 break;
4554 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004555 /* RFC7231#6.1:
4556 * Responses with status codes that are defined as
4557 * cacheable by default (e.g., 200, 203, 204, 206,
4558 * 300, 301, 404, 405, 410, 414, and 501 in this
4559 * specification) can be reused by a cache with
4560 * heuristic expiration unless otherwise indicated
4561 * by the method definition or explicit cache
4562 * controls [RFC7234]; all other status codes are
4563 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004564 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004565 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004566 break;
4567 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004568
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004569 /*
4570 * 3: we may need to capture headers
4571 */
4572 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004573 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004574 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4575 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004576
Willy Tarreau557f1992015-05-01 10:05:17 +02004577 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4578 * by RFC7230#3.3.3 :
4579 *
4580 * The length of a message body is determined by one of the following
4581 * (in order of precedence):
4582 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004583 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4584 * the connection will become a tunnel immediately after the empty
4585 * line that concludes the header fields. A client MUST ignore
4586 * any Content-Length or Transfer-Encoding header fields received
4587 * in such a message. Any 101 response (Switching Protocols) is
4588 * managed in the same manner.
4589 *
4590 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004591 * (Informational), 204 (No Content), or 304 (Not Modified) status
4592 * code is always terminated by the first empty line after the
4593 * header fields, regardless of the header fields present in the
4594 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004595 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004596 * 3. If a Transfer-Encoding header field is present and the chunked
4597 * transfer coding (Section 4.1) is the final encoding, the message
4598 * body length is determined by reading and decoding the chunked
4599 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004600 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004601 * If a Transfer-Encoding header field is present in a response and
4602 * the chunked transfer coding is not the final encoding, the
4603 * message body length is determined by reading the connection until
4604 * it is closed by the server. If a Transfer-Encoding header field
4605 * is present in a request and the chunked transfer coding is not
4606 * the final encoding, the message body length cannot be determined
4607 * reliably; the server MUST respond with the 400 (Bad Request)
4608 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004609 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004610 * If a message is received with both a Transfer-Encoding and a
4611 * Content-Length header field, the Transfer-Encoding overrides the
4612 * Content-Length. Such a message might indicate an attempt to
4613 * perform request smuggling (Section 9.5) or response splitting
4614 * (Section 9.4) and ought to be handled as an error. A sender MUST
4615 * remove the received Content-Length field prior to forwarding such
4616 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004617 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004618 * 4. If a message is received without Transfer-Encoding and with
4619 * either multiple Content-Length header fields having differing
4620 * field-values or a single Content-Length header field having an
4621 * invalid value, then the message framing is invalid and the
4622 * recipient MUST treat it as an unrecoverable error. If this is a
4623 * request message, the server MUST respond with a 400 (Bad Request)
4624 * status code and then close the connection. If this is a response
4625 * message received by a proxy, the proxy MUST close the connection
4626 * to the server, discard the received response, and send a 502 (Bad
4627 * Gateway) response to the client. If this is a response message
4628 * received by a user agent, the user agent MUST close the
4629 * connection to the server and discard the received response.
4630 *
4631 * 5. If a valid Content-Length header field is present without
4632 * Transfer-Encoding, its decimal value defines the expected message
4633 * body length in octets. If the sender closes the connection or
4634 * the recipient times out before the indicated number of octets are
4635 * received, the recipient MUST consider the message to be
4636 * incomplete and close the connection.
4637 *
4638 * 6. If this is a request message and none of the above are true, then
4639 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004640 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004641 * 7. Otherwise, this is a response message without a declared message
4642 * body length, so the message body length is determined by the
4643 * number of octets received prior to the server closing the
4644 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004645 */
4646
4647 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004648 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004649 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004650 * FIXME: should we parse anyway and return an error on chunked encoding ?
4651 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004652 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4653 txn->status == 101)) {
4654 /* Either we've established an explicit tunnel, or we're
4655 * switching the protocol. In both cases, we're very unlikely
4656 * to understand the next protocols. We have to switch to tunnel
4657 * mode, so that we transfer the request and responses then let
4658 * this protocol pass unmodified. When we later implement specific
4659 * parsers for such protocols, we'll want to check the Upgrade
4660 * header which contains information about that protocol for
4661 * responses with status 101 (eg: see RFC2817 about TLS).
4662 */
4663 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4664 msg->flags |= HTTP_MSGF_XFER_LEN;
4665 goto end;
4666 }
4667
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004668 if (txn->meth == HTTP_METH_HEAD ||
4669 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004670 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004671 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004672 goto skip_content_length;
4673 }
4674
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004675 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004676 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004677 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004678 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004679 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4680 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004681 /* bad transfer-encoding (chunked followed by something else) */
4682 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004683 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004684 break;
4685 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004686 }
4687
Willy Tarreau1c913912015-04-30 10:57:51 +02004688 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004689 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02004690 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004691 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02004692 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4693 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02004694 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004695 signed long long cl;
4696
Willy Tarreauad14f752011-09-02 20:33:27 +02004697 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004698 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004699 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004700 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004701
Willy Tarreauad14f752011-09-02 20:33:27 +02004702 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004703 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004704 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004705 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004706
Willy Tarreauad14f752011-09-02 20:33:27 +02004707 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004708 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004709 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004710 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004711
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004712 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004713 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004714 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004715 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004716
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004717 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004718 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004719 }
4720
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004721 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
4722 * 407 (Proxy-Authenticate) responses and set the connection to private
4723 */
4724 if (srv_conn && txn->status == 401) {
4725 /* check for Negotiate/NTLM WWW-Authenticate headers */
4726 ctx.idx = 0;
4727 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
4728 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
4729 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
4730 srv_conn->flags |= CO_FL_PRIVATE;
4731 }
4732 } else if (srv_conn && txn->status == 407) {
4733 /* check for Negotiate/NTLM Proxy-Authenticate headers */
4734 ctx.idx = 0;
4735 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
4736 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
4737 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
4738 srv_conn->flags |= CO_FL_PRIVATE;
4739 }
4740 }
4741
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004742 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01004743 /* Now we have to check if we need to modify the Connection header.
4744 * This is more difficult on the response than it is on the request,
4745 * because we can have two different HTTP versions and we don't know
4746 * how the client will interprete a response. For instance, let's say
4747 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4748 * HTTP/1.1 response without any header. Maybe it will bound itself to
4749 * HTTP/1.0 because it only knows about it, and will consider the lack
4750 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4751 * the lack of header as a keep-alive. Thus we will use two flags
4752 * indicating how a request MAY be understood by the client. In case
4753 * of multiple possibilities, we'll fix the header to be explicit. If
4754 * ambiguous cases such as both close and keepalive are seen, then we
4755 * will fall back to explicit close. Note that we won't take risks with
4756 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004757 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004758 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004759 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02004760 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01004761 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004762
Willy Tarreau60466522010-01-18 19:08:45 +01004763 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02004764 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01004765 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004766
Willy Tarreau60466522010-01-18 19:08:45 +01004767 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02004768 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01004769 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004770 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004771 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004772 }
Willy Tarreau60466522010-01-18 19:08:45 +01004773 else { /* SCL / KAL */
4774 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004775 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004776 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004777 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004778
Willy Tarreau60466522010-01-18 19:08:45 +01004779 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004780 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004781
Willy Tarreau60466522010-01-18 19:08:45 +01004782 /* Some keep-alive responses are converted to Server-close if
4783 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004784 */
Willy Tarreau60466522010-01-18 19:08:45 +01004785 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4786 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004787 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004788 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004789 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004790 }
4791
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004792 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02004793 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02004794 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02004795
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004796 /* end of job, return OK */
4797 rep->analysers &= ~an_bit;
4798 rep->analyse_exp = TICK_ETERNITY;
4799 channel_auto_close(rep);
4800 return 1;
4801
4802 abort_keep_alive:
4803 /* A keep-alive request to the server failed on a network error.
4804 * The client is required to retry. We need to close without returning
4805 * any other information so that the client retries.
4806 */
4807 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004808 rep->analysers &= AN_RES_FLT_END;
4809 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004810 channel_auto_close(rep);
4811 s->logs.logwait = 0;
4812 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004813 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01004814 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004815 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004816 return 0;
4817}
4818
4819/* This function performs all the processing enabled for the current response.
4820 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004821 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004822 * other functions. It works like process_request (see indications above).
4823 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004824int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004825{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004826 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004827 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004828 struct http_msg *msg = &txn->rsp;
4829 struct proxy *cur_proxy;
4830 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004831 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004832
Christopher Faulete0768eb2018-10-03 16:38:02 +02004833 if (IS_HTX_STRM(s))
4834 return htx_process_res_common(s, rep, an_bit, px);
4835
Christopher Faulet45073512018-07-20 10:16:29 +02004836 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 +02004837 now_ms, __FUNCTION__,
4838 s,
4839 rep,
4840 rep->rex, rep->wex,
4841 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004842 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004843 rep->analysers);
4844
4845 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
4846 return 0;
4847
Willy Tarreau70730dd2014-04-24 18:06:27 +02004848 /* The stats applet needs to adjust the Connection header but we don't
4849 * apply any filter there.
4850 */
Willy Tarreau612adb82015-03-10 15:25:54 +01004851 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
4852 rep->analysers &= ~an_bit;
4853 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02004854 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01004855 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02004856
Willy Tarreau58975672014-04-24 21:13:57 +02004857 /*
4858 * We will have to evaluate the filters.
4859 * As opposed to version 1.2, now they will be evaluated in the
4860 * filters order and not in the header order. This means that
4861 * each filter has to be validated among all headers.
4862 *
4863 * Filters are tried with ->be first, then with ->fe if it is
4864 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004865 *
4866 * Maybe we are in resume condiion. In this case I choose the
4867 * "struct proxy" which contains the rule list matching the resume
4868 * pointer. If none of theses "struct proxy" match, I initialise
4869 * the process with the first one.
4870 *
4871 * In fact, I check only correspondance betwwen the current list
4872 * pointer and the ->fe rule list. If it doesn't match, I initialize
4873 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02004874 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004875 if (s->current_rule_list == &sess->fe->http_res_rules)
4876 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004877 else
4878 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02004879 while (1) {
4880 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004881
Willy Tarreau58975672014-04-24 21:13:57 +02004882 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02004883 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004884 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004885
Willy Tarreau51d861a2015-05-22 17:30:48 +02004886 if (ret == HTTP_RULE_RES_BADREQ)
4887 goto return_srv_prx_502;
4888
4889 if (ret == HTTP_RULE_RES_DONE) {
4890 rep->analysers &= ~an_bit;
4891 rep->analyse_exp = TICK_ETERNITY;
4892 return 1;
4893 }
4894 }
4895
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004896 /* we need to be called again. */
4897 if (ret == HTTP_RULE_RES_YIELD) {
4898 channel_dont_close(rep);
4899 return 0;
4900 }
4901
Willy Tarreau58975672014-04-24 21:13:57 +02004902 /* try headers filters */
4903 if (rule_set->rsp_exp != NULL) {
4904 if (apply_filters_to_response(s, rep, rule_set) < 0) {
4905 return_bad_resp:
4906 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004907 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004908 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004909 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01004910 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02004911 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004912 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02004913 txn->status = 502;
4914 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01004915 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004916 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004917 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02004918 if (!(s->flags & SF_ERR_MASK))
4919 s->flags |= SF_ERR_PRXCOND;
4920 if (!(s->flags & SF_FINST_MASK))
4921 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02004922 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004923 }
Willy Tarreau58975672014-04-24 21:13:57 +02004924 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004925
Willy Tarreau58975672014-04-24 21:13:57 +02004926 /* has the response been denied ? */
4927 if (txn->flags & TX_SVDENY) {
4928 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004929 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004930
Olivier Houcharda798bf52019-03-08 18:52:00 +01004931 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
4932 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004933 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01004934 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004935
Willy Tarreau58975672014-04-24 21:13:57 +02004936 goto return_srv_prx_502;
4937 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004938
Willy Tarreau58975672014-04-24 21:13:57 +02004939 /* add response headers from the rule sets in the same order */
4940 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02004941 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004942 break;
Willy Tarreau58975672014-04-24 21:13:57 +02004943 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004944 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02004945 ret = acl_pass(ret);
4946 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4947 ret = !ret;
4948 if (!ret)
4949 continue;
4950 }
Christopher Faulet10079f52018-10-03 15:17:28 +02004951 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02004952 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004953 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004954
Willy Tarreau58975672014-04-24 21:13:57 +02004955 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004956 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02004957 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004958 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02004959 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004960
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004961 /* After this point, this anayzer can't return yield, so we can
4962 * remove the bit corresponding to this analyzer from the list.
4963 *
4964 * Note that the intermediate returns and goto found previously
4965 * reset the analyzers.
4966 */
4967 rep->analysers &= ~an_bit;
4968 rep->analyse_exp = TICK_ETERNITY;
4969
Willy Tarreau58975672014-04-24 21:13:57 +02004970 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02004971 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02004972 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004973
Willy Tarreau58975672014-04-24 21:13:57 +02004974 /*
4975 * Now check for a server cookie.
4976 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02004977 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02004978 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004979
Willy Tarreau58975672014-04-24 21:13:57 +02004980 /*
4981 * Check for cache-control or pragma headers if required.
4982 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01004983 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02004984 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004985
Willy Tarreau58975672014-04-24 21:13:57 +02004986 /*
4987 * Add server cookie in the response if needed
4988 */
4989 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
4990 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004991 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02004992 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
4993 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
4994 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
4995 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
4996 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004997 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02004998 /* the server is known, it's not the one the client requested, or the
4999 * cookie's last seen date needs to be refreshed. We have to
5000 * insert a set-cookie here, except if we want to insert only on POST
5001 * requests and this one isn't. Note that servers which don't have cookies
5002 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005003 */
Willy Tarreau58975672014-04-24 21:13:57 +02005004 if (!objt_server(s->target)->cookie) {
5005 chunk_printf(&trash,
5006 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5007 s->be->cookie_name);
5008 }
5009 else {
5010 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005011
Willy Tarreau58975672014-04-24 21:13:57 +02005012 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5013 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005014 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5015 s30tob64((date.tv_sec+3) >> 2,
5016 trash.area + trash.data);
5017 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005018
Willy Tarreau58975672014-04-24 21:13:57 +02005019 if (s->be->cookie_maxlife) {
5020 /* emit first_date, which is either the original one or
5021 * the current date.
5022 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005023 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005024 s30tob64(txn->cookie_first_date ?
5025 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005026 (date.tv_sec+3) >> 2,
5027 trash.area + trash.data);
5028 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005029 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005030 }
Willy Tarreau58975672014-04-24 21:13:57 +02005031 chunk_appendf(&trash, "; path=/");
5032 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005033
Willy Tarreau58975672014-04-24 21:13:57 +02005034 if (s->be->cookie_domain)
5035 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005036
Willy Tarreau58975672014-04-24 21:13:57 +02005037 if (s->be->ck_opts & PR_CK_HTTPONLY)
5038 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005039
Willy Tarreau58975672014-04-24 21:13:57 +02005040 if (s->be->ck_opts & PR_CK_SECURE)
5041 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005042
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005043 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005044 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005045
Willy Tarreau58975672014-04-24 21:13:57 +02005046 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005047 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005048 /* the server did not change, only the date was updated */
5049 txn->flags |= TX_SCK_UPDATED;
5050 else
5051 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005052
Willy Tarreau58975672014-04-24 21:13:57 +02005053 /* Here, we will tell an eventual cache on the client side that we don't
5054 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5055 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5056 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005057 */
Willy Tarreau58975672014-04-24 21:13:57 +02005058 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005059
Willy Tarreau58975672014-04-24 21:13:57 +02005060 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005061
Willy Tarreau58975672014-04-24 21:13:57 +02005062 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5063 "Cache-control: private", 22) < 0))
5064 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005065 }
Willy Tarreau58975672014-04-24 21:13:57 +02005066 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005067
Willy Tarreau58975672014-04-24 21:13:57 +02005068 /*
5069 * Check if result will be cacheable with a cookie.
5070 * We'll block the response if security checks have caught
5071 * nasty things such as a cacheable cookie.
5072 */
5073 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5074 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5075 (s->be->options & PR_O_CHK_CACHE)) {
5076 /* we're in presence of a cacheable response containing
5077 * a set-cookie header. We'll block it as requested by
5078 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005079 */
Willy Tarreau58975672014-04-24 21:13:57 +02005080 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01005081 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005082
Olivier Houcharda798bf52019-03-08 18:52:00 +01005083 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5084 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005085 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01005086 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005087
Christopher Faulet767a84b2017-11-24 16:50:31 +01005088 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5089 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005090 send_log(s->be, LOG_ALERT,
5091 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5092 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5093 goto return_srv_prx_502;
5094 }
Willy Tarreau03945942009-12-22 16:50:27 +01005095
Willy Tarreau70730dd2014-04-24 18:06:27 +02005096 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005097 /*
5098 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5099 * If an "Upgrade" token is found, the header is left untouched in order
5100 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005101 * if anything but "Upgrade" is present in the Connection header. We don't
5102 * want to touch any 101 response either since it's switching to another
5103 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005104 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005105 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005106 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005107 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005108
Willy Tarreau58975672014-04-24 21:13:57 +02005109 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5110 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5111 /* we want a keep-alive response here. Keep-alive header
5112 * required if either side is not 1.1.
5113 */
5114 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5115 want_flags |= TX_CON_KAL_SET;
5116 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005117 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005118 /* we want a close response here. Close header required if
5119 * the server is 1.1, regardless of the client.
5120 */
5121 if (msg->flags & HTTP_MSGF_VER_11)
5122 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005123 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005124
Willy Tarreau58975672014-04-24 21:13:57 +02005125 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5126 http_change_connection_header(txn, msg, want_flags);
5127 }
5128
5129 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005130 /* Always enter in the body analyzer */
5131 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5132 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005133
Willy Tarreau58975672014-04-24 21:13:57 +02005134 /* if the user wants to log as soon as possible, without counting
5135 * bytes from the server, then this is the right moment. We have
5136 * to temporarily assign bytes_out to log what we currently have.
5137 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005138 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005139 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5140 s->logs.bytes_out = txn->rsp.eoh;
5141 s->do_log(s);
5142 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005143 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005144 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005145}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005146
Willy Tarreaud98cf932009-12-27 22:54:55 +01005147/* This function is an analyser which forwards response body (including chunk
5148 * sizes if any). It is called as soon as we must forward, even if we forward
5149 * zero byte. The only situation where it must not be called is when we're in
5150 * tunnel mode and we want to forward till the close. It's used both to forward
5151 * remaining data and to resync after end of body. It expects the msg_state to
5152 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005153 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005154 *
5155 * It is capable of compressing response data both in content-length mode and
5156 * in chunked mode. The state machines follows different flows depending on
5157 * whether content-length and chunked modes are used, since there are no
5158 * trailers in content-length :
5159 *
5160 * chk-mode cl-mode
5161 * ,----- BODY -----.
5162 * / \
5163 * V size > 0 V chk-mode
5164 * .--> SIZE -------------> DATA -------------> CRLF
5165 * | | size == 0 | last byte |
5166 * | v final crlf v inspected |
5167 * | TRAILERS -----------> DONE |
5168 * | |
5169 * `----------------------------------------------'
5170 *
5171 * Compression only happens in the DATA state, and must be flushed in final
5172 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5173 * is performed at once on final states for all bytes parsed, or when leaving
5174 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005175 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005176int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005177{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005178 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005179 struct http_txn *txn = s->txn;
5180 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005181 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005182
Christopher Faulete0768eb2018-10-03 16:38:02 +02005183 if (IS_HTX_STRM(s))
5184 return htx_response_forward_body(s, res, an_bit);
5185
Christopher Faulet45073512018-07-20 10:16:29 +02005186 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 +02005187 now_ms, __FUNCTION__,
5188 s,
5189 res,
5190 res->rex, res->wex,
5191 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005192 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005193 res->analysers);
5194
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005195 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5196 return 0;
5197
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005198 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005199 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Faulet93e02d82019-03-08 14:18:50 +01005200 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005201 /* Output closed while we were sending data. We must abort and
5202 * wake the other side up.
5203 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005204 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005205 msg->msg_state = HTTP_MSG_ERROR;
5206 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005207 return 1;
5208 }
5209
Willy Tarreau4fe41902010-06-07 22:27:41 +02005210 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005211 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005212
Christopher Fauletd7c91962015-04-30 11:48:27 +02005213 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005214 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5215 ? HTTP_MSG_CHUNK_SIZE
5216 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005217 }
5218
Willy Tarreauefdf0942014-04-24 20:08:57 +02005219 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005220 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005221 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005222 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005223 }
5224
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005225 if (msg->msg_state < HTTP_MSG_DONE) {
5226 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5227 ? http_msg_forward_chunked_body(s, msg)
5228 : http_msg_forward_body(s, msg));
5229 if (!ret)
5230 goto missing_data_or_waiting;
5231 if (ret < 0)
5232 goto return_bad_res;
5233 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005234
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005235 /* other states, DONE...TUNNEL */
5236 /* for keep-alive we don't want to forward closes on DONE */
5237 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5238 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5239 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005240
Christopher Faulet894da4c2017-07-18 11:29:07 +02005241 http_resync_states(s);
5242 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005243 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5244 if (res->flags & CF_SHUTW) {
5245 /* response errors are most likely due to the
5246 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01005247 goto return_cli_abort;
Willy Tarreau5523b322009-12-29 12:05:52 +01005248 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005249 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005250 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005251 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005252 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005253 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005254 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005255 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005256
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005257 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005258 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01005259 goto return_cli_abort;
Willy Tarreauf003d372012-11-26 13:35:37 +01005260
5261 /* stop waiting for data if the input is closed before the end. If the
5262 * client side was already closed, it means that the client has aborted,
5263 * so we don't want to count this as a server abort. Otherwise it's a
5264 * server abort.
5265 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005266 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005267 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01005268 goto return_cli_abort;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005269 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01005270 if (!ci_data(res))
5271 goto return_srv_abort;
Willy Tarreau40dba092010-03-04 18:14:51 +01005272 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005273
Willy Tarreau40dba092010-03-04 18:14:51 +01005274 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005275 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005276 goto return_bad_res;
5277
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005278 /* When TE: chunked is used, we need to get there again to parse
5279 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005280 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5281 * or if there are filters registered on the stream, we don't want to
5282 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005283 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005284 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005285 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005286 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5287 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005288 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005289
Willy Tarreau5c620922011-05-11 19:56:11 +02005290 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005291 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005292 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005293 * modes are already handled by the stream sock layer. We must not do
5294 * this in content-length mode because it could present the MSG_MORE
5295 * flag with the last block of forwarded data, which would cause an
5296 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005297 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005298 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005299 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005300
Willy Tarreau87b09662015-04-03 00:22:06 +02005301 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005302 return 0;
5303
Christopher Faulet93e02d82019-03-08 14:18:50 +01005304 return_srv_abort:
5305 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
5306 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005307 if (objt_server(s->target))
Christopher Faulet93e02d82019-03-08 14:18:50 +01005308 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
5309 if (!(s->flags & SF_ERR_MASK))
5310 s->flags |= SF_ERR_SRVCL;
5311 goto return_error;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005312
Christopher Faulet93e02d82019-03-08 14:18:50 +01005313 return_cli_abort:
5314 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5315 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005316 if (objt_server(s->target))
Christopher Faulet93e02d82019-03-08 14:18:50 +01005317 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
5318 if (!(s->flags & SF_ERR_MASK))
5319 s->flags |= SF_ERR_CLICL;
5320 goto return_error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005321
Christopher Faulet93e02d82019-03-08 14:18:50 +01005322 return_bad_res:
5323 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
5324 if (objt_server(s->target)) {
5325 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
5326 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
5327 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005328 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01005329 s->flags |= SF_ERR_SRVCL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005330
Christopher Faulet93e02d82019-03-08 14:18:50 +01005331 return_error:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005332 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005333 txn->rsp.msg_state = HTTP_MSG_ERROR;
5334 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005335 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005336 res->analysers &= AN_RES_FLT_END;
5337 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaue7dff022015-04-03 01:14:29 +02005338 if (!(s->flags & SF_FINST_MASK))
5339 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005340 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005341}
5342
5343
Christopher Faulet10079f52018-10-03 15:17:28 +02005344int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005345{
5346 struct channel *chn = msg->chn;
5347 int ret;
5348
5349 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5350
5351 if (msg->msg_state == HTTP_MSG_ENDING)
5352 goto ending;
5353
5354 /* Neither content-length, nor transfer-encoding was found, so we must
5355 * read the body until the server connection is closed. In that case, we
5356 * eat data as they come. Of course, this happens for response only. */
5357 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005358 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005359 msg->chunk_len += len;
5360 msg->body_len += len;
5361 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005362 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005363 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005364 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005365 msg->next += ret;
5366 msg->chunk_len -= ret;
5367 if (msg->chunk_len) {
5368 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005369 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005370 chn->flags |= CF_WAKE_WRITE;
5371 goto missing_data_or_waiting;
5372 }
5373
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005374 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5375 * always set for a request. */
5376 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5377 /* The server still sending data that should be filtered */
5378 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5379 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005380 msg->msg_state = HTTP_MSG_TUNNEL;
5381 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005382 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005383
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005384 msg->msg_state = HTTP_MSG_ENDING;
5385
5386 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005387 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005388 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005389 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5390 /* default_ret */ msg->next,
5391 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005392 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005393 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005394 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5395 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005396 if (msg->next)
5397 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005398
Christopher Fauletda02e172015-12-04 09:25:05 +01005399 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5400 /* default_ret */ 1,
5401 /* on_error */ goto error,
5402 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005403 if (msg->msg_state == HTTP_MSG_ENDING)
5404 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005405 return 1;
5406
5407 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005408 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005409 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5410 /* default_ret */ msg->next,
5411 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005412 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005413 msg->next -= ret;
5414 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5415 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005416 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005417 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005418 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005419 return 0;
5420 error:
5421 return -1;
5422}
5423
Christopher Faulet10079f52018-10-03 15:17:28 +02005424int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005425{
5426 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005427 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005428 int ret;
5429
5430 /* Here we have the guarantee to be in one of the following state:
5431 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5432 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5433
Christopher Fauletca874b82018-09-20 11:31:01 +02005434 if (msg->msg_state == HTTP_MSG_ENDING)
5435 goto ending;
5436
5437 /* Don't parse chunks if there is no input data */
5438 if (!ci_data(chn))
5439 goto waiting;
5440
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005441 switch_states:
5442 switch (msg->msg_state) {
5443 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005444 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005445 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005446 /* on_error */ goto error);
5447 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005448 msg->chunk_len -= ret;
5449 if (msg->chunk_len) {
5450 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005451 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005452 chn->flags |= CF_WAKE_WRITE;
5453 goto missing_data_or_waiting;
5454 }
5455
5456 /* nothing left to forward for this chunk*/
5457 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5458 /* fall through for HTTP_MSG_CHUNK_CRLF */
5459
5460 case HTTP_MSG_CHUNK_CRLF:
5461 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005462 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005463 if (ret == 0)
5464 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005465 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005466 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005467 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005468 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005469 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005470 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005471 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005472 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5473 /* fall through for HTTP_MSG_CHUNK_SIZE */
5474
5475 case HTTP_MSG_CHUNK_SIZE:
5476 /* read the chunk size and assign it to ->chunk_len,
5477 * then set ->next to point to the body and switch to
5478 * DATA or TRAILERS state.
5479 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005480 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005481 if (ret == 0)
5482 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005483 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005484 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005485 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005486 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005487 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005488 }
5489
5490 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005491 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005492 msg->chunk_len = chunk;
5493 msg->body_len += chunk;
5494
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005495 if (msg->chunk_len) {
5496 msg->msg_state = HTTP_MSG_DATA;
5497 goto switch_states;
5498 }
5499 msg->msg_state = HTTP_MSG_TRAILERS;
5500 /* fall through for HTTP_MSG_TRAILERS */
5501
5502 case HTTP_MSG_TRAILERS:
5503 ret = http_forward_trailers(msg);
5504 if (ret < 0)
5505 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005506 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5507 /* default_ret */ 1,
5508 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005509 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005510 if (!ret)
5511 goto missing_data_or_waiting;
5512 break;
5513
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005514 default:
5515 /* This should no happen in this function */
5516 goto error;
5517 }
5518
5519 msg->msg_state = HTTP_MSG_ENDING;
5520 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005521 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005522 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005523 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005524 /* default_ret */ msg->next,
5525 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005526 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005527 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005528 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5529 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005530 if (msg->next)
5531 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005532
Christopher Fauletda02e172015-12-04 09:25:05 +01005533 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005534 /* default_ret */ 1,
5535 /* on_error */ goto error,
5536 /* on_wait */ goto waiting);
5537 msg->msg_state = HTTP_MSG_DONE;
5538 return 1;
5539
5540 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005541 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005542 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005543 /* default_ret */ msg->next,
5544 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005545 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005546 msg->next -= ret;
5547 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5548 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005549 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005550 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005551 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005552 return 0;
5553
5554 chunk_parsing_error:
5555 if (msg->err_pos >= 0) {
5556 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005557 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005558 msg->msg_state, strm_fe(s));
5559 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005560 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005561 msg, msg->msg_state, s->be);
5562 }
5563 error:
5564 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005565}
5566
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005567
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005568/* Iterate the same filter through all request headers.
5569 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005570 * Since it can manage the switch to another backend, it updates the per-proxy
5571 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005572 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005573int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005574{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005575 char *cur_ptr, *cur_end, *cur_next;
5576 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005577 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005578 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005579 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005581 last_hdr = 0;
5582
Willy Tarreauf37954d2018-06-15 18:31:02 +02005583 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005584 old_idx = 0;
5585
5586 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005587 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005588 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005589 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005590 (exp->action == ACT_ALLOW ||
5591 exp->action == ACT_DENY ||
5592 exp->action == ACT_TARPIT))
5593 return 0;
5594
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005595 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005596 if (!cur_idx)
5597 break;
5598
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005599 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005600 cur_ptr = cur_next;
5601 cur_end = cur_ptr + cur_hdr->len;
5602 cur_next = cur_end + cur_hdr->cr + 1;
5603
5604 /* Now we have one header between cur_ptr and cur_end,
5605 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005606 */
5607
Willy Tarreau15a53a42015-01-21 13:39:42 +01005608 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005609 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005610 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005611 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005612 last_hdr = 1;
5613 break;
5614
5615 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005616 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005617 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005618 break;
5619
5620 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005621 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005622 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005623 break;
5624
5625 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005626 len = exp_replace(trash.area,
5627 trash.size, cur_ptr,
5628 exp->replace, pmatch);
5629 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005630 return -1;
5631
Willy Tarreau6e27be12018-08-22 04:46:47 +02005632 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5633
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005634 /* FIXME: if the user adds a newline in the replacement, the
5635 * index will not be recalculated for now, and the new line
5636 * will not be counted as a new header.
5637 */
5638
5639 cur_end += delta;
5640 cur_next += delta;
5641 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005642 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005643 break;
5644
5645 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005646 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005647 cur_next += delta;
5648
Willy Tarreaufa355d42009-11-29 18:12:29 +01005649 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005650 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5651 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005652 cur_hdr->len = 0;
5653 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005654 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005655 break;
5656
5657 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005658 }
5659
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005660 /* keep the link from this header to next one in case of later
5661 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005662 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005663 old_idx = cur_idx;
5664 }
5665 return 0;
5666}
5667
5668
5669/* Apply the filter to the request line.
5670 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5671 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005672 * Since it can manage the switch to another backend, it updates the per-proxy
5673 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005674 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005675int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005676{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005677 char *cur_ptr, *cur_end;
5678 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005679 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005680 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005681
Willy Tarreau3d300592007-03-18 18:34:41 +01005682 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005683 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005684 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005685 (exp->action == ACT_ALLOW ||
5686 exp->action == ACT_DENY ||
5687 exp->action == ACT_TARPIT))
5688 return 0;
5689 else if (exp->action == ACT_REMOVE)
5690 return 0;
5691
5692 done = 0;
5693
Willy Tarreauf37954d2018-06-15 18:31:02 +02005694 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005695 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005696
5697 /* Now we have the request line between cur_ptr and cur_end */
5698
Willy Tarreau15a53a42015-01-21 13:39:42 +01005699 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005700 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005701 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005702 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005703 done = 1;
5704 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005705
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005706 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005707 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005708 done = 1;
5709 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005710
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005711 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005712 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005713 done = 1;
5714 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005715
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005716 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005717 len = exp_replace(trash.area, trash.size,
5718 cur_ptr, exp->replace, pmatch);
5719 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005720 return -1;
5721
Willy Tarreau6e27be12018-08-22 04:46:47 +02005722 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5723
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005724 /* FIXME: if the user adds a newline in the replacement, the
5725 * index will not be recalculated for now, and the new line
5726 * will not be counted as a new header.
5727 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005728
Willy Tarreaufa355d42009-11-29 18:12:29 +01005729 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005730 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005731 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005732 HTTP_MSG_RQMETH,
5733 cur_ptr, cur_end + 1,
5734 NULL, NULL);
5735 if (unlikely(!cur_end))
5736 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005737
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005738 /* we have a full request and we know that we have either a CR
5739 * or an LF at <ptr>.
5740 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005741 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5742 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005743 /* there is no point trying this regex on headers */
5744 return 1;
5745 }
5746 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005747 return done;
5748}
Willy Tarreau97de6242006-12-27 17:18:38 +01005749
Willy Tarreau58f10d72006-12-04 02:26:12 +01005750
Willy Tarreau58f10d72006-12-04 02:26:12 +01005751
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005752/*
Willy Tarreau87b09662015-04-03 00:22:06 +02005753 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005754 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005755 * unparsable request. Since it can manage the switch to another backend, it
5756 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005757 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005758int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005759{
Willy Tarreau192252e2015-04-04 01:47:55 +02005760 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005761 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005762 struct hdr_exp *exp;
5763
5764 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005765 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005766
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005767 /*
5768 * The interleaving of transformations and verdicts
5769 * makes it difficult to decide to continue or stop
5770 * the evaluation.
5771 */
5772
Willy Tarreau6c123b12010-01-28 20:22:06 +01005773 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5774 break;
5775
Willy Tarreau3d300592007-03-18 18:34:41 +01005776 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005777 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005778 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005779 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005780
5781 /* if this filter had a condition, evaluate it now and skip to
5782 * next filter if the condition does not match.
5783 */
5784 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005785 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01005786 ret = acl_pass(ret);
5787 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5788 ret = !ret;
5789
5790 if (!ret)
5791 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005792 }
5793
5794 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005795 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005796 if (unlikely(ret < 0))
5797 return -1;
5798
5799 if (likely(ret == 0)) {
5800 /* The filter did not match the request, it can be
5801 * iterated through all headers.
5802 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01005803 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
5804 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005805 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005806 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005807 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005808}
5809
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005810
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005811/* Delete a value in a header between delimiters <from> and <next> in buffer
5812 * <buf>. The number of characters displaced is returned, and the pointer to
5813 * the first delimiter is updated if required. The function tries as much as
5814 * possible to respect the following principles :
5815 * - replace <from> delimiter by the <next> one unless <from> points to a
5816 * colon, in which case <next> is simply removed
5817 * - set exactly one space character after the new first delimiter, unless
5818 * there are not enough characters in the block being moved to do so.
5819 * - remove unneeded spaces before the previous delimiter and after the new
5820 * one.
5821 *
5822 * It is the caller's responsibility to ensure that :
5823 * - <from> points to a valid delimiter or the colon ;
5824 * - <next> points to a valid delimiter or the final CR/LF ;
5825 * - there are non-space chars before <from> ;
5826 * - there is a CR/LF at or after <next>.
5827 */
Willy Tarreauc5a4fd52018-12-11 11:42:27 +01005828static int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005829{
5830 char *prev = *from;
5831
5832 if (*prev == ':') {
5833 /* We're removing the first value, preserve the colon and add a
5834 * space if possible.
5835 */
Willy Tarreau2235b262016-11-05 15:50:20 +01005836 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005837 next++;
5838 prev++;
5839 if (prev < next)
5840 *prev++ = ' ';
5841
Willy Tarreau2235b262016-11-05 15:50:20 +01005842 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005843 next++;
5844 } else {
5845 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01005846 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005847 prev--;
5848 *from = prev;
5849
5850 /* copy the delimiter and if possible a space if we're
5851 * not at the end of the line.
5852 */
Willy Tarreau2235b262016-11-05 15:50:20 +01005853 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005854 *prev++ = *next++;
5855 if (prev + 1 < next)
5856 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01005857 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005858 next++;
5859 }
5860 }
Willy Tarreaue3128022018-07-12 15:55:34 +02005861 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005862}
5863
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005864/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005865 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005866 * desirable to call it only when needed. This code is quite complex because
5867 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5868 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005869 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005870void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005871{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005872 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005873 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005874 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005875 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005876 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5877 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005878
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005879 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005880 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005881 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005882
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005883 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005884 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005885 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005886
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005887 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005888 hdr_beg = hdr_next;
5889 hdr_end = hdr_beg + cur_hdr->len;
5890 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005891
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005892 /* We have one full header between hdr_beg and hdr_end, and the
5893 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005894 * "Cookie:" headers.
5895 */
5896
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005897 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005898 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005899 old_idx = cur_idx;
5900 continue;
5901 }
5902
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005903 del_from = NULL; /* nothing to be deleted */
5904 preserve_hdr = 0; /* assume we may kill the whole header */
5905
Willy Tarreau58f10d72006-12-04 02:26:12 +01005906 /* Now look for cookies. Conforming to RFC2109, we have to support
5907 * attributes whose name begin with a '$', and associate them with
5908 * the right cookie, if we want to delete this cookie.
5909 * So there are 3 cases for each cookie read :
5910 * 1) it's a special attribute, beginning with a '$' : ignore it.
5911 * 2) it's a server id cookie that we *MAY* want to delete : save
5912 * some pointers on it (last semi-colon, beginning of cookie...)
5913 * 3) it's an application cookie : we *MAY* have to delete a previous
5914 * "special" cookie.
5915 * At the end of loop, if a "special" cookie remains, we may have to
5916 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005917 * *MUST* delete it.
5918 *
5919 * Note: RFC2965 is unclear about the processing of spaces around
5920 * the equal sign in the ATTR=VALUE form. A careful inspection of
5921 * the RFC explicitly allows spaces before it, and not within the
5922 * tokens (attrs or values). An inspection of RFC2109 allows that
5923 * too but section 10.1.3 lets one think that spaces may be allowed
5924 * after the equal sign too, resulting in some (rare) buggy
5925 * implementations trying to do that. So let's do what servers do.
5926 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5927 * allowed quoted strings in values, with any possible character
5928 * after a backslash, including control chars and delimitors, which
5929 * causes parsing to become ambiguous. Browsers also allow spaces
5930 * within values even without quotes.
5931 *
5932 * We have to keep multiple pointers in order to support cookie
5933 * removal at the beginning, middle or end of header without
5934 * corrupting the header. All of these headers are valid :
5935 *
5936 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5937 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5938 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5939 * | | | | | | | | |
5940 * | | | | | | | | hdr_end <--+
5941 * | | | | | | | +--> next
5942 * | | | | | | +----> val_end
5943 * | | | | | +-----------> val_beg
5944 * | | | | +--------------> equal
5945 * | | | +----------------> att_end
5946 * | | +---------------------> att_beg
5947 * | +--------------------------> prev
5948 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005949 */
5950
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005951 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
5952 /* Iterate through all cookies on this line */
5953
5954 /* find att_beg */
5955 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01005956 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005957 att_beg++;
5958
5959 /* find att_end : this is the first character after the last non
5960 * space before the equal. It may be equal to hdr_end.
5961 */
5962 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005963
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005964 while (equal < hdr_end) {
5965 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01005966 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01005967 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005968 continue;
5969 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005970 }
5971
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005972 /* here, <equal> points to '=', a delimitor or the end. <att_end>
5973 * is between <att_beg> and <equal>, both may be identical.
5974 */
5975
5976 /* look for end of cookie if there is an equal sign */
5977 if (equal < hdr_end && *equal == '=') {
5978 /* look for the beginning of the value */
5979 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01005980 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005981 val_beg++;
5982
5983 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02005984 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005985
5986 /* make val_end point to the first white space or delimitor after the value */
5987 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01005988 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005989 val_end--;
5990 } else {
5991 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01005992 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005993
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005994 /* We have nothing to do with attributes beginning with '$'. However,
5995 * they will automatically be removed if a header before them is removed,
5996 * since they're supposed to be linked together.
5997 */
5998 if (*att_beg == '$')
5999 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006000
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006001 /* Ignore cookies with no equal sign */
6002 if (equal == next) {
6003 /* This is not our cookie, so we must preserve it. But if we already
6004 * scheduled another cookie for removal, we cannot remove the
6005 * complete header, but we can remove the previous block itself.
6006 */
6007 preserve_hdr = 1;
6008 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006009 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006010 val_end += delta;
6011 next += delta;
6012 hdr_end += delta;
6013 hdr_next += delta;
6014 cur_hdr->len += delta;
6015 http_msg_move_end(&txn->req, delta);
6016 prev = del_from;
6017 del_from = NULL;
6018 }
6019 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006020 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006021
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006022 /* if there are spaces around the equal sign, we need to
6023 * strip them otherwise we'll get trouble for cookie captures,
6024 * or even for rewrites. Since this happens extremely rarely,
6025 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006026 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006027 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6028 int stripped_before = 0;
6029 int stripped_after = 0;
6030
6031 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006032 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006033 equal += stripped_before;
6034 val_beg += stripped_before;
6035 }
6036
6037 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006038 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006039 val_beg += stripped_after;
6040 stripped_before += stripped_after;
6041 }
6042
6043 val_end += stripped_before;
6044 next += stripped_before;
6045 hdr_end += stripped_before;
6046 hdr_next += stripped_before;
6047 cur_hdr->len += stripped_before;
6048 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006049 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006050 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006051
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006052 /* First, let's see if we want to capture this cookie. We check
6053 * that we don't already have a client side cookie, because we
6054 * can only capture one. Also as an optimisation, we ignore
6055 * cookies shorter than the declared name.
6056 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006057 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6058 (val_end - att_beg >= sess->fe->capture_namelen) &&
6059 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006060 int log_len = val_end - att_beg;
6061
Willy Tarreaubafbe012017-11-24 17:34:44 +01006062 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006063 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006064 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006065 if (log_len > sess->fe->capture_len)
6066 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006067 memcpy(txn->cli_cookie, att_beg, log_len);
6068 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006069 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006070 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006071
Willy Tarreaubca99692010-10-06 19:25:55 +02006072 /* Persistence cookies in passive, rewrite or insert mode have the
6073 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006074 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006075 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006076 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006077 * For cookies in prefix mode, the form is :
6078 *
6079 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006080 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006081 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6082 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6083 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006084 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006085
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006086 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6087 * have the server ID between val_beg and delim, and the original cookie between
6088 * delim+1 and val_end. Otherwise, delim==val_end :
6089 *
6090 * Cookie: NAME=SRV; # in all but prefix modes
6091 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6092 * | || || | |+-> next
6093 * | || || | +--> val_end
6094 * | || || +---------> delim
6095 * | || |+------------> val_beg
6096 * | || +-------------> att_end = equal
6097 * | |+-----------------> att_beg
6098 * | +------------------> prev
6099 * +-------------------------> hdr_beg
6100 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006101
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006102 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006103 for (delim = val_beg; delim < val_end; delim++)
6104 if (*delim == COOKIE_DELIM)
6105 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006106 } else {
6107 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006108 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006109 /* Now check if the cookie contains a date field, which would
6110 * appear after a vertical bar ('|') just after the server name
6111 * and before the delimiter.
6112 */
6113 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6114 if (vbar1) {
6115 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006116 * right is the last seen date. It is a base64 encoded
6117 * 30-bit value representing the UNIX date since the
6118 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006119 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006120 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006121 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006122 if (val_end - vbar1 >= 5) {
6123 val = b64tos30(vbar1);
6124 if (val > 0)
6125 txn->cookie_last_date = val << 2;
6126 }
6127 /* look for a second vertical bar */
6128 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6129 if (vbar1 && (val_end - vbar1 > 5)) {
6130 val = b64tos30(vbar1 + 1);
6131 if (val > 0)
6132 txn->cookie_first_date = val << 2;
6133 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006134 }
6135 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006136
Willy Tarreauf64d1412010-10-07 20:06:11 +02006137 /* if the cookie has an expiration date and the proxy wants to check
6138 * it, then we do that now. We first check if the cookie is too old,
6139 * then only if it has expired. We detect strict overflow because the
6140 * time resolution here is not great (4 seconds). Cookies with dates
6141 * in the future are ignored if their offset is beyond one day. This
6142 * allows an admin to fix timezone issues without expiring everyone
6143 * and at the same time avoids keeping unwanted side effects for too
6144 * long.
6145 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006146 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6147 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006148 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006149 txn->flags &= ~TX_CK_MASK;
6150 txn->flags |= TX_CK_OLD;
6151 delim = val_beg; // let's pretend we have not found the cookie
6152 txn->cookie_first_date = 0;
6153 txn->cookie_last_date = 0;
6154 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006155 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6156 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006157 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006158 txn->flags &= ~TX_CK_MASK;
6159 txn->flags |= TX_CK_EXPIRED;
6160 delim = val_beg; // let's pretend we have not found the cookie
6161 txn->cookie_first_date = 0;
6162 txn->cookie_last_date = 0;
6163 }
6164
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006165 /* Here, we'll look for the first running server which supports the cookie.
6166 * This allows to share a same cookie between several servers, for example
6167 * to dedicate backup servers to specific servers only.
6168 * However, to prevent clients from sticking to cookie-less backup server
6169 * when they have incidentely learned an empty cookie, we simply ignore
6170 * empty cookies and mark them as invalid.
6171 * The same behaviour is applied when persistence must be ignored.
6172 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006173 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006174 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006175
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006176 while (srv) {
6177 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6178 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006179 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006180 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006181 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006182 /* we found the server and we can use it */
6183 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006184 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006185 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006186 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006187 break;
6188 } else {
6189 /* we found a server, but it's down,
6190 * mark it as such and go on in case
6191 * another one is available.
6192 */
6193 txn->flags &= ~TX_CK_MASK;
6194 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006195 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006196 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006197 srv = srv->next;
6198 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006199
Willy Tarreauf64d1412010-10-07 20:06:11 +02006200 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006201 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006202 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006203 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006204 txn->flags |= TX_CK_UNUSED;
6205 else
6206 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006207 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006208
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006209 /* depending on the cookie mode, we may have to either :
6210 * - delete the complete cookie if we're in insert+indirect mode, so that
6211 * the server never sees it ;
6212 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlant5ba80252018-11-15 09:25:36 -08006213 * application cookie so that it does not get accidently removed later,
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006214 * if we're in cookie prefix mode
6215 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006216 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006217 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006218
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006219 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006220 val_end += delta;
6221 next += delta;
6222 hdr_end += delta;
6223 hdr_next += delta;
6224 cur_hdr->len += delta;
6225 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006226
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006227 del_from = NULL;
6228 preserve_hdr = 1; /* we want to keep this cookie */
6229 }
6230 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006231 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006232 del_from = prev;
6233 }
6234 } else {
6235 /* This is not our cookie, so we must preserve it. But if we already
6236 * scheduled another cookie for removal, we cannot remove the
6237 * complete header, but we can remove the previous block itself.
6238 */
6239 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006240
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006241 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006242 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006243 if (att_beg >= del_from)
6244 att_beg += delta;
6245 if (att_end >= del_from)
6246 att_end += delta;
6247 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006248 val_end += delta;
6249 next += delta;
6250 hdr_end += delta;
6251 hdr_next += delta;
6252 cur_hdr->len += delta;
6253 http_msg_move_end(&txn->req, delta);
6254 prev = del_from;
6255 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006256 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006257 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006258
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006259 /* continue with next cookie on this header line */
6260 att_beg = next;
6261 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006262
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006263 /* There are no more cookies on this line.
6264 * We may still have one (or several) marked for deletion at the
6265 * end of the line. We must do this now in two ways :
6266 * - if some cookies must be preserved, we only delete from the
6267 * mark to the end of line ;
6268 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006269 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006270 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006271 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006272 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006273 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006274 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006275 cur_hdr->len += delta;
6276 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006277 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006278
6279 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006280 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6281 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006282 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006283 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006284 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006285 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006286 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006287 }
6288
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006289 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006290 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006291 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006292}
6293
6294
Willy Tarreaua15645d2007-03-18 16:22:39 +01006295/* Iterate the same filter through all response headers contained in <rtr>.
6296 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6297 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006298int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006299{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006300 char *cur_ptr, *cur_end, *cur_next;
6301 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006302 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006303 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006304 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006305
6306 last_hdr = 0;
6307
Willy Tarreauf37954d2018-06-15 18:31:02 +02006308 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006309 old_idx = 0;
6310
6311 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006312 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006313 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006314 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006315 (exp->action == ACT_ALLOW ||
6316 exp->action == ACT_DENY))
6317 return 0;
6318
6319 cur_idx = txn->hdr_idx.v[old_idx].next;
6320 if (!cur_idx)
6321 break;
6322
6323 cur_hdr = &txn->hdr_idx.v[cur_idx];
6324 cur_ptr = cur_next;
6325 cur_end = cur_ptr + cur_hdr->len;
6326 cur_next = cur_end + cur_hdr->cr + 1;
6327
6328 /* Now we have one header between cur_ptr and cur_end,
6329 * and the next header starts at cur_next.
6330 */
6331
Willy Tarreau15a53a42015-01-21 13:39:42 +01006332 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006333 switch (exp->action) {
6334 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006335 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006336 last_hdr = 1;
6337 break;
6338
6339 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006340 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006341 last_hdr = 1;
6342 break;
6343
6344 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006345 len = exp_replace(trash.area,
6346 trash.size, cur_ptr,
6347 exp->replace, pmatch);
6348 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006349 return -1;
6350
Willy Tarreau6e27be12018-08-22 04:46:47 +02006351 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6352
Willy Tarreaua15645d2007-03-18 16:22:39 +01006353 /* FIXME: if the user adds a newline in the replacement, the
6354 * index will not be recalculated for now, and the new line
6355 * will not be counted as a new header.
6356 */
6357
6358 cur_end += delta;
6359 cur_next += delta;
6360 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006361 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006362 break;
6363
6364 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006365 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006366 cur_next += delta;
6367
Willy Tarreaufa355d42009-11-29 18:12:29 +01006368 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006369 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6370 txn->hdr_idx.used--;
6371 cur_hdr->len = 0;
6372 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006373 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006374 break;
6375
6376 }
6377 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006378
6379 /* keep the link from this header to next one in case of later
6380 * removal of next header.
6381 */
6382 old_idx = cur_idx;
6383 }
6384 return 0;
6385}
6386
6387
6388/* Apply the filter to the status line in the response buffer <rtr>.
6389 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6390 * or -1 if a replacement resulted in an invalid status line.
6391 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006392int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006393{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006394 char *cur_ptr, *cur_end;
6395 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006396 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006397 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006398
Willy Tarreau3d300592007-03-18 18:34:41 +01006399 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006400 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006401 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006402 (exp->action == ACT_ALLOW ||
6403 exp->action == ACT_DENY))
6404 return 0;
6405 else if (exp->action == ACT_REMOVE)
6406 return 0;
6407
6408 done = 0;
6409
Willy Tarreauf37954d2018-06-15 18:31:02 +02006410 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006411 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006412
6413 /* Now we have the status line between cur_ptr and cur_end */
6414
Willy Tarreau15a53a42015-01-21 13:39:42 +01006415 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006416 switch (exp->action) {
6417 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006418 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006419 done = 1;
6420 break;
6421
6422 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006423 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006424 done = 1;
6425 break;
6426
6427 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006428 len = exp_replace(trash.area, trash.size,
6429 cur_ptr, exp->replace, pmatch);
6430 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006431 return -1;
6432
Willy Tarreau6e27be12018-08-22 04:46:47 +02006433 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6434
Willy Tarreaua15645d2007-03-18 16:22:39 +01006435 /* FIXME: if the user adds a newline in the replacement, the
6436 * index will not be recalculated for now, and the new line
6437 * will not be counted as a new header.
6438 */
6439
Willy Tarreaufa355d42009-11-29 18:12:29 +01006440 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006441 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006442 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006443 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006444 cur_ptr, cur_end + 1,
6445 NULL, NULL);
6446 if (unlikely(!cur_end))
6447 return -1;
6448
6449 /* we have a full respnse and we know that we have either a CR
6450 * or an LF at <ptr>.
6451 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006452 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006453 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006454 /* there is no point trying this regex on headers */
6455 return 1;
6456 }
6457 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006458 return done;
6459}
6460
6461
6462
6463/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006464 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006465 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6466 * unparsable response.
6467 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006468int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006469{
Willy Tarreau192252e2015-04-04 01:47:55 +02006470 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006471 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006472 struct hdr_exp *exp;
6473
6474 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006475 int ret;
6476
6477 /*
6478 * The interleaving of transformations and verdicts
6479 * makes it difficult to decide to continue or stop
6480 * the evaluation.
6481 */
6482
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006483 if (txn->flags & TX_SVDENY)
6484 break;
6485
Willy Tarreau3d300592007-03-18 18:34:41 +01006486 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006487 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6488 exp->action == ACT_PASS)) {
6489 exp = exp->next;
6490 continue;
6491 }
6492
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006493 /* if this filter had a condition, evaluate it now and skip to
6494 * next filter if the condition does not match.
6495 */
6496 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006497 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006498 ret = acl_pass(ret);
6499 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6500 ret = !ret;
6501 if (!ret)
6502 continue;
6503 }
6504
Willy Tarreaua15645d2007-03-18 16:22:39 +01006505 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006506 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006507 if (unlikely(ret < 0))
6508 return -1;
6509
6510 if (likely(ret == 0)) {
6511 /* The filter did not match the response, it can be
6512 * iterated through all headers.
6513 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006514 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6515 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006516 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006517 }
6518 return 0;
6519}
6520
6521
Willy Tarreaua15645d2007-03-18 16:22:39 +01006522/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006523 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006524 * desirable to call it only when needed. This function is also used when we
6525 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006526 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006527void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006528{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006529 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006530 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006531 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006532 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006533 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006534 char *hdr_beg, *hdr_end, *hdr_next;
6535 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006536
Willy Tarreaua15645d2007-03-18 16:22:39 +01006537 /* Iterate through the headers.
6538 * we start with the start line.
6539 */
6540 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006541 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006542
6543 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6544 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006545 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006546
6547 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006548 hdr_beg = hdr_next;
6549 hdr_end = hdr_beg + cur_hdr->len;
6550 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006551
Willy Tarreau24581ba2010-08-31 22:39:35 +02006552 /* We have one full header between hdr_beg and hdr_end, and the
6553 * next header starts at hdr_next. We're only interested in
6554 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006555 */
6556
Willy Tarreau24581ba2010-08-31 22:39:35 +02006557 is_cookie2 = 0;
6558 prev = hdr_beg + 10;
6559 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006560 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006561 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6562 if (!val) {
6563 old_idx = cur_idx;
6564 continue;
6565 }
6566 is_cookie2 = 1;
6567 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006568 }
6569
Willy Tarreau24581ba2010-08-31 22:39:35 +02006570 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6571 * <prev> points to the colon.
6572 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006573 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006574
Willy Tarreau24581ba2010-08-31 22:39:35 +02006575 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6576 * check-cache is enabled) and we are not interested in checking
6577 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006578 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006579 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006580 return;
6581
Willy Tarreau24581ba2010-08-31 22:39:35 +02006582 /* OK so now we know we have to process this response cookie.
6583 * The format of the Set-Cookie header is slightly different
6584 * from the format of the Cookie header in that it does not
6585 * support the comma as a cookie delimiter (thus the header
6586 * cannot be folded) because the Expires attribute described in
6587 * the original Netscape's spec may contain an unquoted date
6588 * with a comma inside. We have to live with this because
6589 * many browsers don't support Max-Age and some browsers don't
6590 * support quoted strings. However the Set-Cookie2 header is
6591 * clean.
6592 *
6593 * We have to keep multiple pointers in order to support cookie
6594 * removal at the beginning, middle or end of header without
6595 * corrupting the header (in case of set-cookie2). A special
6596 * pointer, <scav> points to the beginning of the set-cookie-av
6597 * fields after the first semi-colon. The <next> pointer points
6598 * either to the end of line (set-cookie) or next unquoted comma
6599 * (set-cookie2). All of these headers are valid :
6600 *
6601 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6602 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6603 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6604 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6605 * | | | | | | | | | |
6606 * | | | | | | | | +-> next hdr_end <--+
6607 * | | | | | | | +------------> scav
6608 * | | | | | | +--------------> val_end
6609 * | | | | | +--------------------> val_beg
6610 * | | | | +----------------------> equal
6611 * | | | +------------------------> att_end
6612 * | | +----------------------------> att_beg
6613 * | +------------------------------> prev
6614 * +-----------------------------------------> hdr_beg
6615 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006616
Willy Tarreau24581ba2010-08-31 22:39:35 +02006617 for (; prev < hdr_end; prev = next) {
6618 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006619
Willy Tarreau24581ba2010-08-31 22:39:35 +02006620 /* find att_beg */
6621 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006622 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006623 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006624
Willy Tarreau24581ba2010-08-31 22:39:35 +02006625 /* find att_end : this is the first character after the last non
6626 * space before the equal. It may be equal to hdr_end.
6627 */
6628 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006629
Willy Tarreau24581ba2010-08-31 22:39:35 +02006630 while (equal < hdr_end) {
6631 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6632 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006633 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006634 continue;
6635 att_end = equal;
6636 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006637
Willy Tarreau24581ba2010-08-31 22:39:35 +02006638 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6639 * is between <att_beg> and <equal>, both may be identical.
6640 */
6641
6642 /* look for end of cookie if there is an equal sign */
6643 if (equal < hdr_end && *equal == '=') {
6644 /* look for the beginning of the value */
6645 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006646 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006647 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006648
Willy Tarreau24581ba2010-08-31 22:39:35 +02006649 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006650 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006651
6652 /* make val_end point to the first white space or delimitor after the value */
6653 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006654 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006655 val_end--;
6656 } else {
6657 /* <equal> points to next comma, semi-colon or EOL */
6658 val_beg = val_end = next = equal;
6659 }
6660
6661 if (next < hdr_end) {
6662 /* Set-Cookie2 supports multiple cookies, and <next> points to
6663 * a colon or semi-colon before the end. So skip all attr-value
6664 * pairs and look for the next comma. For Set-Cookie, since
6665 * commas are permitted in values, skip to the end.
6666 */
6667 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02006668 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006669 else
6670 next = hdr_end;
6671 }
6672
6673 /* Now everything is as on the diagram above */
6674
6675 /* Ignore cookies with no equal sign */
6676 if (equal == val_end)
6677 continue;
6678
6679 /* If there are spaces around the equal sign, we need to
6680 * strip them otherwise we'll get trouble for cookie captures,
6681 * or even for rewrites. Since this happens extremely rarely,
6682 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006683 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006684 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6685 int stripped_before = 0;
6686 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006687
Willy Tarreau24581ba2010-08-31 22:39:35 +02006688 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006689 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006690 equal += stripped_before;
6691 val_beg += stripped_before;
6692 }
6693
6694 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006695 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006696 val_beg += stripped_after;
6697 stripped_before += stripped_after;
6698 }
6699
6700 val_end += stripped_before;
6701 next += stripped_before;
6702 hdr_end += stripped_before;
6703 hdr_next += stripped_before;
6704 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006705 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006706 }
6707
6708 /* First, let's see if we want to capture this cookie. We check
6709 * that we don't already have a server side cookie, because we
6710 * can only capture one. Also as an optimisation, we ignore
6711 * cookies shorter than the declared name.
6712 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006713 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006714 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006715 (val_end - att_beg >= sess->fe->capture_namelen) &&
6716 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006717 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006718 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006719 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01006720 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006721 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006722 if (log_len > sess->fe->capture_len)
6723 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01006724 memcpy(txn->srv_cookie, att_beg, log_len);
6725 txn->srv_cookie[log_len] = 0;
6726 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006727 }
6728
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006729 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006730 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006731 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006732 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6733 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006734 /* assume passive cookie by default */
6735 txn->flags &= ~TX_SCK_MASK;
6736 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006737
6738 /* If the cookie is in insert mode on a known server, we'll delete
6739 * this occurrence because we'll insert another one later.
6740 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006741 * a direct access.
6742 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006743 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006744 /* The "preserve" flag was set, we don't want to touch the
6745 * server's cookie.
6746 */
6747 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006748 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006749 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006750 /* this cookie must be deleted */
6751 if (*prev == ':' && next == hdr_end) {
6752 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006753 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006754 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6755 txn->hdr_idx.used--;
6756 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006757 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006758 hdr_next += delta;
6759 http_msg_move_end(&txn->rsp, delta);
6760 /* note: while both invalid now, <next> and <hdr_end>
6761 * are still equal, so the for() will stop as expected.
6762 */
6763 } else {
6764 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006765 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006766 next = prev;
6767 hdr_end += delta;
6768 hdr_next += delta;
6769 cur_hdr->len += delta;
6770 http_msg_move_end(&txn->rsp, delta);
6771 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006772 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006773 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006774 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006775 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006776 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006777 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006778 * with this server since we know it.
6779 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006780 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006781 next += delta;
6782 hdr_end += delta;
6783 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006784 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006785 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006786
Willy Tarreauf1348312010-10-07 15:54:11 +02006787 txn->flags &= ~TX_SCK_MASK;
6788 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006789 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006790 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006791 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006792 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006793 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006794 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006795 next += delta;
6796 hdr_end += delta;
6797 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006798 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006799 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006800
Willy Tarreau827aee92011-03-10 16:55:02 +01006801 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006802 txn->flags &= ~TX_SCK_MASK;
6803 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006804 }
6805 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006806 /* that's done for this cookie, check the next one on the same
6807 * line when next != hdr_end (only if is_cookie2).
6808 */
6809 }
6810 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006811 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006812 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006813}
6814
6815
Willy Tarreaua15645d2007-03-18 16:22:39 +01006816/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006817 * Parses the Cache-Control and Pragma request header fields to determine if
6818 * the request may be served from the cache and/or if it is cacheable. Updates
6819 * s->txn->flags.
6820 */
6821void check_request_for_cacheability(struct stream *s, struct channel *chn)
6822{
6823 struct http_txn *txn = s->txn;
6824 char *p1, *p2;
6825 char *cur_ptr, *cur_end, *cur_next;
6826 int pragma_found;
6827 int cc_found;
6828 int cur_idx;
6829
Christopher Faulet25a02f62018-10-24 12:00:25 +02006830 if (IS_HTX_STRM(s))
6831 return htx_check_request_for_cacheability(s, chn);
6832
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006833 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
6834 return; /* nothing more to do here */
6835
6836 cur_idx = 0;
6837 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006838 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006839
6840 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6841 struct hdr_idx_elem *cur_hdr;
6842 int val;
6843
6844 cur_hdr = &txn->hdr_idx.v[cur_idx];
6845 cur_ptr = cur_next;
6846 cur_end = cur_ptr + cur_hdr->len;
6847 cur_next = cur_end + cur_hdr->cr + 1;
6848
6849 /* We have one full header between cur_ptr and cur_end, and the
6850 * next header starts at cur_next.
6851 */
6852
6853 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6854 if (val) {
6855 if ((cur_end - (cur_ptr + val) >= 8) &&
6856 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6857 pragma_found = 1;
6858 continue;
6859 }
6860 }
6861
William Lallemand8a16fe02018-05-22 11:04:33 +02006862 /* Don't use the cache and don't try to store if we found the
6863 * Authorization header */
6864 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
6865 if (val) {
6866 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6867 txn->flags |= TX_CACHE_IGNORE;
6868 continue;
6869 }
6870
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006871 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6872 if (!val)
6873 continue;
6874
6875 /* OK, right now we know we have a cache-control header at cur_ptr */
6876 cc_found = 1;
6877 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
6878
6879 if (p1 >= cur_end) /* no more info */
6880 continue;
6881
6882 /* p1 is at the beginning of the value */
6883 p2 = p1;
6884 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
6885 p2++;
6886
6887 /* we have a complete value between p1 and p2. We don't check the
6888 * values after max-age, max-stale nor min-fresh, we simply don't
6889 * use the cache when they're specified.
6890 */
6891 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
6892 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
6893 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
6894 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
6895 txn->flags |= TX_CACHE_IGNORE;
6896 continue;
6897 }
6898
6899 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
6900 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6901 continue;
6902 }
6903 }
6904
6905 /* RFC7234#5.4:
6906 * When the Cache-Control header field is also present and
6907 * understood in a request, Pragma is ignored.
6908 * When the Cache-Control header field is not present in a
6909 * request, caches MUST consider the no-cache request
6910 * pragma-directive as having the same effect as if
6911 * "Cache-Control: no-cache" were present.
6912 */
6913 if (!cc_found && pragma_found)
6914 txn->flags |= TX_CACHE_IGNORE;
6915}
6916
6917/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01006918 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006919 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006920void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006921{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006922 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006923 char *p1, *p2;
6924
6925 char *cur_ptr, *cur_end, *cur_next;
6926 int cur_idx;
6927
Christopher Faulet25a02f62018-10-24 12:00:25 +02006928
6929 if (IS_HTX_STRM(s))
6930 return htx_check_response_for_cacheability(s, rtr);
6931
Willy Tarreau12b32f22017-12-21 16:08:09 +01006932 if (txn->status < 200) {
6933 /* do not try to cache interim responses! */
6934 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006935 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01006936 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006937
6938 /* Iterate through the headers.
6939 * we start with the start line.
6940 */
6941 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006942 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006943
6944 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6945 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006946 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006947
6948 cur_hdr = &txn->hdr_idx.v[cur_idx];
6949 cur_ptr = cur_next;
6950 cur_end = cur_ptr + cur_hdr->len;
6951 cur_next = cur_end + cur_hdr->cr + 1;
6952
6953 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01006954 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006955 */
6956
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006957 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6958 if (val) {
6959 if ((cur_end - (cur_ptr + val) >= 8) &&
6960 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6961 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6962 return;
6963 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006964 }
6965
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006966 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6967 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006968 continue;
6969
6970 /* OK, right now we know we have a cache-control header at cur_ptr */
6971
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006972 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006973
6974 if (p1 >= cur_end) /* no more info */
6975 continue;
6976
6977 /* p1 is at the beginning of the value */
6978 p2 = p1;
6979
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006980 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006981 p2++;
6982
6983 /* we have a complete value between p1 and p2 */
6984 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01006985 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6986 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
6987 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6988 continue;
6989 }
6990
Willy Tarreaua15645d2007-03-18 16:22:39 +01006991 /* we have something of the form no-cache="set-cookie" */
6992 if ((cur_end - p1 >= 21) &&
6993 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6994 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006995 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006996 continue;
6997 }
6998
6999 /* OK, so we know that either p2 points to the end of string or to a comma */
7000 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007001 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007002 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007003 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007004 return;
7005 }
7006
7007 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007008 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007009 continue;
7010 }
7011 }
7012}
7013
Willy Tarreau58f10d72006-12-04 02:26:12 +01007014
Willy Tarreaub2513902006-12-17 14:52:38 +01007015/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007016 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007017 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007018 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007019 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007020 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007021 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007022 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007023 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007024int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007025{
7026 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007027 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007028 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007029
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007030 if (!uri_auth)
7031 return 0;
7032
Cyril Bonté70be45d2010-10-12 00:14:35 +02007033 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007034 return 0;
7035
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007036 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007037 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007038 return 0;
7039
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007040 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007041 return 0;
7042
Willy Tarreaub2513902006-12-17 14:52:38 +01007043 return 1;
7044}
7045
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007046/* Append the description of what is present in error snapshot <es> into <out>.
7047 * The description must be small enough to always fit in a trash. The output
7048 * buffer may be the trash so the trash must not be used inside this function.
7049 */
7050void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7051{
Christopher Fauleted26fb82018-11-29 22:53:30 +01007052 chunk_appendf(out,
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007053 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7054 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7055 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7056 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7057 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7058 es->ctx.http.m_flags, es->ctx.http.m_clen,
7059 es->ctx.http.m_blen, es->ctx.http.b_flags);
7060}
7061
Willy Tarreau4076a152009-04-02 15:18:36 +02007062/*
7063 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007064 * By default it tries to report the error position as msg->err_pos. However if
7065 * this one is not set, it will then report msg->next, which is the last known
7066 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007067 * displays buffers as a contiguous area starting at buf->p. The direction is
7068 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007069 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007070void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007071 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007072 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007073{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007074 union error_snapshot_ctx ctx;
7075 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007076
Willy Tarreauef3ca732018-09-07 15:47:35 +02007077 /* http-specific part now */
7078 ctx.http.sid = s->uniq_id;
7079 ctx.http.state = state;
7080 ctx.http.b_flags = msg->chn->flags;
7081 ctx.http.s_flags = s->flags;
7082 ctx.http.t_flags = s->txn->flags;
7083 ctx.http.m_flags = msg->flags;
7084 ctx.http.m_clen = msg->chunk_len;
7085 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007086
Willy Tarreauef3ca732018-09-07 15:47:35 +02007087 ofs = msg->chn->total - ci_data(msg->chn);
7088 if (ofs < 0)
7089 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007090
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007091 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007092 other_end, s->target,
7093 strm_sess(s), &msg->chn->buf,
7094 ofs, co_data(msg->chn),
7095 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7096 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007097}
Willy Tarreaub2513902006-12-17 14:52:38 +01007098
Willy Tarreaubaaee002006-06-26 02:48:02 +02007099/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007100 * Print a debug line with a header. Always stop at the first CR or LF char,
7101 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7102 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007103 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007104void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007105{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007106 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007107 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007108
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007109 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007110 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007111 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007112 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 +02007113
7114 for (max = 0; start + max < end; max++)
7115 if (start[max] == '\r' || start[max] == '\n')
7116 break;
7117
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007118 UBOUND(max, trash.size - trash.data - 3);
7119 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7120 trash.area[trash.data++] = '\n';
7121 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007122}
7123
Willy Tarreaueee5b512015-04-03 23:46:31 +02007124
7125/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7126 * The hdr_idx is allocated as well. In case of allocation failure, everything
7127 * allocated is freed and NULL is returned. Otherwise the new transaction is
7128 * assigned to the stream and returned.
7129 */
7130struct http_txn *http_alloc_txn(struct stream *s)
7131{
7132 struct http_txn *txn = s->txn;
7133
7134 if (txn)
7135 return txn;
7136
Willy Tarreaubafbe012017-11-24 17:34:44 +01007137 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007138 if (!txn)
7139 return txn;
7140
7141 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007142 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007143 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007144 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007145 return NULL;
7146 }
7147
7148 s->txn = txn;
7149 return txn;
7150}
7151
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007152void http_txn_reset_req(struct http_txn *txn)
7153{
7154 txn->req.flags = 0;
7155 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7156 txn->req.next = 0;
7157 txn->req.chunk_len = 0LL;
7158 txn->req.body_len = 0LL;
7159 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7160}
7161
7162void http_txn_reset_res(struct http_txn *txn)
7163{
7164 txn->rsp.flags = 0;
7165 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7166 txn->rsp.next = 0;
7167 txn->rsp.chunk_len = 0LL;
7168 txn->rsp.body_len = 0LL;
7169 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7170}
7171
Willy Tarreau0937bc42009-12-22 15:03:09 +01007172/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007173 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007174 * the required fields are properly allocated and that we only need to (re)init
7175 * them. This should be used before processing any new request.
7176 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007177void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007178{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007179 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007180 struct proxy *fe = strm_fe(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02007181 struct conn_stream *cs = objt_cs(s->si[0].end);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007182
Christopher Fauletf2824e62018-10-01 12:12:37 +02007183 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
7184 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
7185 : 0);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007186 txn->status = -1;
Willy Tarreauc9036c02019-01-11 19:38:25 +01007187 *(unsigned int *)txn->cache_hash = 0;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007188
Willy Tarreauf64d1412010-10-07 20:06:11 +02007189 txn->cookie_first_date = 0;
7190 txn->cookie_last_date = 0;
7191
Willy Tarreaueee5b512015-04-03 23:46:31 +02007192 txn->srv_cookie = NULL;
7193 txn->cli_cookie = NULL;
7194 txn->uri = NULL;
7195
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007196 http_txn_reset_req(txn);
7197 http_txn_reset_res(txn);
7198
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007199 txn->req.chn = &s->req;
7200 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007201
7202 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007203
7204 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7205 if (fe->options2 & PR_O2_REQBUG_OK)
7206 txn->req.err_pos = -1; /* let buggy requests pass */
7207
Willy Tarreau0937bc42009-12-22 15:03:09 +01007208 if (txn->hdr_idx.v)
7209 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007210
7211 vars_init(&s->vars_txn, SCOPE_TXN);
7212 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007213}
7214
7215/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007216void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007217{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007218 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007219 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007220
7221 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007222 pool_free(pool_head_requri, txn->uri);
7223 pool_free(pool_head_capture, txn->cli_cookie);
7224 pool_free(pool_head_capture, txn->srv_cookie);
7225 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007226
William Lallemanda73203e2012-03-12 12:48:57 +01007227 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007228 txn->uri = NULL;
7229 txn->srv_cookie = NULL;
7230 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007231
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007232 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007233 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007234 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007235 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007236 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007237 }
7238
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007239 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007240 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007241 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007242 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007243 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007244 }
7245
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007246 if (!LIST_ISEMPTY(&s->vars_txn.head))
7247 vars_prune(&s->vars_txn, s->sess, s);
7248 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7249 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007250}
7251
7252/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007253void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007254{
7255 http_end_txn(s);
7256 http_init_txn(s);
7257
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007258 /* reinitialise the current rule list pointer to NULL. We are sure that
7259 * any rulelist match the NULL pointer.
7260 */
7261 s->current_rule_list = NULL;
7262
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007263 s->be = strm_fe(s);
7264 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007265 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007266 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007267 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007268 /* re-init store persistence */
7269 s->store_count = 0;
Olivier Houcharda798bf52019-03-08 18:52:00 +01007270 s->uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007271
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007272 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007273
Willy Tarreau739cfba2010-01-25 23:11:14 +01007274 /* We must trim any excess data from the response buffer, because we
7275 * may have blocked an invalid response from a server that we don't
Joseph Herlant5ba80252018-11-15 09:25:36 -08007276 * want to accidently forward once we disable the analysers, nor do
Willy Tarreau739cfba2010-01-25 23:11:14 +01007277 * we want those data to come along with next response. A typical
7278 * example of such data would be from a buggy server responding to
7279 * a HEAD with some data, or sending more than the advertised
7280 * content-length.
7281 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007282 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007283 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007284
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007285 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007286 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007287
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007288 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007289 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007290
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007291 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007292 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007293
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007294 s->req.rex = TICK_ETERNITY;
7295 s->req.wex = TICK_ETERNITY;
7296 s->req.analyse_exp = TICK_ETERNITY;
7297 s->res.rex = TICK_ETERNITY;
7298 s->res.wex = TICK_ETERNITY;
7299 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007300 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007301}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007302
Willy Tarreau79e57332018-10-02 16:01:16 +02007303/* This function executes one of the set-{method,path,query,uri} actions. It
7304 * takes the string from the variable 'replace' with length 'len', then modifies
7305 * the relevant part of the request line accordingly. Then it updates various
7306 * pointers to the next elements which were moved, and the total buffer length.
7307 * It finds the action to be performed in p[2], previously filled by function
7308 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7309 * error, though this can be revisited when this code is finally exploited.
7310 *
7311 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7312 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007313 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007314 * In query string case, the mark question '?' must be set at the start of the
7315 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007316 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007317int http_replace_req_line(int action, const char *replace, int len,
7318 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007319{
Willy Tarreau79e57332018-10-02 16:01:16 +02007320 struct http_txn *txn = s->txn;
7321 char *cur_ptr, *cur_end;
7322 int offset = 0;
7323 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007324
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007325 if (IS_HTX_STRM(s))
7326 return htx_req_replace_stline(action, replace, len, px, s);
7327
Willy Tarreau79e57332018-10-02 16:01:16 +02007328 switch (action) {
7329 case 0: // method
7330 cur_ptr = ci_head(&s->req);
7331 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007332
Willy Tarreau79e57332018-10-02 16:01:16 +02007333 /* adjust req line offsets and lengths */
7334 delta = len - offset - (cur_end - cur_ptr);
7335 txn->req.sl.rq.m_l += delta;
7336 txn->req.sl.rq.u += delta;
7337 txn->req.sl.rq.v += delta;
7338 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007339
Willy Tarreau79e57332018-10-02 16:01:16 +02007340 case 1: // path
7341 cur_ptr = http_txn_get_path(txn);
7342 if (!cur_ptr)
7343 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007344
Willy Tarreau79e57332018-10-02 16:01:16 +02007345 cur_end = cur_ptr;
7346 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7347 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007348
Willy Tarreau79e57332018-10-02 16:01:16 +02007349 /* adjust req line offsets and lengths */
7350 delta = len - offset - (cur_end - cur_ptr);
7351 txn->req.sl.rq.u_l += delta;
7352 txn->req.sl.rq.v += delta;
7353 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007354
Willy Tarreau79e57332018-10-02 16:01:16 +02007355 case 2: // query
7356 offset = 1;
7357 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7358 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7359 while (cur_ptr < cur_end && *cur_ptr != '?')
7360 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007361
Willy Tarreau79e57332018-10-02 16:01:16 +02007362 /* skip the question mark or indicate that we must insert it
7363 * (but only if the format string is not empty then).
7364 */
7365 if (cur_ptr < cur_end)
7366 cur_ptr++;
7367 else if (len > 1)
7368 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007369
Willy Tarreau79e57332018-10-02 16:01:16 +02007370 /* adjust req line offsets and lengths */
7371 delta = len - offset - (cur_end - cur_ptr);
7372 txn->req.sl.rq.u_l += delta;
7373 txn->req.sl.rq.v += delta;
7374 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007375
Willy Tarreau79e57332018-10-02 16:01:16 +02007376 case 3: // uri
7377 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7378 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007379
Willy Tarreau79e57332018-10-02 16:01:16 +02007380 /* adjust req line offsets and lengths */
7381 delta = len - offset - (cur_end - cur_ptr);
7382 txn->req.sl.rq.u_l += delta;
7383 txn->req.sl.rq.v += delta;
7384 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007385
Willy Tarreau79e57332018-10-02 16:01:16 +02007386 default:
7387 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007388 }
7389
Willy Tarreau79e57332018-10-02 16:01:16 +02007390 /* commit changes and adjust end of message */
7391 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7392 txn->req.sl.rq.l += delta;
7393 txn->hdr_idx.v[0].len += delta;
7394 http_msg_move_end(&txn->req, delta);
7395 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007396}
7397
Willy Tarreau79e57332018-10-02 16:01:16 +02007398/* This function replace the HTTP status code and the associated message. The
7399 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007400 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007401void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007402{
Willy Tarreau79e57332018-10-02 16:01:16 +02007403 struct http_txn *txn = s->txn;
7404 char *cur_ptr, *cur_end;
7405 int delta;
7406 char *res;
7407 int c_l;
7408 const char *msg = reason;
7409 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007410
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007411 if (IS_HTX_STRM(s))
7412 return htx_res_set_status(status, reason, s);
7413
Willy Tarreau79e57332018-10-02 16:01:16 +02007414 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007415
Willy Tarreau79e57332018-10-02 16:01:16 +02007416 res = ultoa_o(status, trash.area, trash.size);
7417 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007418
Willy Tarreau79e57332018-10-02 16:01:16 +02007419 trash.area[c_l] = ' ';
7420 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007421
Willy Tarreau79e57332018-10-02 16:01:16 +02007422 /* Do we have a custom reason format string? */
7423 if (msg == NULL)
7424 msg = http_get_reason(status);
7425 msg_len = strlen(msg);
7426 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7427 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007428
Willy Tarreau79e57332018-10-02 16:01:16 +02007429 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7430 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007431
Willy Tarreau79e57332018-10-02 16:01:16 +02007432 /* commit changes and adjust message */
7433 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7434 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007435
Willy Tarreau79e57332018-10-02 16:01:16 +02007436 /* adjust res line offsets and lengths */
7437 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7438 txn->rsp.sl.st.c_l = c_l;
7439 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007440
Willy Tarreau79e57332018-10-02 16:01:16 +02007441 delta = trash.data - (cur_end - cur_ptr);
7442 txn->rsp.sl.st.l += delta;
7443 txn->hdr_idx.v[0].len += delta;
7444 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007445}
7446
Willy Tarreau58f10d72006-12-04 02:26:12 +01007447/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007448 * Local variables:
7449 * c-indent-level: 8
7450 * c-basic-offset: 8
7451 * End:
7452 */