blob: e471320a980f39e49875f8e7ddc87dc16c8e21ff [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 Lallemand9ed62032016-11-21 17:49:11 +010045#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Willy Tarreau8797c062007-05-07 00:55:35 +020047#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020048#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020049#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010050#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020052#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010053#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010054#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020055#include <proto/compression.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010056#include <proto/dns.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 Faulet03b9d8b2019-03-26 22:02:00 +0100499 return;
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) {
Christopher Faulet3d36d4e2019-09-04 09:39:42 +0200601 if (unlikely(!channel_is_rewritable(req) && co_data(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 Tarreau196a7df2019-09-12 14:01:40 +02001076 /* "chunked" mandatory if transfer-encoding is used */
1077 if (ctx.idx && !(msg->flags & HTTP_MSGF_TE_CHNK))
1078 goto return_bad_req;
1079
Willy Tarreau1c913912015-04-30 10:57:51 +02001080 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001081 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001082 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001083 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001084 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1085 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001086 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001087 signed long long cl;
1088
Willy Tarreauad14f752011-09-02 20:33:27 +02001089 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001090 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001091 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001092 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001093
Willy Tarreauad14f752011-09-02 20:33:27 +02001094 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001095 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001096 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001097 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001098
Willy Tarreauad14f752011-09-02 20:33:27 +02001099 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001100 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001101 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001102 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001103
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001104 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001105 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001106 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001107 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001108
Christopher Fauletbe821b92017-03-30 11:21:53 +02001109 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001110 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001111 }
1112
Willy Tarreau34dfc602015-05-01 10:09:49 +02001113 /* even bodyless requests have a known length */
1114 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001115
Willy Tarreau179085c2014-04-28 16:48:56 +02001116 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1117 * only change if both the request and the config reference something else.
1118 * Option httpclose by itself sets tunnel mode where headers are mangled.
1119 * However, if another mode is set, it will affect it (eg: server-close/
1120 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1121 * if FE and BE have the same settings (common). The method consists in
1122 * checking if options changed between the two calls (implying that either
1123 * one is non-null, or one of them is non-null and we are there for the first
1124 * time.
1125 */
1126 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001127 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001128 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001129
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001130 /* we may have to wait for the request's body */
1131 if ((s->be->options & PR_O_WREQ_BODY) &&
1132 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1133 req->analysers |= AN_REQ_HTTP_BODY;
1134
Willy Tarreau83ece462017-12-21 15:13:09 +01001135 /*
1136 * RFC7234#4:
1137 * A cache MUST write through requests with methods
1138 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1139 * the origin server; i.e., a cache is not allowed
1140 * to generate a reply to such a request before
1141 * having forwarded the request and having received
1142 * a corresponding response.
1143 *
1144 * RFC7231#4.2.1:
1145 * Of the request methods defined by this
1146 * specification, the GET, HEAD, OPTIONS, and TRACE
1147 * methods are defined to be safe.
1148 */
1149 if (likely(txn->meth == HTTP_METH_GET ||
1150 txn->meth == HTTP_METH_HEAD ||
1151 txn->meth == HTTP_METH_OPTIONS ||
1152 txn->meth == HTTP_METH_TRACE))
1153 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1154
Willy Tarreaud787e662009-07-07 10:14:51 +02001155 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001156 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001157 req->analyse_exp = TICK_ETERNITY;
1158 return 1;
1159
1160 return_bad_req:
1161 /* We centralize bad requests processing here */
1162 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1163 /* we detected a parsing error. We want to archive this request
1164 * in the dedicated proxy area for later troubleshooting.
1165 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001166 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001167 }
1168
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001169 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001170 txn->req.msg_state = HTTP_MSG_ERROR;
1171 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001172 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001173
Olivier Houcharda798bf52019-03-08 18:52:00 +01001174 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001175 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001176 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001177
1178 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001179 if (!(s->flags & SF_ERR_MASK))
1180 s->flags |= SF_ERR_PRXCOND;
1181 if (!(s->flags & SF_FINST_MASK))
1182 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001183
Christopher Faulet0184ea72017-01-05 14:06:34 +01001184 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001185 req->analyse_exp = TICK_ETERNITY;
1186 return 0;
1187}
1188
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001189
Willy Tarreau347a35d2013-11-22 17:51:09 +01001190/* This function prepares an applet to handle the stats. It can deal with the
1191 * "100-continue" expectation, check that admin rules are met for POST requests,
1192 * and program a response message if something was unexpected. It cannot fail
1193 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001194 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001195 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001196 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001197 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001198int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001199{
1200 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001201 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001202 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001203 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001204 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001205 struct uri_auth *uri_auth = s->be->uri_auth;
1206 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001207 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001208
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001209 appctx = si_appctx(si);
1210 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1211 appctx->st1 = appctx->st2 = 0;
1212 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1213 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001214 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001215 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001216
Willy Tarreauf37954d2018-06-15 18:31:02 +02001217 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001218 lookup = uri + uri_auth->uri_len;
1219
1220 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1221 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001222 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001223 break;
1224 }
1225 }
1226
1227 if (uri_auth->refresh) {
1228 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1229 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001230 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001231 break;
1232 }
1233 }
1234 }
1235
1236 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1237 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001238 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001239 break;
1240 }
1241 }
1242
Willy Tarreau1e62df92016-01-11 18:57:53 +01001243 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1244 if (memcmp(h, ";typed", 6) == 0) {
1245 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1246 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1247 break;
1248 }
1249 }
1250
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001251 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1252 if (memcmp(h, ";st=", 4) == 0) {
1253 int i;
1254 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001255 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001256 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1257 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001258 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001259 break;
1260 }
1261 }
1262 break;
1263 }
1264 }
1265
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001266 appctx->ctx.stats.scope_str = 0;
1267 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001268 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1269 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1270 int itx = 0;
1271 const char *h2;
1272 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1273 const char *err;
1274
1275 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1276 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001277 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001278 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1279 itx++;
1280 h++;
1281 }
1282
1283 if (itx > STAT_SCOPE_TXT_MAXLEN)
1284 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001285 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001286
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001287 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001288 memcpy(scope_txt, h2, itx);
1289 scope_txt[itx] = '\0';
1290 err = invalid_char(scope_txt);
1291 if (err) {
1292 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001293 appctx->ctx.stats.scope_str = 0;
1294 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001295 }
1296 break;
1297 }
1298 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001299
1300 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001301 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001302 int ret = 1;
1303
1304 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001305 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 +01001306 ret = acl_pass(ret);
1307 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1308 ret = !ret;
1309 }
1310
1311 if (ret) {
1312 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001313 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001314 break;
1315 }
1316 }
1317
Christopher Faulet5d45e382019-02-27 15:15:23 +01001318 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1319 appctx->st0 = STAT_HTTP_HEAD;
1320 else if (txn->meth == HTTP_METH_POST && (msg->flags & HTTP_MSGF_CNT_LEN)) {
Christopher Fauletbcf242a2019-03-01 11:36:26 +01001321 if (appctx->ctx.stats.flags & STAT_ADMIN)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001322 appctx->st0 = STAT_HTTP_POST;
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,
Dragan Dosen26743032019-04-30 15:54:36 +02001588 rule->arg.hdr_add.re, rule->action)) {
Christopher Fauletea827bd2018-11-15 15:34:11 +01001589 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 */
Christopher Faulete84289e2019-04-19 14:50:55 +02001758 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001759 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001760 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001761 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001762 else
1763 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001764 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02001765 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001766
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001767 free_trash_chunk(key);
1768 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001769 break;
1770 }
William Lallemand73025dd2014-04-24 14:38:37 +02001771
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001772 case ACT_HTTP_EARLY_HINT:
1773 if (!(txn->req.flags & HTTP_MSGF_VER_11))
1774 break;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001775 early_hints = http_apply_early_hint_rule(s, early_hints,
1776 rule->arg.early_hint.name,
1777 rule->arg.early_hint.name_len,
1778 &rule->arg.early_hint.fmt);
1779 if (!early_hints) {
Christopher Fauletea827bd2018-11-15 15:34:11 +01001780 rule_ret = HTTP_RULE_RES_DONE;
1781 goto end;
1782 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001783 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02001784 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01001785 if ((s->req.flags & CF_READ_ERROR) ||
1786 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01001787 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02001788 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02001789
Willy Tarreauacc98002015-09-27 23:34:39 +02001790 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02001791 case ACT_RET_ERR:
1792 case ACT_RET_CONT:
1793 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02001794 case ACT_RET_STOP:
Christopher Faulet4629d082019-07-04 11:27:15 +02001795 rule_ret = HTTP_RULE_RES_STOP;
1796 goto end;
1797 case ACT_RET_DONE:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001798 rule_ret = HTTP_RULE_RES_DONE;
1799 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02001800 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02001801 s->current_rule = rule;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001802 rule_ret = HTTP_RULE_RES_YIELD;
1803 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001804 }
William Lallemand73025dd2014-04-24 14:38:37 +02001805 break;
1806
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001807 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02001808 /* Note: only the first valid tracking parameter of each
1809 * applies.
1810 */
1811
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001812 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02001813 struct stktable *t;
1814 struct stksess *ts;
1815 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001816 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02001817
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02001818 t = rule->arg.trk_ctr.table.t;
1819 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 +02001820
1821 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001822 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02001823
1824 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001825 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1826 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1827 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001828 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02001829
Emeric Brun819fc6f2017-06-13 19:37:32 +02001830 if (ptr1)
1831 stktable_data_cast(ptr1, http_req_cnt)++;
1832
1833 if (ptr2)
1834 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
1835 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
1836
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001837 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01001838
1839 /* If data was modified, we need to touch to re-schedule sync */
1840 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001841 }
Willy Tarreau09448f72014-06-25 18:12:15 +02001842
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001843 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001844 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001845 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02001846 }
1847 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02001848 break;
1849
Joseph Herlant5ba80252018-11-15 09:25:36 -08001850 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02001851 default:
1852 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001853 }
1854 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01001855
Christopher Fauletea827bd2018-11-15 15:34:11 +01001856 end:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001857 if (early_hints) {
1858 http_send_early_hints(s, early_hints);
1859 free_trash_chunk(early_hints);
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001860 }
1861
Willy Tarreau96257ec2012-12-27 10:46:37 +01001862 /* we reached the end of the rules, nothing to report */
Christopher Fauletea827bd2018-11-15 15:34:11 +01001863 return rule_ret;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001864}
1865
Willy Tarreau71241ab2012-12-27 11:30:54 +01001866
Willy Tarreau51d861a2015-05-22 17:30:48 +02001867/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
1868 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
1869 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
1870 * is returned, the process can continue the evaluation of next rule list. If
1871 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
1872 * is returned, it means the operation could not be processed and a server error
1873 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
1874 * deny rule. If *YIELD is returned, the caller must call again the function
1875 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001876 */
Christopher Faulet10079f52018-10-03 15:17:28 +02001877enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001878http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001879{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001880 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001881 struct http_txn *txn = s->txn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001882 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001883 struct hdr_ctx ctx;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001884 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001885 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001886
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001887 /* If "the current_rule_list" match the executed rule list, we are in
1888 * resume condition. If a resume is needed it is always in the action
1889 * and never in the ACL or converters. In this case, we initialise the
1890 * current rule, and go to the action execution point.
1891 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001892 if (s->current_rule) {
1893 rule = s->current_rule;
1894 s->current_rule = NULL;
1895 if (s->current_rule_list == rules)
1896 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001897 }
1898 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001899
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001900 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001901
1902 /* check optional condition */
1903 if (rule->cond) {
1904 int ret;
1905
Willy Tarreau192252e2015-04-04 01:47:55 +02001906 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001907 ret = acl_pass(ret);
1908
1909 if (rule->cond->pol == ACL_COND_UNLESS)
1910 ret = !ret;
1911
1912 if (!ret) /* condition not matched */
1913 continue;
1914 }
1915
Willy Tarreauacc98002015-09-27 23:34:39 +02001916 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001917resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001918 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001919 case ACT_ACTION_ALLOW:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001920 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
1921 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001922
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001923 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001924 txn->flags |= TX_SVDENY;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001925 rule_ret = HTTP_RULE_RES_STOP;
1926 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001927
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001928 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001929 s->task->nice = rule->arg.nice;
1930 break;
1931
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001932 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001933 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001934 break;
1935
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001936 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001937 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Willy Tarreau51347ed2013-06-11 19:34:13 +02001938 break;
1939
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001940 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001941 s->logs.level = rule->arg.loglevel;
1942 break;
1943
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001944 case ACT_HTTP_REPLACE_HDR:
1945 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001946 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
1947 rule->arg.hdr_add.name_len,
1948 &rule->arg.hdr_add.fmt,
Dragan Dosen26743032019-04-30 15:54:36 +02001949 rule->arg.hdr_add.re, rule->action)) {
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001950 rule_ret = HTTP_RULE_RES_BADREQ;
1951 goto end;
1952 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001953 break;
1954
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001955 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001956 ctx.idx = 0;
1957 /* remove all occurrences of the header */
1958 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001959 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001960 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
1961 }
Willy Tarreau85603282015-01-21 20:39:27 +01001962 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001963
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001964 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001965 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02001966 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001967
1968 replace = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001969 if (!replace) {
1970 rule_ret = HTTP_RULE_RES_BADREQ;
1971 goto end;
1972 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001973
1974 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001975 memcpy(replace->area, rule->arg.hdr_add.name,
1976 rule->arg.hdr_add.name_len);
1977 replace->data = rule->arg.hdr_add.name_len;
1978 replace->area[replace->data++] = ':';
1979 replace->area[replace->data++] = ' ';
1980 replace->data += build_logline(s,
1981 replace->area + replace->data,
1982 replace->size - replace->data,
1983 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01001984
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001985 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001986 /* remove all occurrences of the header */
1987 ctx.idx = 0;
1988 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001989 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001990 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
1991 }
1992 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001993
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001994 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001995 static unsigned char rate_limit = 0;
1996
1997 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001998 replace->area[rule->arg.hdr_add.name_len] = 0;
1999 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,
2000 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002001 }
2002
Olivier Houcharda798bf52019-03-08 18:52:00 +01002003 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002004 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002005 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002006 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002007 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002008 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01002009 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002010 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002011
2012 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002013 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002014 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002015
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002016 case ACT_HTTP_DEL_ACL:
2017 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002018 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002019 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002020
2021 /* collect reference */
2022 ref = pat_ref_lookup(rule->arg.map.ref);
2023 if (!ref)
2024 continue;
2025
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002026 /* allocate key */
2027 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002028 if (!key) {
2029 rule_ret = HTTP_RULE_RES_BADREQ;
2030 goto end;
2031 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002032
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002033 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002034 key->data = build_logline(s, key->area, key->size,
2035 &rule->arg.map.key);
2036 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002037
2038 /* perform update */
2039 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002040 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002041 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002042 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002043
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002044 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002045 break;
2046 }
2047
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002048 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002049 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002050 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002051
2052 /* collect reference */
2053 ref = pat_ref_lookup(rule->arg.map.ref);
2054 if (!ref)
2055 continue;
2056
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002057 /* allocate key */
2058 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002059 if (!key) {
2060 rule_ret = HTTP_RULE_RES_BADREQ;
2061 goto end;
2062 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002063
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002064 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002065 key->data = build_logline(s, key->area, key->size,
2066 &rule->arg.map.key);
2067 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002068
2069 /* perform update */
2070 /* check if the entry already exists */
Christopher Faulete84289e2019-04-19 14:50:55 +02002071 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002072 if (pat_ref_find_elt(ref, key->area) == NULL)
2073 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02002074 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002075
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002076 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002077 break;
2078 }
2079
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002080 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002081 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002082 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002083
2084 /* collect reference */
2085 ref = pat_ref_lookup(rule->arg.map.ref);
2086 if (!ref)
2087 continue;
2088
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002089 /* allocate key */
2090 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002091 if (!key) {
2092 rule_ret = HTTP_RULE_RES_BADREQ;
2093 goto end;
2094 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002095
2096 /* allocate value */
2097 value = alloc_trash_chunk();
2098 if (!value) {
2099 free_trash_chunk(key);
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002100 rule_ret = HTTP_RULE_RES_BADREQ;
2101 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002102 }
2103
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002104 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002105 key->data = build_logline(s, key->area, key->size,
2106 &rule->arg.map.key);
2107 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002108
2109 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002110 value->data = build_logline(s, value->area,
2111 value->size,
2112 &rule->arg.map.value);
2113 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002114
2115 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002116 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002117 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002118 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002119 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002120 else
2121 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002122 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002123 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002124 free_trash_chunk(key);
2125 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002126 break;
2127 }
William Lallemand73025dd2014-04-24 14:38:37 +02002128
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002129 case ACT_HTTP_REDIR:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002130 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002131 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002132 rule_ret = HTTP_RULE_RES_BADREQ;
2133 goto end;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002134
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002135 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2136 /* Note: only the first valid tracking parameter of each
2137 * applies.
2138 */
2139
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002140 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002141 struct stktable *t;
2142 struct stksess *ts;
2143 struct stktable_key *key;
2144 void *ptr;
2145
2146 t = rule->arg.trk_ctr.table.t;
2147 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2148
2149 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002150 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002151
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002152 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002153
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002154 /* let's count a new HTTP request as it's the first time we do it */
2155 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2156 if (ptr)
2157 stktable_data_cast(ptr, http_req_cnt)++;
2158
2159 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2160 if (ptr)
2161 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2162 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2163
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002164 /* When the client triggers a 4xx from the server, it's most often due
2165 * to a missing object or permission. These events should be tracked
2166 * because if they happen often, it may indicate a brute force or a
2167 * vulnerability scan. Normally this is done when receiving the response
2168 * but here we're tracking after this ought to have been done so we have
2169 * to do it on purpose.
2170 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002171 if ((unsigned)(txn->status - 400) < 100) {
2172 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2173 if (ptr)
2174 stktable_data_cast(ptr, http_err_cnt)++;
2175
2176 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2177 if (ptr)
2178 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2179 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2180 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002181
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002182 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002183
Emeric Brun0fed0b02017-11-29 16:15:07 +01002184 /* If data was modified, we need to touch to re-schedule sync */
2185 stktable_touch_local(t, ts, 0);
2186
Emeric Brun819fc6f2017-06-13 19:37:32 +02002187 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2188 if (sess->fe != s->be)
2189 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2190
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002191 }
2192 }
2193 break;
2194
Thierry FOURNIER42148732015-09-02 17:17:33 +02002195 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002196 if ((s->req.flags & CF_READ_ERROR) ||
2197 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002198 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002199 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002200
Willy Tarreauacc98002015-09-27 23:34:39 +02002201 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002202 case ACT_RET_ERR:
2203 case ACT_RET_CONT:
2204 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002205 case ACT_RET_STOP:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002206 rule_ret = HTTP_RULE_RES_STOP;
2207 goto end;
Christopher Faulet4629d082019-07-04 11:27:15 +02002208 case ACT_RET_DONE:
2209 rule_ret = HTTP_RULE_RES_DONE;
2210 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002211 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002212 s->current_rule = rule;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002213 rule_ret = HTTP_RULE_RES_YIELD;
2214 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002215 }
William Lallemand73025dd2014-04-24 14:38:37 +02002216 break;
2217
Joseph Herlant5ba80252018-11-15 09:25:36 -08002218 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002219 default:
2220 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002221 }
2222 }
2223
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002224 end:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002225 /* we reached the end of the rules, nothing to report */
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002226 return rule_ret;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002227}
2228
2229
Willy Tarreau71241ab2012-12-27 11:30:54 +01002230/* Perform an HTTP redirect based on the information in <rule>. The function
2231 * returns non-zero on success, or zero in case of a, irrecoverable error such
2232 * as too large a request to build a valid response.
2233 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002234int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002235{
Willy Tarreaub329a312015-05-22 16:27:37 +02002236 struct http_msg *req = &txn->req;
2237 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002238 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002239 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002240 int ret = 0;
2241
Christopher Fauletf2824e62018-10-01 12:12:37 +02002242 if (IS_HTX_STRM(s))
2243 return htx_apply_redirect_rule(rule, s, txn);
2244
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002245 chunk = alloc_trash_chunk();
2246 if (!chunk)
2247 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002248
2249 /* build redirect message */
2250 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002251 case 308:
2252 msg_fmt = HTTP_308;
2253 break;
2254 case 307:
2255 msg_fmt = HTTP_307;
2256 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002257 case 303:
2258 msg_fmt = HTTP_303;
2259 break;
2260 case 301:
2261 msg_fmt = HTTP_301;
2262 break;
2263 case 302:
2264 default:
2265 msg_fmt = HTTP_302;
2266 break;
2267 }
2268
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002269 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2270 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002271
2272 switch(rule->type) {
2273 case REDIRECT_TYPE_SCHEME: {
2274 const char *path;
2275 const char *host;
2276 struct hdr_ctx ctx;
2277 int pathlen;
2278 int hostlen;
2279
2280 host = "";
2281 hostlen = 0;
2282 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002283 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002284 host = ctx.line + ctx.val;
2285 hostlen = ctx.vlen;
2286 }
2287
Willy Tarreau6b952c82018-09-10 17:45:34 +02002288 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002289 /* build message using path */
2290 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002291 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002292 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2293 int qs = 0;
2294 while (qs < pathlen) {
2295 if (path[qs] == '?') {
2296 pathlen = qs;
2297 break;
2298 }
2299 qs++;
2300 }
2301 }
2302 } else {
2303 path = "/";
2304 pathlen = 1;
2305 }
2306
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002307 if (rule->rdr_str) { /* this is an old "redirect" rule */
2308 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002309 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002310 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002311
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002312 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002313 memcpy(chunk->area + chunk->data, rule->rdr_str,
2314 rule->rdr_len);
2315 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002316 }
2317 else {
2318 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002319 chunk->data += build_logline(s,
2320 chunk->area + chunk->data,
2321 chunk->size - chunk->data,
2322 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002323
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002324 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002325 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002326 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002327 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002328 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002329 memcpy(chunk->area + chunk->data, "://", 3);
2330 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002331
2332 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002333 memcpy(chunk->area + chunk->data, host, hostlen);
2334 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002335
2336 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002337 memcpy(chunk->area + chunk->data, path, pathlen);
2338 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002339
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002340 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002341 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002342 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002343 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002344 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002345 chunk->area[chunk->data] = '/';
2346 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002347 }
2348
2349 break;
2350 }
2351 case REDIRECT_TYPE_PREFIX: {
2352 const char *path;
2353 int pathlen;
2354
Willy Tarreau6b952c82018-09-10 17:45:34 +02002355 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002356 /* build message using path */
2357 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002358 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002359 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2360 int qs = 0;
2361 while (qs < pathlen) {
2362 if (path[qs] == '?') {
2363 pathlen = qs;
2364 break;
2365 }
2366 qs++;
2367 }
2368 }
2369 } else {
2370 path = "/";
2371 pathlen = 1;
2372 }
2373
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002374 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002375 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002376 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002377
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002378 /* add prefix. Note that if prefix == "/", we don't want to
2379 * add anything, otherwise it makes it hard for the user to
2380 * configure a self-redirection.
2381 */
2382 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002383 memcpy(chunk->area + chunk->data,
2384 rule->rdr_str, rule->rdr_len);
2385 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002386 }
2387 }
2388 else {
2389 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002390 chunk->data += build_logline(s,
2391 chunk->area + chunk->data,
2392 chunk->size - chunk->data,
2393 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002394
2395 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002396 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002397 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002398 }
2399
2400 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002401 memcpy(chunk->area + chunk->data, path, pathlen);
2402 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002403
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002404 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002405 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002406 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002407 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002408 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002409 chunk->area[chunk->data] = '/';
2410 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002411 }
2412
2413 break;
2414 }
2415 case REDIRECT_TYPE_LOCATION:
2416 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002417 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002418 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002419 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002420
2421 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002422 memcpy(chunk->area + chunk->data, rule->rdr_str,
2423 rule->rdr_len);
2424 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002425 }
2426 else {
2427 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002428 chunk->data += build_logline(s,
2429 chunk->area + chunk->data,
2430 chunk->size - chunk->data,
2431 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002432
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002433 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002434 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002435 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002436 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002437 break;
2438 }
2439
2440 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002441 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2442 chunk->data += 14;
2443 memcpy(chunk->area + chunk->data, rule->cookie_str,
2444 rule->cookie_len);
2445 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002446 }
2447
Willy Tarreau19b14122017-02-28 09:48:11 +01002448 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002449 txn->status = rule->code;
2450 /* let's log the request time */
2451 s->logs.tv_request = now;
2452
Christopher Fauletbe821b92017-03-30 11:21:53 +02002453 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002454 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2455 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2456 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002457 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002458 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002459 memcpy(chunk->area + chunk->data,
2460 "\r\nProxy-Connection: keep-alive", 30);
2461 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002462 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002463 memcpy(chunk->area + chunk->data,
2464 "\r\nConnection: keep-alive", 24);
2465 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002466 }
2467 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002468 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2469 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002470 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002471 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002472 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002473 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002474 req->next -= req->sov;
2475 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002476 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002477 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002478 req->msg_state = HTTP_MSG_CLOSED;
2479 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002480 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002481 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002482 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002483 /* let the server side turn to SI_ST_CLO */
2484 channel_shutw_now(req->chn);
Willy Tarreau67bd3fc2019-10-22 18:13:44 +02002485 channel_dont_connect(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002486 } else {
2487 /* keep-alive not possible */
2488 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002489 memcpy(chunk->area + chunk->data,
2490 "\r\nProxy-Connection: close\r\n\r\n", 29);
2491 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002492 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002493 memcpy(chunk->area + chunk->data,
2494 "\r\nConnection: close\r\n\r\n", 23);
2495 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002496 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002497 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002498 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002499 }
2500
Willy Tarreaue7dff022015-04-03 01:14:29 +02002501 if (!(s->flags & SF_ERR_MASK))
2502 s->flags |= SF_ERR_LOCAL;
2503 if (!(s->flags & SF_FINST_MASK))
2504 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002505
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002506 ret = 1;
2507 leave:
2508 free_trash_chunk(chunk);
2509 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002510}
2511
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002512/* This stream analyser runs all HTTP request processing which is common to
2513 * frontends and backends, which means blocking ACLs, filters, connection-close,
2514 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002515 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002516 * either needs more data or wants to immediately abort the request (eg: deny,
2517 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002518 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002519int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002520{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002521 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002522 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002523 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002524 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002525 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002526 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002527 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002528 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002529
Christopher Faulete0768eb2018-10-03 16:38:02 +02002530 if (IS_HTX_STRM(s))
2531 return htx_process_req_common(s, req, an_bit, px);
2532
Willy Tarreau655dce92009-11-08 13:10:58 +01002533 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002534 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002535 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002536 }
2537
Christopher Faulet45073512018-07-20 10:16:29 +02002538 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 +02002539 now_ms, __FUNCTION__,
2540 s,
2541 req,
2542 req->rex, req->wex,
2543 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002544 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002545 req->analysers);
2546
Christopher Faulet1907ccc2019-04-29 13:12:02 +02002547 /* just in case we have some per-backend tracking. Only called the first
2548 * execution of the analyser. */
2549 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
2550 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002551
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002552 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002553 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002554 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002555
Willy Tarreau0b748332014-04-29 00:13:29 +02002556 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002557 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2558 goto return_prx_yield;
2559
Willy Tarreau0b748332014-04-29 00:13:29 +02002560 case HTTP_RULE_RES_CONT:
2561 case HTTP_RULE_RES_STOP: /* nothing to do */
2562 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002563
Willy Tarreau0b748332014-04-29 00:13:29 +02002564 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2565 if (txn->flags & TX_CLTARPIT)
2566 goto tarpit;
2567 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002568
Willy Tarreau0b748332014-04-29 00:13:29 +02002569 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2570 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002571
Willy Tarreau0b748332014-04-29 00:13:29 +02002572 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002573 goto done;
2574
Willy Tarreau0b748332014-04-29 00:13:29 +02002575 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2576 goto return_bad_req;
2577 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002578 }
2579
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002580 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2581 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002582 struct hdr_ctx ctx;
2583
2584 ctx.idx = 0;
2585 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002586 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002587 if (unlikely(http_header_add_tail2(&txn->req,
2588 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002589 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002590 goto return_bad_req;
2591 }
2592 }
2593
2594 }
2595
Willy Tarreau52542592014-04-28 18:33:26 +02002596 /* OK at this stage, we know that the request was accepted according to
2597 * the http-request rules, we can check for the stats. Note that the
2598 * URI is detected *before* the req* rules in order not to be affected
2599 * by a possible reqrep, while they are processed *after* so that a
2600 * reqdeny can still block them. This clearly needs to change in 1.6!
2601 */
Christopher Faulet4629d082019-07-04 11:27:15 +02002602 if (!s->target && stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002603 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01002604 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002605 txn->status = 500;
2606 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002607 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002608
Willy Tarreaue7dff022015-04-03 01:14:29 +02002609 if (!(s->flags & SF_ERR_MASK))
2610 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002611 goto return_prx_cond;
2612 }
2613
2614 /* parse the whole stats request and extract the relevant information */
2615 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002616 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002617 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002618
Willy Tarreau0b748332014-04-29 00:13:29 +02002619 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2620 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002621
Willy Tarreau0b748332014-04-29 00:13:29 +02002622 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2623 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002624 }
2625
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002626 /* evaluate the req* rules except reqadd */
2627 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002628 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002629 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002630
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002631 if (txn->flags & TX_CLDENY)
2632 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002633
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002634 if (txn->flags & TX_CLTARPIT) {
2635 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002636 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002637 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002638 }
Willy Tarreau06619262006-12-17 08:37:22 +01002639
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002640 /* add request headers from the rule sets in the same order */
2641 list_for_each_entry(wl, &px->req_add, list) {
2642 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002643 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002644 ret = acl_pass(ret);
2645 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2646 ret = !ret;
2647 if (!ret)
2648 continue;
2649 }
2650
Christopher Faulet10079f52018-10-03 15:17:28 +02002651 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002652 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002653 }
2654
Willy Tarreau52542592014-04-28 18:33:26 +02002655
2656 /* Proceed with the stats now. */
Christopher Faulet2571bc62019-03-01 11:44:26 +01002657 if (unlikely(objt_applet(s->target))) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002658 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002659 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +01002660 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002661
Christopher Fauletbcf242a2019-03-01 11:36:26 +01002662 http_handle_expect_hdr(s, req, msg);
2663
Willy Tarreaue7dff022015-04-03 01:14:29 +02002664 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2665 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2666 if (!(s->flags & SF_FINST_MASK))
2667 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002668
Willy Tarreau70730dd2014-04-24 18:06:27 +02002669 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002670 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2671 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002672 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +01002673
2674 req->flags |= CF_SEND_DONTWAIT;
2675 s->flags |= SF_ASSIGNED;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002676 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002677 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002678
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002679 /* check whether we have some ACLs set to redirect this request */
2680 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002681 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002682 int ret;
2683
Willy Tarreau192252e2015-04-04 01:47:55 +02002684 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002685 ret = acl_pass(ret);
2686 if (rule->cond->pol == ACL_COND_UNLESS)
2687 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002688 if (!ret)
2689 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01002690 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002691 if (!http_apply_redirect_rule(rule, s, txn))
2692 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002693 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002694 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002695
Willy Tarreau2be39392010-01-03 17:24:51 +01002696 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2697 * If this happens, then the data will not come immediately, so we must
2698 * send all what we have without waiting. Note that due to the small gain
2699 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002700 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01002701 * itself once used.
2702 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002703 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01002704
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002705 done: /* done with this analyser, continue with next ones that the calling
2706 * points will have set, if any.
2707 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002708 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002709 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
2710 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002711 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002712
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002713 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02002714 /* Allow cookie logging
2715 */
2716 if (s->be->cookie_name || sess->fe->capture_name)
2717 manage_client_side_cookies(s, req);
2718
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002719 /* When a connection is tarpitted, we use the tarpit timeout,
2720 * which may be the same as the connect timeout if unspecified.
2721 * If unset, then set it to zero because we really want it to
2722 * eventually expire. We build the tarpit as an analyser.
2723 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002724 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002725
2726 /* wipe the request out so that we can drop the connection early
2727 * if the client closes first.
2728 */
2729 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002730
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002731 txn->status = http_err_codes[deny_status];
2732
Christopher Faulet0184ea72017-01-05 14:06:34 +01002733 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002734 req->analysers |= AN_REQ_HTTP_TARPIT;
2735 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2736 if (!req->analyse_exp)
2737 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02002738 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +01002739 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002740 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002741 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002742 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002743 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002744 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002745
2746 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002747
2748 /* Allow cookie logging
2749 */
2750 if (s->be->cookie_name || sess->fe->capture_name)
2751 manage_client_side_cookies(s, req);
2752
Willy Tarreau0b748332014-04-29 00:13:29 +02002753 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002754 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002755 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002756 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02002757 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +01002758 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002759 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002760 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002761 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002762 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002763 goto return_prx_cond;
2764
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002765 return_bad_req:
2766 /* We centralize bad requests processing here */
2767 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2768 /* we detected a parsing error. We want to archive this request
2769 * in the dedicated proxy area for later troubleshooting.
2770 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02002771 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002772 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002773
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002774 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002775 txn->req.msg_state = HTTP_MSG_ERROR;
2776 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002777 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002778
Olivier Houcharda798bf52019-03-08 18:52:00 +01002779 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002780 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002781 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002782
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002783 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002784 if (!(s->flags & SF_ERR_MASK))
2785 s->flags |= SF_ERR_PRXCOND;
2786 if (!(s->flags & SF_FINST_MASK))
2787 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002788
Christopher Faulet0184ea72017-01-05 14:06:34 +01002789 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002790 req->analyse_exp = TICK_ETERNITY;
2791 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002792
2793 return_prx_yield:
2794 channel_dont_connect(req);
2795 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002796}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002797
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002798/* This function performs all the processing enabled for the current request.
2799 * It returns 1 if the processing can continue on next analysers, or zero if it
2800 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002801 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002802 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002803int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002804{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002805 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002806 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002807 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02002808 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002809
Christopher Faulete0768eb2018-10-03 16:38:02 +02002810 if (IS_HTX_STRM(s))
2811 return htx_process_request(s, req, an_bit);
2812
Willy Tarreau655dce92009-11-08 13:10:58 +01002813 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002814 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002815 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002816 return 0;
2817 }
2818
Christopher Faulet45073512018-07-20 10:16:29 +02002819 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 +02002820 now_ms, __FUNCTION__,
2821 s,
2822 req,
2823 req->rex, req->wex,
2824 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002825 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002826 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002827
Willy Tarreau59234e92008-11-30 23:51:27 +01002828 /*
2829 * Right now, we know that we have processed the entire headers
2830 * and that unwanted requests have been filtered out. We can do
2831 * whatever we want with the remaining request. Also, now we
2832 * may have separate values for ->fe, ->be.
2833 */
Willy Tarreau06619262006-12-17 08:37:22 +01002834
Willy Tarreau59234e92008-11-30 23:51:27 +01002835 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002836 * If HTTP PROXY is set we simply get remote server address parsing
2837 * incoming request. Note that this requires that a connection is
2838 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01002839 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002840 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002841 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002842 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002843
Willy Tarreau9471b8c2013-12-15 13:31:35 +01002844 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002845 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002846 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002847 txn->req.msg_state = HTTP_MSG_ERROR;
2848 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002849 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002850 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002851
Willy Tarreaue7dff022015-04-03 01:14:29 +02002852 if (!(s->flags & SF_ERR_MASK))
2853 s->flags |= SF_ERR_RESOURCE;
2854 if (!(s->flags & SF_FINST_MASK))
2855 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002856
2857 return 0;
2858 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002859
Willy Tarreau6b952c82018-09-10 17:45:34 +02002860 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02002861 if (url2sa(ci_head(req) + msg->sl.rq.u,
2862 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02002863 &conn->addr.to, NULL) == -1)
2864 goto return_bad_req;
2865
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002866 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02002867 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
2868 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002869 * u_l characters by a single "/".
2870 */
2871 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002872 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002873 char *cur_end = cur_ptr + txn->req.sl.rq.l;
2874 int delta;
2875
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002876 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002877 http_msg_move_end(&txn->req, delta);
2878 cur_end += delta;
2879 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
2880 goto return_bad_req;
2881 }
2882 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002883 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002884 char *cur_end = cur_ptr + txn->req.sl.rq.l;
2885 int delta;
2886
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002887 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002888 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002889 http_msg_move_end(&txn->req, delta);
2890 cur_end += delta;
2891 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
2892 goto return_bad_req;
2893 }
Willy Tarreau3284dd22019-07-18 16:17:15 +02002894 conn->target = &s->be->obj_type;
Willy Tarreau59234e92008-11-30 23:51:27 +01002895 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002896
Willy Tarreau59234e92008-11-30 23:51:27 +01002897 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002898 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002899 * Note that doing so might move headers in the request, but
2900 * the fields will stay coherent and the URI will not move.
2901 * This should only be performed in the backend.
2902 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002903 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002904 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002905
William Lallemanda73203e2012-03-12 12:48:57 +01002906 /* add unique-id if "header-unique-id" is specified */
2907
Thierry Fournierf4011dd2016-03-29 17:23:51 +02002908 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01002909 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002910 goto return_bad_req;
2911 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002912 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002913 }
William Lallemanda73203e2012-03-12 12:48:57 +01002914
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002915 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02002916 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01002917 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002918 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01002919 goto return_bad_req;
2920 }
2921
Cyril Bontéb21570a2009-11-29 20:04:48 +01002922 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002923 * 9: add X-Forwarded-For if either the frontend or the backend
2924 * asks for it.
2925 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002926 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02002927 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002928 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
2929 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
2930 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002931 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02002932 /* The header is set to be added only if none is present
2933 * and we found it, so don't do anything.
2934 */
2935 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002936 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002937 /* Add an X-Forwarded-For header unless the source IP is
2938 * in the 'except' network range.
2939 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002940 if ((!sess->fe->except_mask.s_addr ||
2941 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
2942 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01002943 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002944 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01002945 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002946 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002947 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002948 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002949
2950 /* Note: we rely on the backend to get the header name to be used for
2951 * x-forwarded-for, because the header is really meant for the backends.
2952 * However, if the backend did not specify any option, we have to rely
2953 * on the frontend's header name.
2954 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002955 if (s->be->fwdfor_hdr_len) {
2956 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002957 memcpy(trash.area,
2958 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002959 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002960 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002961 memcpy(trash.area,
2962 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002963 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002964 len += snprintf(trash.area + len,
2965 trash.size - len,
2966 ": %d.%d.%d.%d", pn[0], pn[1],
2967 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002968
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002969 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01002970 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002971 }
2972 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002973 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002974 /* FIXME: for the sake of completeness, we should also support
2975 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002976 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002977 int len;
2978 char pn[INET6_ADDRSTRLEN];
2979 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002980 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01002981 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002982
Willy Tarreau59234e92008-11-30 23:51:27 +01002983 /* Note: we rely on the backend to get the header name to be used for
2984 * x-forwarded-for, because the header is really meant for the backends.
2985 * However, if the backend did not specify any option, we have to rely
2986 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002987 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002988 if (s->be->fwdfor_hdr_len) {
2989 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002990 memcpy(trash.area, s->be->fwdfor_hdr_name,
2991 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01002992 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002993 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002994 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
2995 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002996 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002997 len += snprintf(trash.area + len, trash.size - len,
2998 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002999
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003000 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003001 goto return_bad_req;
3002 }
3003 }
3004
3005 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003006 * 10: add X-Original-To if either the frontend or the backend
3007 * asks for it.
3008 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003009 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003010
3011 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003012 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003013 /* Add an X-Original-To header unless the destination IP is
3014 * in the 'except' network range.
3015 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003016 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003017
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003018 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003019 ((!sess->fe->except_mask_to.s_addr ||
3020 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3021 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003022 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003023 (((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 +02003024 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003025 int len;
3026 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003027 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003028
3029 /* Note: we rely on the backend to get the header name to be used for
3030 * x-original-to, because the header is really meant for the backends.
3031 * However, if the backend did not specify any option, we have to rely
3032 * on the frontend's header name.
3033 */
3034 if (s->be->orgto_hdr_len) {
3035 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003036 memcpy(trash.area,
3037 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003038 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003039 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003040 memcpy(trash.area,
3041 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003042 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003043 len += snprintf(trash.area + len,
3044 trash.size - len,
3045 ": %d.%d.%d.%d", pn[0], pn[1],
3046 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003047
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003048 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003049 goto return_bad_req;
3050 }
3051 }
3052 }
3053
Willy Tarreau50fc7772012-11-11 22:19:57 +01003054 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3055 * If an "Upgrade" token is found, the header is left untouched in order not to have
3056 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3057 * "Upgrade" is present in the Connection header.
3058 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003059 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003060 unsigned int want_flags = 0;
3061
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003062 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003063 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003064 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003065 want_flags |= TX_CON_CLO_SET;
3066 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003067 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003068 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003069 want_flags |= TX_CON_KAL_SET;
3070 }
3071
3072 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003073 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003074 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003075
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003076
Willy Tarreau522d6c02009-12-06 18:49:18 +01003077 /* If we have no server assigned yet and we're balancing on url_param
3078 * with a POST request, we may be interested in checking the body for
3079 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003080 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003081 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +01003082 s->txn->meth == HTTP_METH_POST &&
3083 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003084 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003085 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003086 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003087 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003088
Christopher Fauletbe821b92017-03-30 11:21:53 +02003089 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3090 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +01003091
Christopher Fauletbe821b92017-03-30 11:21:53 +02003092 /* We expect some data from the client. Unless we know for sure
3093 * we already have a full request, we have to re-enable quick-ack
3094 * in case we previously disabled it, otherwise we might cause
3095 * the client to delay further data.
3096 */
3097 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletbe821b92017-03-30 11:21:53 +02003098 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003099 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau1a18b542018-12-11 16:37:42 +01003100 conn_set_quickack(cli_conn, 1);
Willy Tarreau03945942009-12-22 16:50:27 +01003101
Willy Tarreau59234e92008-11-30 23:51:27 +01003102 /*************************************************************
3103 * OK, that's finished for the headers. We have done what we *
3104 * could. Let's switch to the DATA state. *
3105 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003106 req->analyse_exp = TICK_ETERNITY;
3107 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003108
Willy Tarreau59234e92008-11-30 23:51:27 +01003109 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003110 /* OK let's go on with the BODY now */
3111 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003112
Willy Tarreau59234e92008-11-30 23:51:27 +01003113 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003114 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003115 /* we detected a parsing error. We want to archive this request
3116 * in the dedicated proxy area for later troubleshooting.
3117 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003118 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003119 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003120
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003121 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003122 txn->req.msg_state = HTTP_MSG_ERROR;
3123 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003124 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003125 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003126
Olivier Houcharda798bf52019-03-08 18:52:00 +01003127 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003128 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003129 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003130
Willy Tarreaue7dff022015-04-03 01:14:29 +02003131 if (!(s->flags & SF_ERR_MASK))
3132 s->flags |= SF_ERR_PRXCOND;
3133 if (!(s->flags & SF_FINST_MASK))
3134 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003135 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003136}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003137
Willy Tarreau60b85b02008-11-30 23:28:40 +01003138/* This function is an analyser which processes the HTTP tarpit. It always
3139 * returns zero, at the beginning because it prevents any other processing
3140 * from occurring, and at the end because it terminates the request.
3141 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003142int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003143{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003144 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003145
Christopher Faulete0768eb2018-10-03 16:38:02 +02003146 if (IS_HTX_STRM(s))
3147 return htx_process_tarpit(s, req, an_bit);
3148
Willy Tarreau60b85b02008-11-30 23:28:40 +01003149 /* This connection is being tarpitted. The CLIENT side has
3150 * already set the connect expiration date to the right
3151 * timeout. We just have to check that the client is still
3152 * there and that the timeout has not expired.
3153 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003154 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003155 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003156 !tick_is_expired(req->analyse_exp, now_ms))
3157 return 0;
3158
3159 /* We will set the queue timer to the time spent, just for
3160 * logging purposes. We fake a 500 server error, so that the
3161 * attacker will not suspect his connection has been tarpitted.
3162 * It will not cause trouble to the logs because we can exclude
3163 * the tarpitted connections by filtering on the 'PT' status flags.
3164 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003165 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3166
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003167 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003168 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003169
Christopher Faulet0184ea72017-01-05 14:06:34 +01003170 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003171 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003172
Willy Tarreaue7dff022015-04-03 01:14:29 +02003173 if (!(s->flags & SF_ERR_MASK))
3174 s->flags |= SF_ERR_PRXCOND;
3175 if (!(s->flags & SF_FINST_MASK))
3176 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003177 return 0;
3178}
3179
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003180/* This function is an analyser which waits for the HTTP request body. It waits
3181 * for either the buffer to be full, or the full advertised contents to have
3182 * reached the buffer. It must only be called after the standard HTTP request
3183 * processing has occurred, because it expects the request to be parsed and will
3184 * look for the Expect header. It may send a 100-Continue interim response. It
3185 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3186 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3187 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003188 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003189int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003190{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003191 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003192 struct http_txn *txn = s->txn;
3193 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003194
Christopher Faulete0768eb2018-10-03 16:38:02 +02003195 if (IS_HTX_STRM(s))
3196 return htx_wait_for_request_body(s, req, an_bit);
3197
Christopher Faulet87451fd2019-03-01 11:16:34 +01003198 if (msg->msg_state < HTTP_MSG_BODY)
3199 goto missing_data;
3200
Willy Tarreaud34af782008-11-30 23:36:37 +01003201 /* We have to parse the HTTP request body to find any required data.
3202 * "balance url_param check_post" should have been the only way to get
3203 * into this. We were brought here after HTTP header analysis, so all
3204 * related structures are ready.
3205 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003206 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Christopher Faulet87451fd2019-03-01 11:16:34 +01003207 http_handle_expect_hdr(s, req, msg);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003208
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003209 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003210 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003211 * must save the body in msg->next because it survives buffer
3212 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003213 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003214 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003215
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003216 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003217 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3218 else
3219 msg->msg_state = HTTP_MSG_DATA;
3220 }
3221
Willy Tarreau890988f2014-04-10 11:59:33 +02003222 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3223 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003224 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003225 goto missing_data;
3226
3227 /* OK we have everything we need now */
3228 goto http_end;
3229 }
3230
3231 /* OK here we're parsing a chunked-encoded message */
3232
Willy Tarreau522d6c02009-12-06 18:49:18 +01003233 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003234 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003235 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003236 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003237 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003238 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003239 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003240
Willy Tarreau115acb92009-12-26 13:56:06 +01003241 if (!ret)
3242 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003243 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003244 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003245 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003246 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003247 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003248 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003249 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003250
3251 msg->chunk_len = chunk;
3252 msg->body_len += chunk;
3253
3254 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003255 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003256 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003257 }
3258
Willy Tarreaud98cf932009-12-27 22:54:55 +01003259 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003260 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3261 * for at least a whole chunk or the whole content length bytes after
3262 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003263 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003264 if (msg->msg_state == HTTP_MSG_TRAILERS)
3265 goto http_end;
3266
Willy Tarreaue115b492015-05-01 23:05:14 +02003267 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003268 goto http_end;
3269
3270 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003271 /* we get here if we need to wait for more data. If the buffer is full,
3272 * we have the maximum we can expect.
3273 */
Willy Tarreau23752332018-06-15 14:54:53 +02003274 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003275 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003276
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003277 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003278 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003279 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003280
Willy Tarreaue7dff022015-04-03 01:14:29 +02003281 if (!(s->flags & SF_ERR_MASK))
3282 s->flags |= SF_ERR_CLITO;
3283 if (!(s->flags & SF_FINST_MASK))
3284 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003285 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003286 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003287
3288 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003289 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003290 /* Not enough data. We'll re-use the http-request
3291 * timeout here. Ideally, we should set the timeout
3292 * relative to the accept() date. We just set the
3293 * request timeout once at the beginning of the
3294 * request.
3295 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003296 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003297 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003298 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003299 return 0;
3300 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003301
3302 http_end:
3303 /* The situation will not evolve, so let's give up on the analysis. */
3304 s->logs.tv_request = now; /* update the request timer to reflect full request */
3305 req->analysers &= ~an_bit;
3306 req->analyse_exp = TICK_ETERNITY;
3307 return 1;
3308
3309 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003310 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003311 txn->req.msg_state = HTTP_MSG_ERROR;
3312 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003313 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003314
Willy Tarreaue7dff022015-04-03 01:14:29 +02003315 if (!(s->flags & SF_ERR_MASK))
3316 s->flags |= SF_ERR_PRXCOND;
3317 if (!(s->flags & SF_FINST_MASK))
3318 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003319
Willy Tarreau522d6c02009-12-06 18:49:18 +01003320 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003321 req->analysers &= AN_REQ_FLT_END;
Olivier Houcharda798bf52019-03-08 18:52:00 +01003322 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003323 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003324 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003325 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003326}
3327
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003328/* send a server's name with an outgoing request over an established connection.
3329 * Note: this function is designed to be called once the request has been scheduled
3330 * for being forwarded. This is the reason why it rewinds the buffer before
3331 * proceeding.
3332 */
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003333int http_send_name_header(struct stream *s, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003334
3335 struct hdr_ctx ctx;
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003336 struct http_txn *txn = s->txn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003337 char *hdr_name = be->server_id_hdr_name;
3338 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003339 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003340 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003341 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003342
Christopher Faulet64159df2018-10-24 21:15:35 +02003343 if (IS_HTX_STRM(s))
3344 return htx_send_name_header(s, be, srv_name);
William Lallemandd9e90662012-01-30 17:27:17 +01003345 ctx.idx = 0;
3346
Willy Tarreau211cdec2014-04-17 20:18:08 +02003347 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003348 if (old_o) {
3349 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003350 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003351 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003352 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003353 }
3354
Willy Tarreauf37954d2018-06-15 18:31:02 +02003355 old_i = ci_data(chn);
3356 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 -05003357 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003358 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003359 }
3360
3361 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003362 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003363 memcpy(hdr_val, hdr_name, hdr_name_len);
3364 hdr_val += hdr_name_len;
3365 *hdr_val++ = ':';
3366 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003367 hdr_val += strlcpy2(hdr_val, srv_name,
3368 trash.area + trash.size - hdr_val);
3369 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3370 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003371
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003372 if (old_o) {
3373 /* If this was a forwarded request, we must readjust the amount of
3374 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003375 * variations. Note that the current state is >= HTTP_MSG_BODY,
3376 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003377 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003378 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003379 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003380 txn->req.next -= old_o;
3381 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003382 }
3383
Mark Lamourinec2247f02012-01-04 13:02:01 -05003384 return 0;
3385}
3386
Willy Tarreau610ecce2010-01-04 21:15:02 +01003387/* Terminate current transaction and prepare a new one. This is very tricky
3388 * right now but it works.
3389 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003390void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003391{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003392 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003393 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003394 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003395 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003396 struct connection *srv_conn;
3397 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003398 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003399
Willy Tarreau610ecce2010-01-04 21:15:02 +01003400 /* FIXME: We need a more portable way of releasing a backend's and a
3401 * server's connections. We need a safer way to reinitialize buffer
3402 * flags. We also need a more accurate method for computing per-request
3403 * data.
3404 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003405 cs = objt_cs(s->si[1].end);
3406 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003407
Willy Tarreau4213a112013-12-15 10:25:42 +01003408 /* unless we're doing keep-alive, we want to quickly close the connection
3409 * to the server.
3410 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003411 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003412 !si_conn_ready(&s->si[1]) || !srv_conn->owner) {
Willy Tarreau350f4872014-11-28 14:42:25 +01003413 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3414 si_shutr(&s->si[1]);
3415 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003416 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003417
Willy Tarreaue7dff022015-04-03 01:14:29 +02003418 if (s->flags & SF_BE_ASSIGNED) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003419 _HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003420 if (unlikely(s->srv_conn))
3421 sess_change_server(s, NULL);
3422 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003423
3424 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003425 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003426
Willy Tarreaueee5b512015-04-03 23:46:31 +02003427 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003428 int n;
3429
Willy Tarreaueee5b512015-04-03 23:46:31 +02003430 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003431 if (n < 1 || n > 5)
3432 n = 0;
3433
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003434 if (fe->mode == PR_MODE_HTTP) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003435 _HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003436 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003437 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003438 (be->mode == PR_MODE_HTTP)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003439 _HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3440 _HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003441 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003442 }
3443
3444 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003445 s->logs.bytes_in -= ci_data(&s->req);
3446 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003447
Willy Tarreau66425e32018-07-25 06:55:12 +02003448 /* we may need to know the position in the queue */
3449 pendconn_free(s);
3450
Willy Tarreau610ecce2010-01-04 21:15:02 +01003451 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003452 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003453 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003454 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003455 s->do_log(s);
3456 }
3457
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003458 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003459 stream_stop_content_counters(s);
3460 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003461
Willy Tarreau9efd7452018-05-31 14:48:54 +02003462 /* reset the profiling counter */
3463 s->task->calls = 0;
3464 s->task->cpu_time = 0;
3465 s->task->lat_time = 0;
Willy Tarreaud2d33482019-04-25 17:09:07 +02003466 s->task->call_date = ((profiling & HA_PROF_TASKS_MASK) >= HA_PROF_TASKS_AUTO) ? now_mono_time() : 0;
Willy Tarreau5e370da2019-04-25 18:33:37 +02003467 s->call_rate.curr_sec = s->call_rate.curr_ctr = s->call_rate.prev_ctr = 0;
Willy Tarreau9efd7452018-05-31 14:48:54 +02003468
Willy Tarreau610ecce2010-01-04 21:15:02 +01003469 s->logs.accept_date = date; /* user-visible date for logging */
3470 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003471 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3472 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003473 tv_zero(&s->logs.tv_request);
3474 s->logs.t_queue = -1;
3475 s->logs.t_connect = -1;
3476 s->logs.t_data = -1;
3477 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003478 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3479 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003480
Willy Tarreauf37954d2018-06-15 18:31:02 +02003481 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3482 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003483
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003484 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003485 if (s->flags & SF_CURR_SESS) {
3486 s->flags &= ~SF_CURR_SESS;
Olivier Houcharda798bf52019-03-08 18:52:00 +01003487 _HA_ATOMIC_SUB(&__objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003488 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003489 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003490 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003491 }
3492
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003493 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003494
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003495
3496 /* If we're doing keepalive, first call the mux detach() method
3497 * to let it know we want to detach without freing the connection.
3498 * We then can call si_release_endpoint() to destroy the conn_stream
Willy Tarreau4213a112013-12-15 10:25:42 +01003499 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003500 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houcharda70e1762018-12-13 18:01:00 +01003501 !si_conn_ready(&s->si[1]) ||
3502 (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 +02003503 srv_conn = NULL;
Olivier Houchard985f1392018-11-30 17:31:52 +01003504 else if (!srv_conn->owner) {
3505 srv_conn->owner = s->sess;
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003506 /* Add it unconditionally to the session list, it'll be removed
3507 * later if needed by session_check_idle_conn(), once we'll
3508 * have released the endpoint and know if it no longer has
3509 * attached streams, and so an idling connection
3510 */
Olivier Houchard351411f2018-12-27 17:20:54 +01003511 if (!session_add_conn(s->sess, srv_conn, s->target)) {
3512 srv_conn->owner = NULL;
3513 /* Try to add the connection to the server idle list.
3514 * If it fails, as the connection no longer has an
3515 * owner, it will be destroy later by
3516 * si_release_endpoint(), anyway
3517 */
3518 srv_add_to_idle_list(objt_server(srv_conn->target), srv_conn);
3519 srv_conn = NULL;
3520
3521 }
Olivier Houchard985f1392018-11-30 17:31:52 +01003522 }
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003523 si_release_endpoint(&s->si[1]);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003524 if (srv_conn && srv_conn->owner == s->sess) {
3525 if (session_check_idle_conn(s->sess, srv_conn) != 0)
3526 srv_conn = NULL;
3527 }
3528
Willy Tarreau4213a112013-12-15 10:25:42 +01003529
Willy Tarreau350f4872014-11-28 14:42:25 +01003530 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3531 s->si[1].err_type = SI_ET_NONE;
3532 s->si[1].conn_retries = 0; /* used for logging too */
3533 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003534 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 +01003535 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 +02003536 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 +02003537 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3538 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3539 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003540
Patrick Hemmere3faf022018-08-22 10:02:00 -04003541 hlua_ctx_destroy(s->hlua);
3542 s->hlua = NULL;
3543
Willy Tarreaueee5b512015-04-03 23:46:31 +02003544 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003545 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003546 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003547
3548 if (prev_status == 401 || prev_status == 407) {
3549 /* In HTTP keep-alive mode, if we receive a 401, we still have
3550 * a chance of being able to send the visitor again to the same
3551 * server over the same connection. This is required by some
3552 * broken protocols such as NTLM, and anyway whenever there is
3553 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003554 * it's better to do it (at least it helps with debugging), at
3555 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003556 */
Olivier Houchard250031e2019-05-29 15:01:50 +02003557 s->sess->flags |= SESS_FL_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003558 }
3559
Willy Tarreau53f96852016-02-02 18:50:47 +01003560 /* Never ever allow to reuse a connection from a non-reuse backend */
3561 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3562 srv_conn->flags |= CO_FL_PRIVATE;
3563
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003564 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003565 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003566
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003567 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003568 s->req.flags |= CF_NEVER_WAIT;
3569 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003570 }
3571
Willy Tarreau714ea782015-11-25 20:11:11 +01003572 /* we're removing the analysers, we MUST re-enable events detection.
3573 * We don't enable close on the response channel since it's either
3574 * already closed, or in keep-alive with an idle connection handler.
3575 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003576 channel_auto_read(&s->req);
3577 channel_auto_close(&s->req);
3578 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003579
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003580 /* we're in keep-alive with an idle connection, monitor it if not already done */
3581 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003582 srv = objt_server(srv_conn->target);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003583 if (srv) {
3584 if (srv_conn->flags & CO_FL_PRIVATE)
3585 LIST_ADD(&srv->priv_conns[tid], &srv_conn->list);
3586 else if (prev_flags & TX_NOT_FIRST)
3587 /* note: we check the request, not the connection, but
3588 * this is valid for strategies SAFE and AGGR, and in
3589 * case of ALWS, we don't care anyway.
3590 */
3591 LIST_ADD(&srv->safe_conns[tid], &srv_conn->list);
3592 else
3593 LIST_ADD(&srv->idle_conns[tid], &srv_conn->list);
3594 }
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003595 }
Christopher Faulete6006242017-03-10 11:52:44 +01003596 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3597 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003598}
3599
3600
3601/* This function updates the request state machine according to the response
3602 * state machine and buffer flags. It returns 1 if it changes anything (flag
3603 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3604 * it is only used to find when a request/response couple is complete. Both
3605 * this function and its equivalent should loop until both return zero. It
3606 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3607 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003608int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003609{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003610 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003611 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003612 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003613 unsigned int old_state = txn->req.msg_state;
3614
Christopher Faulet4be98032017-07-18 10:48:24 +02003615 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003616 return 0;
3617
3618 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003619 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003620 * We can shut the read side unless we want to abort_on_close,
3621 * or we have a POST request. The issue with POST requests is
3622 * that some browsers still send a CRLF after the request, and
3623 * this CRLF must be read so that it does not remain in the kernel
3624 * buffers, otherwise a close could cause an RST on some systems
3625 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003626 * Note that if we're using keep-alive on the client side, we'd
3627 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003628 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003629 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003630 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003631 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3632 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Christopher Faulet769d0e92019-03-22 14:23:18 +01003633 !(s->be->options & PR_O_ABRT_CLOSE) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003634 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003635 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003636
Willy Tarreau40f151a2012-12-20 12:10:09 +01003637 /* if the server closes the connection, we want to immediately react
3638 * and close the socket to save packets and syscalls.
3639 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003640 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003641
Willy Tarreau7f876a12015-11-18 11:59:55 +01003642 /* In any case we've finished parsing the request so we must
3643 * disable Nagle when sending data because 1) we're not going
3644 * to shut this side, and 2) the server is waiting for us to
3645 * send pending data.
3646 */
3647 chn->flags |= CF_NEVER_WAIT;
3648
Willy Tarreau610ecce2010-01-04 21:15:02 +01003649 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3650 goto wait_other_side;
3651
3652 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3653 /* The server has not finished to respond, so we
3654 * don't want to move in order not to upset it.
3655 */
3656 goto wait_other_side;
3657 }
3658
Willy Tarreau610ecce2010-01-04 21:15:02 +01003659 /* When we get here, it means that both the request and the
3660 * response have finished receiving. Depending on the connection
3661 * mode, we'll have to wait for the last bytes to leave in either
3662 * direction, and sometimes for a close to be effective.
3663 */
3664
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003665 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3666 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003667 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3668 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003669 }
3670 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3671 /* Option forceclose is set, or either side wants to close,
3672 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003673 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003674 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003675 *
3676 * However, there is an exception if the response
3677 * length is undefined. In this case, we need to wait
3678 * the close from the server. The response will be
3679 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003680 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003681 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3682 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3683 goto check_channel_flags;
3684
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003685 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3686 channel_shutr_now(chn);
3687 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003688 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003689 }
3690 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003691 /* The last possible modes are keep-alive and tunnel. Tunnel mode
3692 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003693 */
Willy Tarreau4213a112013-12-15 10:25:42 +01003694 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
3695 channel_auto_read(chn);
3696 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003697 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003698 }
3699
Christopher Faulet4be98032017-07-18 10:48:24 +02003700 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003701 }
3702
3703 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3704 http_msg_closing:
3705 /* nothing else to forward, just waiting for the output buffer
3706 * to be empty and for the shutw_now to take effect.
3707 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003708 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003709 txn->req.msg_state = HTTP_MSG_CLOSED;
3710 goto http_msg_closed;
3711 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003712 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003713 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003714 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003715 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003716 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003717 }
3718
3719 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3720 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01003721 /* if we don't know whether the server will close, we need to hard close */
3722 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
3723 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3724
Willy Tarreau3988d932013-12-27 23:03:08 +01003725 /* see above in MSG_DONE why we only do this in these states */
3726 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3727 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Christopher Faulet769d0e92019-03-22 14:23:18 +01003728 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01003729 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003730 goto wait_other_side;
3731 }
3732
Christopher Faulet4be98032017-07-18 10:48:24 +02003733 check_channel_flags:
3734 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
3735 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
3736 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02003737 txn->req.msg_state = HTTP_MSG_CLOSING;
3738 goto http_msg_closing;
3739 }
3740
3741
Willy Tarreau610ecce2010-01-04 21:15:02 +01003742 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003743 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003744}
3745
3746
3747/* This function updates the response state machine according to the request
3748 * state machine and buffer flags. It returns 1 if it changes anything (flag
3749 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3750 * it is only used to find when a request/response couple is complete. Both
3751 * this function and its equivalent should loop until both return zero. It
3752 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3753 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003754int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003755{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003756 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003757 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003758 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003759 unsigned int old_state = txn->rsp.msg_state;
3760
Christopher Faulet4be98032017-07-18 10:48:24 +02003761 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003762 return 0;
3763
3764 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3765 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003766 * still monitor the server connection for a possible close
3767 * while the request is being uploaded, so we don't disable
3768 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003769 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003770 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003771
3772 if (txn->req.msg_state == HTTP_MSG_ERROR)
3773 goto wait_other_side;
3774
3775 if (txn->req.msg_state < HTTP_MSG_DONE) {
3776 /* The client seems to still be sending data, probably
3777 * because we got an error response during an upload.
3778 * We have the choice of either breaking the connection
3779 * or letting it pass through. Let's do the later.
3780 */
3781 goto wait_other_side;
3782 }
3783
Willy Tarreau610ecce2010-01-04 21:15:02 +01003784 /* When we get here, it means that both the request and the
3785 * response have finished receiving. Depending on the connection
3786 * mode, we'll have to wait for the last bytes to leave in either
3787 * direction, and sometimes for a close to be effective.
3788 */
3789
3790 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3791 /* Server-close mode : shut read and wait for the request
3792 * side to close its output buffer. The caller will detect
3793 * when we're in DONE and the other is in CLOSED and will
3794 * catch that for the final cleanup.
3795 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003796 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
3797 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003798 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003799 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3800 /* Option forceclose is set, or either side wants to close,
3801 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003802 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003803 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003804 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01003805 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003806 channel_shutr_now(chn);
3807 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003808 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003809 }
3810 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003811 /* The last possible modes are keep-alive and tunnel. Tunnel will
3812 * need to forward remaining data. Keep-alive will need to monitor
3813 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003814 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003815 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02003816 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01003817 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
3818 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003819 }
3820
Christopher Faulet4be98032017-07-18 10:48:24 +02003821 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003822 }
3823
3824 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3825 http_msg_closing:
3826 /* nothing else to forward, just waiting for the output buffer
3827 * to be empty and for the shutw_now to take effect.
3828 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003829 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003830 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3831 goto http_msg_closed;
3832 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003833 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02003834 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003835 txn->rsp.msg_state = HTTP_MSG_ERROR;
Olivier Houcharda798bf52019-03-08 18:52:00 +01003836 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003837 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01003838 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003839 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003840 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003841 }
3842
3843 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3844 http_msg_closed:
3845 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01003846 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003847 channel_auto_close(chn);
3848 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003849 goto wait_other_side;
3850 }
3851
Christopher Faulet4be98032017-07-18 10:48:24 +02003852 check_channel_flags:
3853 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
3854 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
3855 /* if we've just closed an output, let's switch */
3856 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3857 goto http_msg_closing;
3858 }
3859
Willy Tarreau610ecce2010-01-04 21:15:02 +01003860 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02003861 /* We force the response to leave immediately if we're waiting for the
3862 * other side, since there is no pending shutdown to push it out.
3863 */
3864 if (!channel_is_empty(chn))
3865 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003866 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003867}
3868
3869
Christopher Faulet894da4c2017-07-18 11:29:07 +02003870/* Resync the request and response state machines. */
3871void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003872{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003873 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02003874#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01003875 int old_req_state = txn->req.msg_state;
3876 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02003877#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01003878
Willy Tarreau610ecce2010-01-04 21:15:02 +01003879 http_sync_req_state(s);
3880 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003881 if (!http_sync_res_state(s))
3882 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003883 if (!http_sync_req_state(s))
3884 break;
3885 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02003886
Christopher Faulet894da4c2017-07-18 11:29:07 +02003887 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
3888 "req->analysers=0x%08x res->analysers=0x%08x\n",
3889 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02003890 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
3891 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02003892 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02003893
3894
Willy Tarreau610ecce2010-01-04 21:15:02 +01003895 /* OK, both state machines agree on a compatible state.
3896 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01003897 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3898 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02003899 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
3900 * means we must abort the request.
3901 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
3902 * corresponding channel.
3903 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
3904 * on the response with server-close mode means we've completed one
3905 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003906 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02003907 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3908 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01003909 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003910 channel_auto_close(&s->req);
3911 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003912 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003913 channel_auto_close(&s->res);
3914 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003915 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02003916 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3917 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01003918 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01003919 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003920 channel_auto_close(&s->res);
3921 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003922 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003923 channel_abort(&s->req);
3924 channel_auto_close(&s->req);
3925 channel_auto_read(&s->req);
3926 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003927 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02003928 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3929 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3930 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3931 s->req.analysers &= AN_REQ_FLT_END;
3932 if (HAS_REQ_DATA_FILTERS(s))
3933 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
3934 }
3935 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3936 s->res.analysers &= AN_RES_FLT_END;
3937 if (HAS_RSP_DATA_FILTERS(s))
3938 s->res.analysers |= AN_RES_FLT_XFER_DATA;
3939 }
3940 channel_auto_close(&s->req);
3941 channel_auto_read(&s->req);
3942 channel_auto_close(&s->res);
3943 channel_auto_read(&s->res);
3944 }
Willy Tarreau4213a112013-12-15 10:25:42 +01003945 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
3946 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003947 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01003948 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3949 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3950 /* server-close/keep-alive: terminate this transaction,
3951 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02003952 * a fresh-new transaction, but only once we're sure there's
3953 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02003954 * another request. They must not hold any pending output data
3955 * and the response buffer must realigned
3956 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01003957 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003958 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02003959 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02003960 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02003961 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02003962 else {
3963 s->req.analysers = AN_REQ_FLT_END;
3964 s->res.analysers = AN_RES_FLT_END;
3965 txn->flags |= TX_WAIT_CLEANUP;
3966 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003967 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003968}
3969
Willy Tarreaud98cf932009-12-27 22:54:55 +01003970/* This function is an analyser which forwards request body (including chunk
3971 * sizes if any). It is called as soon as we must forward, even if we forward
3972 * zero byte. The only situation where it must not be called is when we're in
3973 * tunnel mode and we want to forward till the close. It's used both to forward
3974 * remaining data and to resync after end of body. It expects the msg_state to
3975 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02003976 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01003977 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02003978 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003979 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003980int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01003981{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003982 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003983 struct http_txn *txn = s->txn;
3984 struct http_msg *msg = &s->txn->req;
Christopher Faulet93e02d82019-03-08 14:18:50 +01003985 short status = 0;
Christopher Faulet3e344292015-11-24 16:24:13 +01003986 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003987
Christopher Faulete0768eb2018-10-03 16:38:02 +02003988 if (IS_HTX_STRM(s))
3989 return htx_request_forward_body(s, req, an_bit);
3990
Christopher Faulet45073512018-07-20 10:16:29 +02003991 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 +02003992 now_ms, __FUNCTION__,
3993 s,
3994 req,
3995 req->rex, req->wex,
3996 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003997 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02003998 req->analysers);
3999
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004000 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4001 return 0;
4002
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004003 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02004004 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004005 /* Output closed while we were sending data. We must abort and
4006 * wake the other side up.
4007 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004008 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004009 msg->msg_state = HTTP_MSG_ERROR;
4010 http_resync_states(s);
Christopher Faulet2f5c7842019-03-19 14:55:11 +01004011 return 1;
Willy Tarreau082b01c2010-01-02 23:58:04 +01004012 }
4013
Willy Tarreaud98cf932009-12-27 22:54:55 +01004014 /* Note that we don't have to send 100-continue back because we don't
4015 * need the data to complete our job, and it's up to the server to
4016 * decide whether to return 100, 417 or anything else in return of
4017 * an "Expect: 100-continue" header.
4018 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004019 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004020 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4021 ? HTTP_MSG_CHUNK_SIZE
4022 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004023
4024 /* TODO/filters: when http-buffer-request option is set or if a
4025 * rule on url_param exists, the first chunk size could be
4026 * already parsed. In that case, msg->next is after the chunk
4027 * size (including the CRLF after the size). So this case should
4028 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004029 }
4030
Willy Tarreau7ba23542014-04-17 21:50:00 +02004031 /* Some post-connect processing might want us to refrain from starting to
4032 * forward data. Currently, the only reason for this is "balance url_param"
4033 * whichs need to parse/process the request after we've enabled forwarding.
4034 */
4035 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004036 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004037 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004038 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004039 channel_dont_close(req); /* don't fail on early shutr */
4040 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004041 }
4042 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4043 }
4044
Willy Tarreau80a92c02014-03-12 10:41:13 +01004045 /* in most states, we should abort in case of early close */
4046 channel_auto_close(req);
4047
Willy Tarreauefdf0942014-04-24 20:08:57 +02004048 if (req->to_forward) {
4049 /* We can't process the buffer's contents yet */
4050 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004051 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004052 }
4053
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004054 if (msg->msg_state < HTTP_MSG_DONE) {
4055 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4056 ? http_msg_forward_chunked_body(s, msg)
4057 : http_msg_forward_body(s, msg));
4058 if (!ret)
4059 goto missing_data_or_waiting;
4060 if (ret < 0)
4061 goto return_bad_req;
4062 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004063
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004064 /* other states, DONE...TUNNEL */
4065 /* we don't want to forward closes on DONE except in tunnel mode. */
4066 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4067 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004068
Christopher Faulet894da4c2017-07-18 11:29:07 +02004069 http_resync_states(s);
4070 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004071 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4072 if (req->flags & CF_SHUTW) {
4073 /* request errors are most likely due to the
4074 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01004075 goto return_srv_abort;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004076 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004077 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004078 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004079 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004080 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004081 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004082 }
4083
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004084 /* If "option abortonclose" is set on the backend, we want to monitor
4085 * the client's connection and forward any shutdown notification to the
4086 * server, which will decide whether to close or to go on processing the
4087 * request. We only do that in tunnel mode, and not in other modes since
4088 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004089 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004090 channel_auto_read(req);
4091 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4092 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4093 s->si[1].flags |= SI_FL_NOLINGER;
4094 channel_auto_close(req);
4095 }
4096 else if (s->txn->meth == HTTP_METH_POST) {
4097 /* POST requests may require to read extra CRLF sent by broken
4098 * browsers and which could cause an RST to be sent upon close
4099 * on some systems (eg: Linux). */
4100 channel_auto_read(req);
4101 }
4102 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004103
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004104 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004105 /* stop waiting for data if the input is closed before the end */
Christopher Faulet93e02d82019-03-08 14:18:50 +01004106 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
4107 goto return_cli_abort;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004108
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004109 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004110 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004111 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01004112 goto return_srv_abort;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004113
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004114 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004115 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004116 * And when content-length is used, we never want to let the possible
4117 * shutdown be forwarded to the other side, as the state machine will
4118 * take care of it once the client responds. It's also important to
4119 * prevent TIME_WAITs from accumulating on the backend side, and for
4120 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004121 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004122 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004123 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004124
Willy Tarreau5c620922011-05-11 19:56:11 +02004125 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004126 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004127 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004128 * modes are already handled by the stream sock layer. We must not do
4129 * this in content-length mode because it could present the MSG_MORE
4130 * flag with the last block of forwarded data, which would cause an
4131 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004132 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004133 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004134 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004135
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004136 return 0;
4137
Christopher Faulet93e02d82019-03-08 14:18:50 +01004138 return_cli_abort:
4139 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4140 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
4141 if (objt_server(s->target))
4142 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
4143 if (!(s->flags & SF_ERR_MASK))
4144 s->flags |= SF_ERR_CLICL;
4145 status = 400;
4146 goto return_error;
4147
4148 return_srv_abort:
4149 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4150 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
4151 if (objt_server(s->target))
4152 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
4153 if (!(s->flags & SF_ERR_MASK))
4154 s->flags |= SF_ERR_SRVCL;
4155 status = 502;
4156 goto return_error;
4157
4158 return_bad_req: /* let's centralize all bad requests */
Olivier Houcharda798bf52019-03-08 18:52:00 +01004159 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004160 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01004161 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02004162 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01004163 s->flags |= SF_ERR_CLICL;
4164 status = 400;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004165
Christopher Faulet93e02d82019-03-08 14:18:50 +01004166 return_error:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004167 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004168 txn->req.msg_state = HTTP_MSG_ERROR;
Christopher Faulet93e02d82019-03-08 14:18:50 +01004169 if (txn->status > 0) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004170 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004171 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004172 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01004173 txn->status = status;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004174 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004175 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004176 req->analysers &= AN_REQ_FLT_END;
4177 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 +01004178 if (!(s->flags & SF_FINST_MASK))
4179 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004180 return 0;
4181}
4182
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004183/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4184 * processing can continue on next analysers, or zero if it either needs more
4185 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004186 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004187 * when it has nothing left to do, and may remove any analyser when it wants to
4188 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004189 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004190int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004191{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004192 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004193 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004194 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004195 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004196 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004197 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004198 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004199 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004200
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004201 srv_conn = cs_conn(objt_cs(s->si[1].end));
4202
Christopher Faulete0768eb2018-10-03 16:38:02 +02004203 if (IS_HTX_STRM(s))
4204 return htx_wait_for_response(s, rep, an_bit);
4205
Christopher Faulet45073512018-07-20 10:16:29 +02004206 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 +02004207 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004208 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004209 rep,
4210 rep->rex, rep->wex,
4211 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004212 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004213 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004214
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004215 /*
4216 * Now parse the partial (or complete) lines.
4217 * We will check the response syntax, and also join multi-line
4218 * headers. An index of all the lines will be elaborated while
4219 * parsing.
4220 *
4221 * For the parsing, we use a 28 states FSM.
4222 *
4223 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004224 * ci_head(rep) = beginning of response
4225 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4226 * ci_tail(rep) = end of input data
4227 * msg->eol = end of current header or line (LF or CRLF)
4228 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004229 */
4230
Willy Tarreau628c40c2014-04-24 19:11:26 +02004231 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004232 /* There's a protected area at the end of the buffer for rewriting
4233 * purposes. We don't want to start to parse the request if the
4234 * protected area is affected, because we may have to move processed
4235 * data later, which is much more complicated.
4236 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004237 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Christopher Faulet3d36d4e2019-09-04 09:39:42 +02004238 if (unlikely(!channel_is_rewritable(rep) && co_data(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004239 /* some data has still not left the buffer, wake us once that's done */
4240 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4241 goto abort_response;
4242 channel_dont_close(rep);
4243 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004244 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004245 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004246 }
4247
Willy Tarreau188e2302018-06-15 11:11:53 +02004248 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004249 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004250 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004251
Willy Tarreauf37954d2018-06-15 18:31:02 +02004252 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004253 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004254 }
4255
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004256 /* 1: we might have to print this header in debug mode */
4257 if (unlikely((global.mode & MODE_DEBUG) &&
4258 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004259 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004260 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004261
Willy Tarreauf37954d2018-06-15 18:31:02 +02004262 sol = ci_head(rep);
4263 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004264 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004265
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004266 sol += hdr_idx_first_pos(&txn->hdr_idx);
4267 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004268
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004269 while (cur_idx) {
4270 eol = sol + txn->hdr_idx.v[cur_idx].len;
4271 debug_hdr("srvhdr", s, sol, eol);
4272 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4273 cur_idx = txn->hdr_idx.v[cur_idx].next;
4274 }
4275 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004276
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004277 /*
4278 * Now we quickly check if we have found a full valid response.
4279 * If not so, we check the FD and buffer states before leaving.
4280 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004281 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004282 * responses are checked first.
4283 *
4284 * Depending on whether the client is still there or not, we
4285 * may send an error response back or not. Note that normally
4286 * we should only check for HTTP status there, and check I/O
4287 * errors somewhere else.
4288 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004289
Willy Tarreau655dce92009-11-08 13:10:58 +01004290 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004291 /* Invalid response */
4292 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4293 /* we detected a parsing error. We want to archive this response
4294 * in the dedicated proxy area for later troubleshooting.
4295 */
4296 hdr_response_bad:
4297 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004298 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004299
Olivier Houcharda798bf52019-03-08 18:52:00 +01004300 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004301 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004302 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004303 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004304 }
Willy Tarreau64648412010-03-05 10:41:54 +01004305 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004306 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004307 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004308 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004309 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004310 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004311 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004312
Willy Tarreaue7dff022015-04-03 01:14:29 +02004313 if (!(s->flags & SF_ERR_MASK))
4314 s->flags |= SF_ERR_PRXCOND;
4315 if (!(s->flags & SF_FINST_MASK))
4316 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004317
4318 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004319 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004320
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004321 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004322 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004323 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004324 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004325 goto hdr_response_bad;
4326 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004327
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004328 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004329 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004330 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004331 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004332 else if (txn->flags & TX_NOT_FIRST)
4333 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004334
Olivier Houcharda798bf52019-03-08 18:52:00 +01004335 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004336 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004337 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004338 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004339 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004340
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004341 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004342 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004343 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004344
4345 /* Check to see if the server refused the early data.
4346 * If so, just send a 425
4347 */
4348 if (objt_cs(s->si[1].end)) {
4349 struct connection *conn = objt_cs(s->si[1].end)->conn;
4350
4351 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4352 txn->status = 425;
4353 }
4354
Willy Tarreau350f4872014-11-28 14:42:25 +01004355 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004356 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004357 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004358
Willy Tarreaue7dff022015-04-03 01:14:29 +02004359 if (!(s->flags & SF_ERR_MASK))
4360 s->flags |= SF_ERR_SRVCL;
4361 if (!(s->flags & SF_FINST_MASK))
4362 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004363 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004364 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004365
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004366 /* read timeout : return a 504 to the client. */
4367 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004368 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004369 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004370
Olivier Houcharda798bf52019-03-08 18:52:00 +01004371 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004372 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004373 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004374 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004375 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004376
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004377 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004378 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004379 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004380 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004381 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004382 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004383
Willy Tarreaue7dff022015-04-03 01:14:29 +02004384 if (!(s->flags & SF_ERR_MASK))
4385 s->flags |= SF_ERR_SRVTO;
4386 if (!(s->flags & SF_FINST_MASK))
4387 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004388 return 0;
4389 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004390
Willy Tarreauf003d372012-11-26 13:35:37 +01004391 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004392 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004393 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4394 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004395 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004396 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004397
Christopher Faulet0184ea72017-01-05 14:06:34 +01004398 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004399 channel_auto_close(rep);
4400
4401 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004402 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004403 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004404
Willy Tarreaue7dff022015-04-03 01:14:29 +02004405 if (!(s->flags & SF_ERR_MASK))
4406 s->flags |= SF_ERR_CLICL;
4407 if (!(s->flags & SF_FINST_MASK))
4408 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004409
Willy Tarreau87b09662015-04-03 00:22:06 +02004410 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004411 return 0;
4412 }
4413
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004414 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004415 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004416 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004417 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004418 else if (txn->flags & TX_NOT_FIRST)
4419 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004420
Olivier Houcharda798bf52019-03-08 18:52:00 +01004421 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004422 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004423 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004424 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004425 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004426
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004427 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004428 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004429 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004430 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004431 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004432 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004433
Willy Tarreaue7dff022015-04-03 01:14:29 +02004434 if (!(s->flags & SF_ERR_MASK))
4435 s->flags |= SF_ERR_SRVCL;
4436 if (!(s->flags & SF_FINST_MASK))
4437 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004438 return 0;
4439 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004440
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004441 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004442 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004443 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004444 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004445 else if (txn->flags & TX_NOT_FIRST)
4446 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004447
Olivier Houcharda798bf52019-03-08 18:52:00 +01004448 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004449 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004450 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004451
Willy Tarreaue7dff022015-04-03 01:14:29 +02004452 if (!(s->flags & SF_ERR_MASK))
4453 s->flags |= SF_ERR_CLICL;
4454 if (!(s->flags & SF_FINST_MASK))
4455 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004456
Willy Tarreau87b09662015-04-03 00:22:06 +02004457 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004458 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004459 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004460
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004461 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004462 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004463 return 0;
4464 }
4465
4466 /* More interesting part now : we know that we have a complete
4467 * response which at least looks like HTTP. We have an indicator
4468 * of each header's length, so we can parse them quickly.
4469 */
4470
4471 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004472 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004473
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004474 /*
4475 * 1: get the status code
4476 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004477 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004478 if (n < 1 || n > 5)
4479 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004480 /* when the client triggers a 4xx from the server, it's most often due
4481 * to a missing object or permission. These events should be tracked
4482 * because if they happen often, it may indicate a brute force or a
4483 * vulnerability scan.
4484 */
4485 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004486 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004487
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004488 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004489 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004490
Willy Tarreau91852eb2015-05-01 13:26:00 +02004491 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4492 * exactly one digit "." one digit. This check may be disabled using
4493 * option accept-invalid-http-response.
4494 */
4495 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4496 if (msg->sl.st.v_l != 8) {
4497 msg->err_pos = 0;
4498 goto hdr_response_bad;
4499 }
4500
Willy Tarreauf37954d2018-06-15 18:31:02 +02004501 if (ci_head(rep)[4] != '/' ||
4502 !isdigit((unsigned char)ci_head(rep)[5]) ||
4503 ci_head(rep)[6] != '.' ||
4504 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004505 msg->err_pos = 4;
4506 goto hdr_response_bad;
4507 }
4508 }
4509
Willy Tarreau5b154472009-12-21 20:11:07 +01004510 /* check if the response is HTTP/1.1 or above */
4511 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004512 ((ci_head(rep)[5] > '1') ||
4513 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004514 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004515
4516 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004517 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 +01004518
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004519 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004520 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004521
Willy Tarreauf37954d2018-06-15 18:31:02 +02004522 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004523
Willy Tarreau39650402010-03-15 19:44:39 +01004524 /* Adjust server's health based on status code. Note: status codes 501
4525 * and 505 are triggered on demand by client request, so we must not
4526 * count them as server failures.
4527 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004528 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004529 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004530 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004531 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004532 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004533 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004534
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004535 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004536 * We may be facing a 100-continue response, or any other informational
4537 * 1xx response which is non-final, in which case this is not the right
4538 * response, and we're waiting for the next one. Let's allow this response
4539 * to go to the client and wait for the next one. There's an exception for
4540 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004541 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004542 if (txn->status < 200 &&
4543 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004544 hdr_idx_init(&txn->hdr_idx);
4545 msg->next -= channel_forward(rep, msg->next);
4546 msg->msg_state = HTTP_MSG_RPBEFORE;
4547 txn->status = 0;
4548 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004549 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004550 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004551 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004552
Willy Tarreaua14ad722017-07-07 11:36:32 +02004553 /*
4554 * 2: check for cacheability.
4555 */
4556
4557 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004558 case 200:
4559 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004560 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004561 case 206:
4562 case 300:
4563 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004564 case 404:
4565 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004566 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004567 case 414:
4568 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004569 break;
4570 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004571 /* RFC7231#6.1:
4572 * Responses with status codes that are defined as
4573 * cacheable by default (e.g., 200, 203, 204, 206,
4574 * 300, 301, 404, 405, 410, 414, and 501 in this
4575 * specification) can be reused by a cache with
4576 * heuristic expiration unless otherwise indicated
4577 * by the method definition or explicit cache
4578 * controls [RFC7234]; all other status codes are
4579 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004580 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004581 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004582 break;
4583 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004584
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004585 /*
4586 * 3: we may need to capture headers
4587 */
4588 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004589 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004590 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4591 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004592
Willy Tarreau557f1992015-05-01 10:05:17 +02004593 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4594 * by RFC7230#3.3.3 :
4595 *
4596 * The length of a message body is determined by one of the following
4597 * (in order of precedence):
4598 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004599 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4600 * the connection will become a tunnel immediately after the empty
4601 * line that concludes the header fields. A client MUST ignore
4602 * any Content-Length or Transfer-Encoding header fields received
4603 * in such a message. Any 101 response (Switching Protocols) is
4604 * managed in the same manner.
4605 *
4606 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004607 * (Informational), 204 (No Content), or 304 (Not Modified) status
4608 * code is always terminated by the first empty line after the
4609 * header fields, regardless of the header fields present in the
4610 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004611 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004612 * 3. If a Transfer-Encoding header field is present and the chunked
4613 * transfer coding (Section 4.1) is the final encoding, the message
4614 * body length is determined by reading and decoding the chunked
4615 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004616 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004617 * If a Transfer-Encoding header field is present in a response and
4618 * the chunked transfer coding is not the final encoding, the
4619 * message body length is determined by reading the connection until
4620 * it is closed by the server. If a Transfer-Encoding header field
4621 * is present in a request and the chunked transfer coding is not
4622 * the final encoding, the message body length cannot be determined
4623 * reliably; the server MUST respond with the 400 (Bad Request)
4624 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004625 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004626 * If a message is received with both a Transfer-Encoding and a
4627 * Content-Length header field, the Transfer-Encoding overrides the
4628 * Content-Length. Such a message might indicate an attempt to
4629 * perform request smuggling (Section 9.5) or response splitting
4630 * (Section 9.4) and ought to be handled as an error. A sender MUST
4631 * remove the received Content-Length field prior to forwarding such
4632 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004633 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004634 * 4. If a message is received without Transfer-Encoding and with
4635 * either multiple Content-Length header fields having differing
4636 * field-values or a single Content-Length header field having an
4637 * invalid value, then the message framing is invalid and the
4638 * recipient MUST treat it as an unrecoverable error. If this is a
4639 * request message, the server MUST respond with a 400 (Bad Request)
4640 * status code and then close the connection. If this is a response
4641 * message received by a proxy, the proxy MUST close the connection
4642 * to the server, discard the received response, and send a 502 (Bad
4643 * Gateway) response to the client. If this is a response message
4644 * received by a user agent, the user agent MUST close the
4645 * connection to the server and discard the received response.
4646 *
4647 * 5. If a valid Content-Length header field is present without
4648 * Transfer-Encoding, its decimal value defines the expected message
4649 * body length in octets. If the sender closes the connection or
4650 * the recipient times out before the indicated number of octets are
4651 * received, the recipient MUST consider the message to be
4652 * incomplete and close the connection.
4653 *
4654 * 6. If this is a request message and none of the above are true, then
4655 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004656 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004657 * 7. Otherwise, this is a response message without a declared message
4658 * body length, so the message body length is determined by the
4659 * number of octets received prior to the server closing the
4660 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004661 */
4662
4663 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004664 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004665 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004666 * FIXME: should we parse anyway and return an error on chunked encoding ?
4667 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004668 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4669 txn->status == 101)) {
4670 /* Either we've established an explicit tunnel, or we're
4671 * switching the protocol. In both cases, we're very unlikely
4672 * to understand the next protocols. We have to switch to tunnel
4673 * mode, so that we transfer the request and responses then let
4674 * this protocol pass unmodified. When we later implement specific
4675 * parsers for such protocols, we'll want to check the Upgrade
4676 * header which contains information about that protocol for
4677 * responses with status 101 (eg: see RFC2817 about TLS).
4678 */
4679 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4680 msg->flags |= HTTP_MSGF_XFER_LEN;
4681 goto end;
4682 }
4683
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004684 if (txn->meth == HTTP_METH_HEAD ||
4685 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004686 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004687 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004688 goto skip_content_length;
4689 }
4690
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004691 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004692 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004693 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004694 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004695 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4696 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004697 /* bad transfer-encoding (chunked followed by something else) */
4698 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004699 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004700 break;
4701 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004702 }
4703
Willy Tarreau196a7df2019-09-12 14:01:40 +02004704 /* "chunked" mandatory if transfer-encoding is used */
4705 if (ctx.idx && !(msg->flags & HTTP_MSGF_TE_CHNK)) {
4706 use_close_only = 1;
4707 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4708 }
4709
Willy Tarreau1c913912015-04-30 10:57:51 +02004710 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004711 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02004712 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004713 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02004714 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4715 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02004716 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004717 signed long long cl;
4718
Willy Tarreauad14f752011-09-02 20:33:27 +02004719 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004720 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004721 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004722 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004723
Willy Tarreauad14f752011-09-02 20:33:27 +02004724 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004725 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004726 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004727 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004728
Willy Tarreauad14f752011-09-02 20:33:27 +02004729 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004730 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004731 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004732 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004733
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004734 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004735 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004736 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004737 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004738
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004739 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004740 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004741 }
4742
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004743 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
4744 * 407 (Proxy-Authenticate) responses and set the connection to private
4745 */
4746 if (srv_conn && txn->status == 401) {
4747 /* check for Negotiate/NTLM WWW-Authenticate headers */
4748 ctx.idx = 0;
4749 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
4750 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
4751 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
4752 srv_conn->flags |= CO_FL_PRIVATE;
4753 }
4754 } else if (srv_conn && txn->status == 407) {
4755 /* check for Negotiate/NTLM Proxy-Authenticate headers */
4756 ctx.idx = 0;
4757 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
4758 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
4759 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
4760 srv_conn->flags |= CO_FL_PRIVATE;
4761 }
4762 }
4763
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004764 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01004765 /* Now we have to check if we need to modify the Connection header.
4766 * This is more difficult on the response than it is on the request,
4767 * because we can have two different HTTP versions and we don't know
4768 * how the client will interprete a response. For instance, let's say
4769 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4770 * HTTP/1.1 response without any header. Maybe it will bound itself to
4771 * HTTP/1.0 because it only knows about it, and will consider the lack
4772 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4773 * the lack of header as a keep-alive. Thus we will use two flags
4774 * indicating how a request MAY be understood by the client. In case
4775 * of multiple possibilities, we'll fix the header to be explicit. If
4776 * ambiguous cases such as both close and keepalive are seen, then we
4777 * will fall back to explicit close. Note that we won't take risks with
4778 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004779 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004780 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004781 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02004782 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01004783 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004784
Willy Tarreau60466522010-01-18 19:08:45 +01004785 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02004786 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01004787 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004788
Willy Tarreau60466522010-01-18 19:08:45 +01004789 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02004790 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01004791 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004792 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004793 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004794 }
Willy Tarreau60466522010-01-18 19:08:45 +01004795 else { /* SCL / KAL */
4796 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004797 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004798 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004799 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004800
Willy Tarreau60466522010-01-18 19:08:45 +01004801 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004802 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004803
Willy Tarreau60466522010-01-18 19:08:45 +01004804 /* Some keep-alive responses are converted to Server-close if
4805 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004806 */
Willy Tarreau60466522010-01-18 19:08:45 +01004807 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4808 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004809 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004810 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004811 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004812 }
4813
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004814 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02004815 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02004816 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02004817
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004818 /* end of job, return OK */
4819 rep->analysers &= ~an_bit;
4820 rep->analyse_exp = TICK_ETERNITY;
4821 channel_auto_close(rep);
4822 return 1;
4823
4824 abort_keep_alive:
4825 /* A keep-alive request to the server failed on a network error.
4826 * The client is required to retry. We need to close without returning
4827 * any other information so that the client retries.
4828 */
4829 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004830 rep->analysers &= AN_RES_FLT_END;
4831 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004832 channel_auto_close(rep);
4833 s->logs.logwait = 0;
4834 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004835 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01004836 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004837 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004838 return 0;
4839}
4840
4841/* This function performs all the processing enabled for the current response.
4842 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004843 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004844 * other functions. It works like process_request (see indications above).
4845 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004846int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004847{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004848 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004849 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004850 struct http_msg *msg = &txn->rsp;
4851 struct proxy *cur_proxy;
4852 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004853 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004854
Christopher Faulete0768eb2018-10-03 16:38:02 +02004855 if (IS_HTX_STRM(s))
4856 return htx_process_res_common(s, rep, an_bit, px);
4857
Christopher Faulet45073512018-07-20 10:16:29 +02004858 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 +02004859 now_ms, __FUNCTION__,
4860 s,
4861 rep,
4862 rep->rex, rep->wex,
4863 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004864 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004865 rep->analysers);
4866
4867 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
4868 return 0;
4869
Willy Tarreau70730dd2014-04-24 18:06:27 +02004870 /* The stats applet needs to adjust the Connection header but we don't
4871 * apply any filter there.
4872 */
Willy Tarreau612adb82015-03-10 15:25:54 +01004873 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
4874 rep->analysers &= ~an_bit;
4875 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02004876 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01004877 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02004878
Willy Tarreau58975672014-04-24 21:13:57 +02004879 /*
4880 * We will have to evaluate the filters.
4881 * As opposed to version 1.2, now they will be evaluated in the
4882 * filters order and not in the header order. This means that
4883 * each filter has to be validated among all headers.
4884 *
4885 * Filters are tried with ->be first, then with ->fe if it is
4886 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004887 *
4888 * Maybe we are in resume condiion. In this case I choose the
4889 * "struct proxy" which contains the rule list matching the resume
4890 * pointer. If none of theses "struct proxy" match, I initialise
4891 * the process with the first one.
4892 *
4893 * In fact, I check only correspondance betwwen the current list
4894 * pointer and the ->fe rule list. If it doesn't match, I initialize
4895 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02004896 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004897 if (s->current_rule_list == &sess->fe->http_res_rules)
4898 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004899 else
4900 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02004901 while (1) {
4902 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004903
Willy Tarreau58975672014-04-24 21:13:57 +02004904 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02004905 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004906 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004907
Willy Tarreau51d861a2015-05-22 17:30:48 +02004908 if (ret == HTTP_RULE_RES_BADREQ)
4909 goto return_srv_prx_502;
4910
4911 if (ret == HTTP_RULE_RES_DONE) {
4912 rep->analysers &= ~an_bit;
4913 rep->analyse_exp = TICK_ETERNITY;
4914 return 1;
4915 }
4916 }
4917
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004918 /* we need to be called again. */
4919 if (ret == HTTP_RULE_RES_YIELD) {
4920 channel_dont_close(rep);
4921 return 0;
4922 }
4923
Willy Tarreau58975672014-04-24 21:13:57 +02004924 /* try headers filters */
4925 if (rule_set->rsp_exp != NULL) {
4926 if (apply_filters_to_response(s, rep, rule_set) < 0) {
4927 return_bad_resp:
4928 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004929 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004930 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004931 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01004932 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02004933 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004934 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02004935 txn->status = 502;
4936 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01004937 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004938 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004939 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02004940 if (!(s->flags & SF_ERR_MASK))
4941 s->flags |= SF_ERR_PRXCOND;
4942 if (!(s->flags & SF_FINST_MASK))
4943 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02004944 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004945 }
Willy Tarreau58975672014-04-24 21:13:57 +02004946 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004947
Willy Tarreau58975672014-04-24 21:13:57 +02004948 /* has the response been denied ? */
4949 if (txn->flags & TX_SVDENY) {
4950 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004951 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004952
Olivier Houcharda798bf52019-03-08 18:52:00 +01004953 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
4954 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004955 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01004956 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004957
Willy Tarreau58975672014-04-24 21:13:57 +02004958 goto return_srv_prx_502;
4959 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004960
Willy Tarreau58975672014-04-24 21:13:57 +02004961 /* add response headers from the rule sets in the same order */
4962 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02004963 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004964 break;
Willy Tarreau58975672014-04-24 21:13:57 +02004965 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004966 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02004967 ret = acl_pass(ret);
4968 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4969 ret = !ret;
4970 if (!ret)
4971 continue;
4972 }
Christopher Faulet10079f52018-10-03 15:17:28 +02004973 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02004974 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004975 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004976
Willy Tarreau58975672014-04-24 21:13:57 +02004977 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004978 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02004979 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004980 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02004981 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004982
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004983 /* After this point, this anayzer can't return yield, so we can
4984 * remove the bit corresponding to this analyzer from the list.
4985 *
4986 * Note that the intermediate returns and goto found previously
4987 * reset the analyzers.
4988 */
4989 rep->analysers &= ~an_bit;
4990 rep->analyse_exp = TICK_ETERNITY;
4991
Willy Tarreau58975672014-04-24 21:13:57 +02004992 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02004993 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02004994 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004995
Willy Tarreau58975672014-04-24 21:13:57 +02004996 /*
4997 * Now check for a server cookie.
4998 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02004999 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005000 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005001
Willy Tarreau58975672014-04-24 21:13:57 +02005002 /*
5003 * Check for cache-control or pragma headers if required.
5004 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005005 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005006 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005007
Willy Tarreau58975672014-04-24 21:13:57 +02005008 /*
5009 * Add server cookie in the response if needed
5010 */
5011 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5012 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005013 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005014 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5015 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5016 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5017 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5018 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005019 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005020 /* the server is known, it's not the one the client requested, or the
5021 * cookie's last seen date needs to be refreshed. We have to
5022 * insert a set-cookie here, except if we want to insert only on POST
5023 * requests and this one isn't. Note that servers which don't have cookies
5024 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005025 */
Willy Tarreau58975672014-04-24 21:13:57 +02005026 if (!objt_server(s->target)->cookie) {
5027 chunk_printf(&trash,
5028 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5029 s->be->cookie_name);
5030 }
5031 else {
5032 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005033
Willy Tarreau58975672014-04-24 21:13:57 +02005034 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5035 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005036 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5037 s30tob64((date.tv_sec+3) >> 2,
5038 trash.area + trash.data);
5039 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005040
Willy Tarreau58975672014-04-24 21:13:57 +02005041 if (s->be->cookie_maxlife) {
5042 /* emit first_date, which is either the original one or
5043 * the current date.
5044 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005045 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005046 s30tob64(txn->cookie_first_date ?
5047 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005048 (date.tv_sec+3) >> 2,
5049 trash.area + trash.data);
5050 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005051 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005052 }
Willy Tarreau58975672014-04-24 21:13:57 +02005053 chunk_appendf(&trash, "; path=/");
5054 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005055
Willy Tarreau58975672014-04-24 21:13:57 +02005056 if (s->be->cookie_domain)
5057 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005058
Willy Tarreau58975672014-04-24 21:13:57 +02005059 if (s->be->ck_opts & PR_CK_HTTPONLY)
5060 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005061
Willy Tarreau58975672014-04-24 21:13:57 +02005062 if (s->be->ck_opts & PR_CK_SECURE)
5063 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005064
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005065 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005066 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005067
Willy Tarreau58975672014-04-24 21:13:57 +02005068 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005069 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005070 /* the server did not change, only the date was updated */
5071 txn->flags |= TX_SCK_UPDATED;
5072 else
5073 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005074
Willy Tarreau58975672014-04-24 21:13:57 +02005075 /* Here, we will tell an eventual cache on the client side that we don't
5076 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5077 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5078 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005079 */
Willy Tarreau58975672014-04-24 21:13:57 +02005080 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005081
Willy Tarreau58975672014-04-24 21:13:57 +02005082 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005083
Willy Tarreau58975672014-04-24 21:13:57 +02005084 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5085 "Cache-control: private", 22) < 0))
5086 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005087 }
Willy Tarreau58975672014-04-24 21:13:57 +02005088 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005089
Willy Tarreau58975672014-04-24 21:13:57 +02005090 /*
5091 * Check if result will be cacheable with a cookie.
5092 * We'll block the response if security checks have caught
5093 * nasty things such as a cacheable cookie.
5094 */
5095 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5096 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5097 (s->be->options & PR_O_CHK_CACHE)) {
5098 /* we're in presence of a cacheable response containing
5099 * a set-cookie header. We'll block it as requested by
5100 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005101 */
Willy Tarreau58975672014-04-24 21:13:57 +02005102 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01005103 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005104
Olivier Houcharda798bf52019-03-08 18:52:00 +01005105 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5106 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005107 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01005108 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005109
Christopher Faulet767a84b2017-11-24 16:50:31 +01005110 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5111 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005112 send_log(s->be, LOG_ALERT,
5113 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5114 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5115 goto return_srv_prx_502;
5116 }
Willy Tarreau03945942009-12-22 16:50:27 +01005117
Willy Tarreau70730dd2014-04-24 18:06:27 +02005118 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005119 /*
5120 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5121 * If an "Upgrade" token is found, the header is left untouched in order
5122 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005123 * if anything but "Upgrade" is present in the Connection header. We don't
5124 * want to touch any 101 response either since it's switching to another
5125 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005126 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005127 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005128 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005129 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005130
Willy Tarreau58975672014-04-24 21:13:57 +02005131 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5132 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5133 /* we want a keep-alive response here. Keep-alive header
5134 * required if either side is not 1.1.
5135 */
5136 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5137 want_flags |= TX_CON_KAL_SET;
5138 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005139 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005140 /* we want a close response here. Close header required if
5141 * the server is 1.1, regardless of the client.
5142 */
5143 if (msg->flags & HTTP_MSGF_VER_11)
5144 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005145 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005146
Willy Tarreau58975672014-04-24 21:13:57 +02005147 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5148 http_change_connection_header(txn, msg, want_flags);
5149 }
5150
5151 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005152 /* Always enter in the body analyzer */
5153 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5154 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005155
Willy Tarreau58975672014-04-24 21:13:57 +02005156 /* if the user wants to log as soon as possible, without counting
5157 * bytes from the server, then this is the right moment. We have
5158 * to temporarily assign bytes_out to log what we currently have.
5159 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005160 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005161 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5162 s->logs.bytes_out = txn->rsp.eoh;
5163 s->do_log(s);
5164 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005165 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005166 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005167}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005168
Willy Tarreaud98cf932009-12-27 22:54:55 +01005169/* This function is an analyser which forwards response body (including chunk
5170 * sizes if any). It is called as soon as we must forward, even if we forward
5171 * zero byte. The only situation where it must not be called is when we're in
5172 * tunnel mode and we want to forward till the close. It's used both to forward
5173 * remaining data and to resync after end of body. It expects the msg_state to
5174 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005175 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005176 *
5177 * It is capable of compressing response data both in content-length mode and
5178 * in chunked mode. The state machines follows different flows depending on
5179 * whether content-length and chunked modes are used, since there are no
5180 * trailers in content-length :
5181 *
5182 * chk-mode cl-mode
5183 * ,----- BODY -----.
5184 * / \
5185 * V size > 0 V chk-mode
5186 * .--> SIZE -------------> DATA -------------> CRLF
5187 * | | size == 0 | last byte |
5188 * | v final crlf v inspected |
5189 * | TRAILERS -----------> DONE |
5190 * | |
5191 * `----------------------------------------------'
5192 *
5193 * Compression only happens in the DATA state, and must be flushed in final
5194 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5195 * is performed at once on final states for all bytes parsed, or when leaving
5196 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005197 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005198int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005199{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005200 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005201 struct http_txn *txn = s->txn;
5202 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005203 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005204
Christopher Faulete0768eb2018-10-03 16:38:02 +02005205 if (IS_HTX_STRM(s))
5206 return htx_response_forward_body(s, res, an_bit);
5207
Christopher Faulet45073512018-07-20 10:16:29 +02005208 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 +02005209 now_ms, __FUNCTION__,
5210 s,
5211 res,
5212 res->rex, res->wex,
5213 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005214 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005215 res->analysers);
5216
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005217 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5218 return 0;
5219
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005220 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005221 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Faulet93e02d82019-03-08 14:18:50 +01005222 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005223 /* Output closed while we were sending data. We must abort and
5224 * wake the other side up.
5225 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005226 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005227 msg->msg_state = HTTP_MSG_ERROR;
5228 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005229 return 1;
5230 }
5231
Willy Tarreau4fe41902010-06-07 22:27:41 +02005232 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005233 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005234
Christopher Fauletd7c91962015-04-30 11:48:27 +02005235 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005236 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5237 ? HTTP_MSG_CHUNK_SIZE
5238 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005239 }
5240
Willy Tarreauefdf0942014-04-24 20:08:57 +02005241 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005242 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005243 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005244 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005245 }
5246
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005247 if (msg->msg_state < HTTP_MSG_DONE) {
5248 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5249 ? http_msg_forward_chunked_body(s, msg)
5250 : http_msg_forward_body(s, msg));
5251 if (!ret)
5252 goto missing_data_or_waiting;
5253 if (ret < 0)
5254 goto return_bad_res;
5255 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005256
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005257 /* other states, DONE...TUNNEL */
5258 /* for keep-alive we don't want to forward closes on DONE */
5259 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5260 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5261 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005262
Christopher Faulet894da4c2017-07-18 11:29:07 +02005263 http_resync_states(s);
5264 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005265 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5266 if (res->flags & CF_SHUTW) {
5267 /* response errors are most likely due to the
5268 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01005269 goto return_cli_abort;
Willy Tarreau5523b322009-12-29 12:05:52 +01005270 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005271 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005272 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005273 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005274 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005275 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005276 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005277 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005278
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005279 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005280 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01005281 goto return_cli_abort;
Willy Tarreauf003d372012-11-26 13:35:37 +01005282
5283 /* stop waiting for data if the input is closed before the end. If the
5284 * client side was already closed, it means that the client has aborted,
5285 * so we don't want to count this as a server abort. Otherwise it's a
5286 * server abort.
5287 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005288 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005289 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01005290 goto return_cli_abort;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005291 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01005292 if (!ci_data(res))
5293 goto return_srv_abort;
Willy Tarreau40dba092010-03-04 18:14:51 +01005294 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005295
Willy Tarreau40dba092010-03-04 18:14:51 +01005296 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005297 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005298 goto return_bad_res;
5299
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005300 /* When TE: chunked is used, we need to get there again to parse
5301 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005302 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5303 * or if there are filters registered on the stream, we don't want to
5304 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005305 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005306 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005307 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005308 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5309 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005310 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005311
Willy Tarreau5c620922011-05-11 19:56:11 +02005312 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005313 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005314 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005315 * modes are already handled by the stream sock layer. We must not do
5316 * this in content-length mode because it could present the MSG_MORE
5317 * flag with the last block of forwarded data, which would cause an
5318 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005319 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005320 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005321 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005322
Willy Tarreau87b09662015-04-03 00:22:06 +02005323 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005324 return 0;
5325
Christopher Faulet93e02d82019-03-08 14:18:50 +01005326 return_srv_abort:
5327 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
5328 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005329 if (objt_server(s->target))
Christopher Faulet93e02d82019-03-08 14:18:50 +01005330 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
5331 if (!(s->flags & SF_ERR_MASK))
5332 s->flags |= SF_ERR_SRVCL;
5333 goto return_error;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005334
Christopher Faulet93e02d82019-03-08 14:18:50 +01005335 return_cli_abort:
5336 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5337 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005338 if (objt_server(s->target))
Christopher Faulet93e02d82019-03-08 14:18:50 +01005339 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
5340 if (!(s->flags & SF_ERR_MASK))
5341 s->flags |= SF_ERR_CLICL;
5342 goto return_error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005343
Christopher Faulet93e02d82019-03-08 14:18:50 +01005344 return_bad_res:
5345 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
5346 if (objt_server(s->target)) {
5347 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
5348 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
5349 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005350 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01005351 s->flags |= SF_ERR_SRVCL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005352
Christopher Faulet93e02d82019-03-08 14:18:50 +01005353 return_error:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005354 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005355 txn->rsp.msg_state = HTTP_MSG_ERROR;
5356 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005357 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005358 res->analysers &= AN_RES_FLT_END;
5359 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 +02005360 if (!(s->flags & SF_FINST_MASK))
5361 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005362 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005363}
5364
5365
Christopher Faulet10079f52018-10-03 15:17:28 +02005366int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005367{
5368 struct channel *chn = msg->chn;
5369 int ret;
5370
5371 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5372
5373 if (msg->msg_state == HTTP_MSG_ENDING)
5374 goto ending;
5375
5376 /* Neither content-length, nor transfer-encoding was found, so we must
5377 * read the body until the server connection is closed. In that case, we
5378 * eat data as they come. Of course, this happens for response only. */
5379 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005380 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005381 msg->chunk_len += len;
5382 msg->body_len += len;
5383 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005384 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005385 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005386 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005387 msg->next += ret;
5388 msg->chunk_len -= ret;
5389 if (msg->chunk_len) {
5390 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005391 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005392 chn->flags |= CF_WAKE_WRITE;
5393 goto missing_data_or_waiting;
5394 }
5395
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005396 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5397 * always set for a request. */
5398 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5399 /* The server still sending data that should be filtered */
5400 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5401 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005402 msg->msg_state = HTTP_MSG_TUNNEL;
5403 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005404 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005405
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005406 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet297d3e22019-03-22 14:16:14 +01005407 chn->flags |= CF_EOI;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005408
5409 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005410 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005411 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005412 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5413 /* default_ret */ msg->next,
5414 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005415 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005416 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005417 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5418 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005419 if (msg->next)
5420 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005421
Christopher Fauletda02e172015-12-04 09:25:05 +01005422 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5423 /* default_ret */ 1,
5424 /* on_error */ goto error,
5425 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005426 if (msg->msg_state == HTTP_MSG_ENDING)
5427 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005428 return 1;
5429
5430 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005431 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005432 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5433 /* default_ret */ msg->next,
5434 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005435 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005436 msg->next -= ret;
5437 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5438 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005439 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005440 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005441 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005442 return 0;
5443 error:
5444 return -1;
5445}
5446
Christopher Faulet10079f52018-10-03 15:17:28 +02005447int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005448{
5449 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005450 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005451 int ret;
5452
5453 /* Here we have the guarantee to be in one of the following state:
5454 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5455 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5456
Christopher Fauletca874b82018-09-20 11:31:01 +02005457 if (msg->msg_state == HTTP_MSG_ENDING)
5458 goto ending;
5459
5460 /* Don't parse chunks if there is no input data */
5461 if (!ci_data(chn))
5462 goto waiting;
5463
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005464 switch_states:
5465 switch (msg->msg_state) {
5466 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005467 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005468 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005469 /* on_error */ goto error);
5470 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005471 msg->chunk_len -= ret;
5472 if (msg->chunk_len) {
5473 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005474 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005475 chn->flags |= CF_WAKE_WRITE;
5476 goto missing_data_or_waiting;
5477 }
5478
5479 /* nothing left to forward for this chunk*/
5480 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5481 /* fall through for HTTP_MSG_CHUNK_CRLF */
5482
5483 case HTTP_MSG_CHUNK_CRLF:
5484 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005485 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005486 if (ret == 0)
5487 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005488 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005489 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005490 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005491 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005492 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005493 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005494 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005495 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5496 /* fall through for HTTP_MSG_CHUNK_SIZE */
5497
5498 case HTTP_MSG_CHUNK_SIZE:
5499 /* read the chunk size and assign it to ->chunk_len,
5500 * then set ->next to point to the body and switch to
5501 * DATA or TRAILERS state.
5502 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005503 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005504 if (ret == 0)
5505 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005506 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005507 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005508 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005509 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005510 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005511 }
5512
5513 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005514 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005515 msg->chunk_len = chunk;
5516 msg->body_len += chunk;
5517
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005518 if (msg->chunk_len) {
5519 msg->msg_state = HTTP_MSG_DATA;
5520 goto switch_states;
5521 }
5522 msg->msg_state = HTTP_MSG_TRAILERS;
5523 /* fall through for HTTP_MSG_TRAILERS */
5524
5525 case HTTP_MSG_TRAILERS:
5526 ret = http_forward_trailers(msg);
5527 if (ret < 0)
5528 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005529 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5530 /* default_ret */ 1,
5531 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005532 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005533 if (!ret)
5534 goto missing_data_or_waiting;
5535 break;
5536
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005537 default:
5538 /* This should no happen in this function */
5539 goto error;
5540 }
5541
5542 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet297d3e22019-03-22 14:16:14 +01005543 chn->flags |= CF_EOI;
5544
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005545 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005546 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005547 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005548 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005549 /* default_ret */ msg->next,
5550 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005551 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005552 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005553 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5554 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005555 if (msg->next)
5556 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005557
Christopher Fauletda02e172015-12-04 09:25:05 +01005558 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005559 /* default_ret */ 1,
5560 /* on_error */ goto error,
5561 /* on_wait */ goto waiting);
5562 msg->msg_state = HTTP_MSG_DONE;
5563 return 1;
5564
5565 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005566 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005567 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005568 /* default_ret */ msg->next,
5569 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005570 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005571 msg->next -= ret;
5572 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5573 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005574 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005575 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005576 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005577 return 0;
5578
5579 chunk_parsing_error:
5580 if (msg->err_pos >= 0) {
5581 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005582 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005583 msg->msg_state, strm_fe(s));
5584 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005585 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005586 msg, msg->msg_state, s->be);
5587 }
5588 error:
5589 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005590}
5591
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005592
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005593/* Iterate the same filter through all request headers.
5594 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005595 * Since it can manage the switch to another backend, it updates the per-proxy
5596 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005597 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005598int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005599{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005600 char *cur_ptr, *cur_end, *cur_next;
5601 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005602 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005603 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005604 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005605
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005606 last_hdr = 0;
5607
Willy Tarreauf37954d2018-06-15 18:31:02 +02005608 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005609 old_idx = 0;
5610
5611 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005612 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005613 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005614 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005615 (exp->action == ACT_ALLOW ||
5616 exp->action == ACT_DENY ||
5617 exp->action == ACT_TARPIT))
5618 return 0;
5619
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005620 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005621 if (!cur_idx)
5622 break;
5623
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005624 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005625 cur_ptr = cur_next;
5626 cur_end = cur_ptr + cur_hdr->len;
5627 cur_next = cur_end + cur_hdr->cr + 1;
5628
5629 /* Now we have one header between cur_ptr and cur_end,
5630 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005631 */
5632
Willy Tarreau15a53a42015-01-21 13:39:42 +01005633 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005634 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005635 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005636 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005637 last_hdr = 1;
5638 break;
5639
5640 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005641 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005642 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005643 break;
5644
5645 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005646 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005647 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005648 break;
5649
5650 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005651 len = exp_replace(trash.area,
5652 trash.size, cur_ptr,
5653 exp->replace, pmatch);
5654 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005655 return -1;
5656
Willy Tarreau6e27be12018-08-22 04:46:47 +02005657 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5658
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005659 /* FIXME: if the user adds a newline in the replacement, the
5660 * index will not be recalculated for now, and the new line
5661 * will not be counted as a new header.
5662 */
5663
5664 cur_end += delta;
5665 cur_next += delta;
5666 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005667 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005668 break;
5669
5670 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005671 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005672 cur_next += delta;
5673
Willy Tarreaufa355d42009-11-29 18:12:29 +01005674 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005675 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5676 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005677 cur_hdr->len = 0;
5678 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005679 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005680 break;
5681
5682 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005683 }
5684
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005685 /* keep the link from this header to next one in case of later
5686 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005687 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005688 old_idx = cur_idx;
5689 }
5690 return 0;
5691}
5692
5693
5694/* Apply the filter to the request line.
5695 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5696 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005697 * Since it can manage the switch to another backend, it updates the per-proxy
5698 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005699 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005700int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005701{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005702 char *cur_ptr, *cur_end;
5703 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005704 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005705 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005706
Willy Tarreau3d300592007-03-18 18:34:41 +01005707 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005708 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005709 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005710 (exp->action == ACT_ALLOW ||
5711 exp->action == ACT_DENY ||
5712 exp->action == ACT_TARPIT))
5713 return 0;
5714 else if (exp->action == ACT_REMOVE)
5715 return 0;
5716
5717 done = 0;
5718
Willy Tarreauf37954d2018-06-15 18:31:02 +02005719 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005720 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005721
5722 /* Now we have the request line between cur_ptr and cur_end */
5723
Willy Tarreau15a53a42015-01-21 13:39:42 +01005724 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005725 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005726 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005727 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005728 done = 1;
5729 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005730
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005731 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005732 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005733 done = 1;
5734 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005735
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005736 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005737 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005738 done = 1;
5739 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005740
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005741 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005742 len = exp_replace(trash.area, trash.size,
5743 cur_ptr, exp->replace, pmatch);
5744 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005745 return -1;
5746
Willy Tarreau6e27be12018-08-22 04:46:47 +02005747 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5748
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005749 /* FIXME: if the user adds a newline in the replacement, the
5750 * index will not be recalculated for now, and the new line
5751 * will not be counted as a new header.
5752 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005753
Willy Tarreaufa355d42009-11-29 18:12:29 +01005754 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005755 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005756 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005757 HTTP_MSG_RQMETH,
5758 cur_ptr, cur_end + 1,
5759 NULL, NULL);
5760 if (unlikely(!cur_end))
5761 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005762
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005763 /* we have a full request and we know that we have either a CR
5764 * or an LF at <ptr>.
5765 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005766 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5767 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005768 /* there is no point trying this regex on headers */
5769 return 1;
5770 }
5771 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005772 return done;
5773}
Willy Tarreau97de6242006-12-27 17:18:38 +01005774
Willy Tarreau58f10d72006-12-04 02:26:12 +01005775
Willy Tarreau58f10d72006-12-04 02:26:12 +01005776
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005777/*
Willy Tarreau87b09662015-04-03 00:22:06 +02005778 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005779 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005780 * unparsable request. Since it can manage the switch to another backend, it
5781 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005782 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005783int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005784{
Willy Tarreau192252e2015-04-04 01:47:55 +02005785 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005786 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005787 struct hdr_exp *exp;
5788
5789 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005790 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005791
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005792 /*
5793 * The interleaving of transformations and verdicts
5794 * makes it difficult to decide to continue or stop
5795 * the evaluation.
5796 */
5797
Willy Tarreau6c123b12010-01-28 20:22:06 +01005798 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5799 break;
5800
Willy Tarreau3d300592007-03-18 18:34:41 +01005801 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005802 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005803 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005804 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005805
5806 /* if this filter had a condition, evaluate it now and skip to
5807 * next filter if the condition does not match.
5808 */
5809 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005810 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01005811 ret = acl_pass(ret);
5812 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5813 ret = !ret;
5814
5815 if (!ret)
5816 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005817 }
5818
5819 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005820 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005821 if (unlikely(ret < 0))
5822 return -1;
5823
5824 if (likely(ret == 0)) {
5825 /* The filter did not match the request, it can be
5826 * iterated through all headers.
5827 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01005828 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
5829 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005830 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005831 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005832 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005833}
5834
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005835
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005836/* Delete a value in a header between delimiters <from> and <next> in buffer
5837 * <buf>. The number of characters displaced is returned, and the pointer to
5838 * the first delimiter is updated if required. The function tries as much as
5839 * possible to respect the following principles :
5840 * - replace <from> delimiter by the <next> one unless <from> points to a
5841 * colon, in which case <next> is simply removed
5842 * - set exactly one space character after the new first delimiter, unless
5843 * there are not enough characters in the block being moved to do so.
5844 * - remove unneeded spaces before the previous delimiter and after the new
5845 * one.
5846 *
5847 * It is the caller's responsibility to ensure that :
5848 * - <from> points to a valid delimiter or the colon ;
5849 * - <next> points to a valid delimiter or the final CR/LF ;
5850 * - there are non-space chars before <from> ;
5851 * - there is a CR/LF at or after <next>.
5852 */
Willy Tarreauc5a4fd52018-12-11 11:42:27 +01005853static int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005854{
5855 char *prev = *from;
5856
5857 if (*prev == ':') {
5858 /* We're removing the first value, preserve the colon and add a
5859 * space if possible.
5860 */
Willy Tarreau2235b262016-11-05 15:50:20 +01005861 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005862 next++;
5863 prev++;
5864 if (prev < next)
5865 *prev++ = ' ';
5866
Willy Tarreau2235b262016-11-05 15:50:20 +01005867 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005868 next++;
5869 } else {
5870 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01005871 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005872 prev--;
5873 *from = prev;
5874
5875 /* copy the delimiter and if possible a space if we're
5876 * not at the end of the line.
5877 */
Willy Tarreau2235b262016-11-05 15:50:20 +01005878 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005879 *prev++ = *next++;
5880 if (prev + 1 < next)
5881 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01005882 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005883 next++;
5884 }
5885 }
Willy Tarreaue3128022018-07-12 15:55:34 +02005886 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005887}
5888
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005889/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005890 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005891 * desirable to call it only when needed. This code is quite complex because
5892 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5893 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005894 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005895void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005896{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005897 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005898 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005899 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005900 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005901 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5902 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005903
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005904 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005905 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005906 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005907
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005908 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005909 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005910 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005911
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005912 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005913 hdr_beg = hdr_next;
5914 hdr_end = hdr_beg + cur_hdr->len;
5915 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005916
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005917 /* We have one full header between hdr_beg and hdr_end, and the
5918 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005919 * "Cookie:" headers.
5920 */
5921
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005922 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005923 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005924 old_idx = cur_idx;
5925 continue;
5926 }
5927
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005928 del_from = NULL; /* nothing to be deleted */
5929 preserve_hdr = 0; /* assume we may kill the whole header */
5930
Willy Tarreau58f10d72006-12-04 02:26:12 +01005931 /* Now look for cookies. Conforming to RFC2109, we have to support
5932 * attributes whose name begin with a '$', and associate them with
5933 * the right cookie, if we want to delete this cookie.
5934 * So there are 3 cases for each cookie read :
5935 * 1) it's a special attribute, beginning with a '$' : ignore it.
5936 * 2) it's a server id cookie that we *MAY* want to delete : save
5937 * some pointers on it (last semi-colon, beginning of cookie...)
5938 * 3) it's an application cookie : we *MAY* have to delete a previous
5939 * "special" cookie.
5940 * At the end of loop, if a "special" cookie remains, we may have to
5941 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005942 * *MUST* delete it.
5943 *
5944 * Note: RFC2965 is unclear about the processing of spaces around
5945 * the equal sign in the ATTR=VALUE form. A careful inspection of
5946 * the RFC explicitly allows spaces before it, and not within the
5947 * tokens (attrs or values). An inspection of RFC2109 allows that
5948 * too but section 10.1.3 lets one think that spaces may be allowed
5949 * after the equal sign too, resulting in some (rare) buggy
5950 * implementations trying to do that. So let's do what servers do.
5951 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5952 * allowed quoted strings in values, with any possible character
5953 * after a backslash, including control chars and delimitors, which
5954 * causes parsing to become ambiguous. Browsers also allow spaces
5955 * within values even without quotes.
5956 *
5957 * We have to keep multiple pointers in order to support cookie
5958 * removal at the beginning, middle or end of header without
5959 * corrupting the header. All of these headers are valid :
5960 *
5961 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5962 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5963 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5964 * | | | | | | | | |
5965 * | | | | | | | | hdr_end <--+
5966 * | | | | | | | +--> next
5967 * | | | | | | +----> val_end
5968 * | | | | | +-----------> val_beg
5969 * | | | | +--------------> equal
5970 * | | | +----------------> att_end
5971 * | | +---------------------> att_beg
5972 * | +--------------------------> prev
5973 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005974 */
5975
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005976 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
5977 /* Iterate through all cookies on this line */
5978
5979 /* find att_beg */
5980 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01005981 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005982 att_beg++;
5983
5984 /* find att_end : this is the first character after the last non
5985 * space before the equal. It may be equal to hdr_end.
5986 */
5987 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005988
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005989 while (equal < hdr_end) {
5990 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01005991 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01005992 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005993 continue;
5994 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005995 }
5996
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005997 /* here, <equal> points to '=', a delimitor or the end. <att_end>
5998 * is between <att_beg> and <equal>, both may be identical.
5999 */
6000
6001 /* look for end of cookie if there is an equal sign */
6002 if (equal < hdr_end && *equal == '=') {
6003 /* look for the beginning of the value */
6004 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006005 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006006 val_beg++;
6007
6008 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006009 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006010
6011 /* make val_end point to the first white space or delimitor after the value */
6012 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006013 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006014 val_end--;
6015 } else {
6016 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006017 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006018
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006019 /* We have nothing to do with attributes beginning with '$'. However,
6020 * they will automatically be removed if a header before them is removed,
6021 * since they're supposed to be linked together.
6022 */
6023 if (*att_beg == '$')
6024 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006025
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006026 /* Ignore cookies with no equal sign */
6027 if (equal == next) {
6028 /* This is not our cookie, so we must preserve it. But if we already
6029 * scheduled another cookie for removal, we cannot remove the
6030 * complete header, but we can remove the previous block itself.
6031 */
6032 preserve_hdr = 1;
6033 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006034 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006035 val_end += delta;
6036 next += delta;
6037 hdr_end += delta;
6038 hdr_next += delta;
6039 cur_hdr->len += delta;
6040 http_msg_move_end(&txn->req, delta);
6041 prev = del_from;
6042 del_from = NULL;
6043 }
6044 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006045 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006046
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006047 /* if there are spaces around the equal sign, we need to
6048 * strip them otherwise we'll get trouble for cookie captures,
6049 * or even for rewrites. Since this happens extremely rarely,
6050 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006051 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006052 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6053 int stripped_before = 0;
6054 int stripped_after = 0;
6055
6056 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006057 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006058 equal += stripped_before;
6059 val_beg += stripped_before;
6060 }
6061
6062 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006063 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006064 val_beg += stripped_after;
6065 stripped_before += stripped_after;
6066 }
6067
6068 val_end += stripped_before;
6069 next += stripped_before;
6070 hdr_end += stripped_before;
6071 hdr_next += stripped_before;
6072 cur_hdr->len += stripped_before;
6073 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006074 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006075 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006076
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006077 /* First, let's see if we want to capture this cookie. We check
6078 * that we don't already have a client side cookie, because we
6079 * can only capture one. Also as an optimisation, we ignore
6080 * cookies shorter than the declared name.
6081 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006082 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6083 (val_end - att_beg >= sess->fe->capture_namelen) &&
6084 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006085 int log_len = val_end - att_beg;
6086
Willy Tarreaubafbe012017-11-24 17:34:44 +01006087 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006088 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006089 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006090 if (log_len > sess->fe->capture_len)
6091 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006092 memcpy(txn->cli_cookie, att_beg, log_len);
6093 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006094 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006095 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006096
Willy Tarreaubca99692010-10-06 19:25:55 +02006097 /* Persistence cookies in passive, rewrite or insert mode have the
6098 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006099 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006100 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006101 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006102 * For cookies in prefix mode, the form is :
6103 *
6104 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006105 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006106 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6107 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6108 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006109 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006110
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006111 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6112 * have the server ID between val_beg and delim, and the original cookie between
6113 * delim+1 and val_end. Otherwise, delim==val_end :
6114 *
6115 * Cookie: NAME=SRV; # in all but prefix modes
6116 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6117 * | || || | |+-> next
6118 * | || || | +--> val_end
6119 * | || || +---------> delim
6120 * | || |+------------> val_beg
6121 * | || +-------------> att_end = equal
6122 * | |+-----------------> att_beg
6123 * | +------------------> prev
6124 * +-------------------------> hdr_beg
6125 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006126
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006127 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006128 for (delim = val_beg; delim < val_end; delim++)
6129 if (*delim == COOKIE_DELIM)
6130 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006131 } else {
6132 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006133 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006134 /* Now check if the cookie contains a date field, which would
6135 * appear after a vertical bar ('|') just after the server name
6136 * and before the delimiter.
6137 */
6138 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6139 if (vbar1) {
6140 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006141 * right is the last seen date. It is a base64 encoded
6142 * 30-bit value representing the UNIX date since the
6143 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006144 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006145 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006146 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006147 if (val_end - vbar1 >= 5) {
6148 val = b64tos30(vbar1);
6149 if (val > 0)
6150 txn->cookie_last_date = val << 2;
6151 }
6152 /* look for a second vertical bar */
6153 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6154 if (vbar1 && (val_end - vbar1 > 5)) {
6155 val = b64tos30(vbar1 + 1);
6156 if (val > 0)
6157 txn->cookie_first_date = val << 2;
6158 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006159 }
6160 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006161
Willy Tarreauf64d1412010-10-07 20:06:11 +02006162 /* if the cookie has an expiration date and the proxy wants to check
6163 * it, then we do that now. We first check if the cookie is too old,
6164 * then only if it has expired. We detect strict overflow because the
6165 * time resolution here is not great (4 seconds). Cookies with dates
6166 * in the future are ignored if their offset is beyond one day. This
6167 * allows an admin to fix timezone issues without expiring everyone
6168 * and at the same time avoids keeping unwanted side effects for too
6169 * long.
6170 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006171 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6172 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006173 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006174 txn->flags &= ~TX_CK_MASK;
6175 txn->flags |= TX_CK_OLD;
6176 delim = val_beg; // let's pretend we have not found the cookie
6177 txn->cookie_first_date = 0;
6178 txn->cookie_last_date = 0;
6179 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006180 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6181 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006182 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006183 txn->flags &= ~TX_CK_MASK;
6184 txn->flags |= TX_CK_EXPIRED;
6185 delim = val_beg; // let's pretend we have not found the cookie
6186 txn->cookie_first_date = 0;
6187 txn->cookie_last_date = 0;
6188 }
6189
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006190 /* Here, we'll look for the first running server which supports the cookie.
6191 * This allows to share a same cookie between several servers, for example
6192 * to dedicate backup servers to specific servers only.
6193 * However, to prevent clients from sticking to cookie-less backup server
6194 * when they have incidentely learned an empty cookie, we simply ignore
6195 * empty cookies and mark them as invalid.
6196 * The same behaviour is applied when persistence must be ignored.
6197 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006198 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006199 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006200
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006201 while (srv) {
6202 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6203 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006204 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006205 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006206 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006207 /* we found the server and we can use it */
6208 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006209 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006210 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006211 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006212 break;
6213 } else {
6214 /* we found a server, but it's down,
6215 * mark it as such and go on in case
6216 * another one is available.
6217 */
6218 txn->flags &= ~TX_CK_MASK;
6219 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006220 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006221 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006222 srv = srv->next;
6223 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006224
Willy Tarreauf64d1412010-10-07 20:06:11 +02006225 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006226 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006227 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006228 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006229 txn->flags |= TX_CK_UNUSED;
6230 else
6231 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006232 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006233
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006234 /* depending on the cookie mode, we may have to either :
6235 * - delete the complete cookie if we're in insert+indirect mode, so that
6236 * the server never sees it ;
6237 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlant5ba80252018-11-15 09:25:36 -08006238 * application cookie so that it does not get accidently removed later,
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006239 * if we're in cookie prefix mode
6240 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006241 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006242 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006243
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006244 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006245 val_end += delta;
6246 next += delta;
6247 hdr_end += delta;
6248 hdr_next += delta;
6249 cur_hdr->len += delta;
6250 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006251
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006252 del_from = NULL;
6253 preserve_hdr = 1; /* we want to keep this cookie */
6254 }
6255 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006256 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006257 del_from = prev;
6258 }
6259 } else {
6260 /* This is not our cookie, so we must preserve it. But if we already
6261 * scheduled another cookie for removal, we cannot remove the
6262 * complete header, but we can remove the previous block itself.
6263 */
6264 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006265
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006266 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006267 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006268 if (att_beg >= del_from)
6269 att_beg += delta;
6270 if (att_end >= del_from)
6271 att_end += delta;
6272 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006273 val_end += delta;
6274 next += delta;
6275 hdr_end += delta;
6276 hdr_next += delta;
6277 cur_hdr->len += delta;
6278 http_msg_move_end(&txn->req, delta);
6279 prev = del_from;
6280 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006281 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006282 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006283
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006284 /* continue with next cookie on this header line */
6285 att_beg = next;
6286 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006287
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006288 /* There are no more cookies on this line.
6289 * We may still have one (or several) marked for deletion at the
6290 * end of the line. We must do this now in two ways :
6291 * - if some cookies must be preserved, we only delete from the
6292 * mark to the end of line ;
6293 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006294 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006295 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006296 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006297 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006298 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006299 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006300 cur_hdr->len += delta;
6301 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006302 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006303
6304 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006305 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6306 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006307 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006308 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006309 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006310 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006311 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006312 }
6313
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006314 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006315 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006316 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006317}
6318
6319
Willy Tarreaua15645d2007-03-18 16:22:39 +01006320/* Iterate the same filter through all response headers contained in <rtr>.
6321 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6322 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006323int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006324{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006325 char *cur_ptr, *cur_end, *cur_next;
6326 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006327 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006328 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006329 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006330
6331 last_hdr = 0;
6332
Willy Tarreauf37954d2018-06-15 18:31:02 +02006333 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006334 old_idx = 0;
6335
6336 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006337 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006338 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006339 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006340 (exp->action == ACT_ALLOW ||
6341 exp->action == ACT_DENY))
6342 return 0;
6343
6344 cur_idx = txn->hdr_idx.v[old_idx].next;
6345 if (!cur_idx)
6346 break;
6347
6348 cur_hdr = &txn->hdr_idx.v[cur_idx];
6349 cur_ptr = cur_next;
6350 cur_end = cur_ptr + cur_hdr->len;
6351 cur_next = cur_end + cur_hdr->cr + 1;
6352
6353 /* Now we have one header between cur_ptr and cur_end,
6354 * and the next header starts at cur_next.
6355 */
6356
Willy Tarreau15a53a42015-01-21 13:39:42 +01006357 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006358 switch (exp->action) {
6359 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006360 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006361 last_hdr = 1;
6362 break;
6363
6364 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006365 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006366 last_hdr = 1;
6367 break;
6368
6369 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006370 len = exp_replace(trash.area,
6371 trash.size, cur_ptr,
6372 exp->replace, pmatch);
6373 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006374 return -1;
6375
Willy Tarreau6e27be12018-08-22 04:46:47 +02006376 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6377
Willy Tarreaua15645d2007-03-18 16:22:39 +01006378 /* FIXME: if the user adds a newline in the replacement, the
6379 * index will not be recalculated for now, and the new line
6380 * will not be counted as a new header.
6381 */
6382
6383 cur_end += delta;
6384 cur_next += delta;
6385 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006386 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006387 break;
6388
6389 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006390 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006391 cur_next += delta;
6392
Willy Tarreaufa355d42009-11-29 18:12:29 +01006393 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006394 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6395 txn->hdr_idx.used--;
6396 cur_hdr->len = 0;
6397 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006398 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006399 break;
6400
6401 }
6402 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006403
6404 /* keep the link from this header to next one in case of later
6405 * removal of next header.
6406 */
6407 old_idx = cur_idx;
6408 }
6409 return 0;
6410}
6411
6412
6413/* Apply the filter to the status line in the response buffer <rtr>.
6414 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6415 * or -1 if a replacement resulted in an invalid status line.
6416 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006417int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006418{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006419 char *cur_ptr, *cur_end;
6420 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006421 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006422 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006423
Willy Tarreau3d300592007-03-18 18:34:41 +01006424 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006425 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006426 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006427 (exp->action == ACT_ALLOW ||
6428 exp->action == ACT_DENY))
6429 return 0;
6430 else if (exp->action == ACT_REMOVE)
6431 return 0;
6432
6433 done = 0;
6434
Willy Tarreauf37954d2018-06-15 18:31:02 +02006435 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006436 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006437
6438 /* Now we have the status line between cur_ptr and cur_end */
6439
Willy Tarreau15a53a42015-01-21 13:39:42 +01006440 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006441 switch (exp->action) {
6442 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006443 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006444 done = 1;
6445 break;
6446
6447 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006448 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006449 done = 1;
6450 break;
6451
6452 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006453 len = exp_replace(trash.area, trash.size,
6454 cur_ptr, exp->replace, pmatch);
6455 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006456 return -1;
6457
Willy Tarreau6e27be12018-08-22 04:46:47 +02006458 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6459
Willy Tarreaua15645d2007-03-18 16:22:39 +01006460 /* FIXME: if the user adds a newline in the replacement, the
6461 * index will not be recalculated for now, and the new line
6462 * will not be counted as a new header.
6463 */
6464
Willy Tarreaufa355d42009-11-29 18:12:29 +01006465 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006466 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006467 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006468 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006469 cur_ptr, cur_end + 1,
6470 NULL, NULL);
6471 if (unlikely(!cur_end))
6472 return -1;
6473
6474 /* we have a full respnse and we know that we have either a CR
6475 * or an LF at <ptr>.
6476 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006477 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006478 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006479 /* there is no point trying this regex on headers */
6480 return 1;
6481 }
6482 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006483 return done;
6484}
6485
6486
6487
6488/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006489 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006490 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6491 * unparsable response.
6492 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006493int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006494{
Willy Tarreau192252e2015-04-04 01:47:55 +02006495 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006496 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006497 struct hdr_exp *exp;
6498
6499 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006500 int ret;
6501
6502 /*
6503 * The interleaving of transformations and verdicts
6504 * makes it difficult to decide to continue or stop
6505 * the evaluation.
6506 */
6507
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006508 if (txn->flags & TX_SVDENY)
6509 break;
6510
Willy Tarreau3d300592007-03-18 18:34:41 +01006511 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006512 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6513 exp->action == ACT_PASS)) {
6514 exp = exp->next;
6515 continue;
6516 }
6517
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006518 /* if this filter had a condition, evaluate it now and skip to
6519 * next filter if the condition does not match.
6520 */
6521 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006522 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006523 ret = acl_pass(ret);
6524 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6525 ret = !ret;
6526 if (!ret)
6527 continue;
6528 }
6529
Willy Tarreaua15645d2007-03-18 16:22:39 +01006530 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006531 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006532 if (unlikely(ret < 0))
6533 return -1;
6534
6535 if (likely(ret == 0)) {
6536 /* The filter did not match the response, it can be
6537 * iterated through all headers.
6538 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006539 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6540 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006541 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006542 }
6543 return 0;
6544}
6545
6546
Willy Tarreaua15645d2007-03-18 16:22:39 +01006547/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006548 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006549 * desirable to call it only when needed. This function is also used when we
6550 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006551 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006552void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006553{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006554 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006555 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006556 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006557 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006558 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006559 char *hdr_beg, *hdr_end, *hdr_next;
6560 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006561
Willy Tarreaua15645d2007-03-18 16:22:39 +01006562 /* Iterate through the headers.
6563 * we start with the start line.
6564 */
6565 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006566 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006567
6568 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6569 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006570 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006571
6572 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006573 hdr_beg = hdr_next;
6574 hdr_end = hdr_beg + cur_hdr->len;
6575 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006576
Willy Tarreau24581ba2010-08-31 22:39:35 +02006577 /* We have one full header between hdr_beg and hdr_end, and the
6578 * next header starts at hdr_next. We're only interested in
6579 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006580 */
6581
Willy Tarreau24581ba2010-08-31 22:39:35 +02006582 is_cookie2 = 0;
6583 prev = hdr_beg + 10;
6584 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006585 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006586 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6587 if (!val) {
6588 old_idx = cur_idx;
6589 continue;
6590 }
6591 is_cookie2 = 1;
6592 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006593 }
6594
Willy Tarreau24581ba2010-08-31 22:39:35 +02006595 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6596 * <prev> points to the colon.
6597 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006598 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006599
Willy Tarreau24581ba2010-08-31 22:39:35 +02006600 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6601 * check-cache is enabled) and we are not interested in checking
6602 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006603 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006604 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006605 return;
6606
Willy Tarreau24581ba2010-08-31 22:39:35 +02006607 /* OK so now we know we have to process this response cookie.
6608 * The format of the Set-Cookie header is slightly different
6609 * from the format of the Cookie header in that it does not
6610 * support the comma as a cookie delimiter (thus the header
6611 * cannot be folded) because the Expires attribute described in
6612 * the original Netscape's spec may contain an unquoted date
6613 * with a comma inside. We have to live with this because
6614 * many browsers don't support Max-Age and some browsers don't
6615 * support quoted strings. However the Set-Cookie2 header is
6616 * clean.
6617 *
6618 * We have to keep multiple pointers in order to support cookie
6619 * removal at the beginning, middle or end of header without
6620 * corrupting the header (in case of set-cookie2). A special
6621 * pointer, <scav> points to the beginning of the set-cookie-av
6622 * fields after the first semi-colon. The <next> pointer points
6623 * either to the end of line (set-cookie) or next unquoted comma
6624 * (set-cookie2). All of these headers are valid :
6625 *
6626 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6627 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6628 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6629 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6630 * | | | | | | | | | |
6631 * | | | | | | | | +-> next hdr_end <--+
6632 * | | | | | | | +------------> scav
6633 * | | | | | | +--------------> val_end
6634 * | | | | | +--------------------> val_beg
6635 * | | | | +----------------------> equal
6636 * | | | +------------------------> att_end
6637 * | | +----------------------------> att_beg
6638 * | +------------------------------> prev
6639 * +-----------------------------------------> hdr_beg
6640 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006641
Willy Tarreau24581ba2010-08-31 22:39:35 +02006642 for (; prev < hdr_end; prev = next) {
6643 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006644
Willy Tarreau24581ba2010-08-31 22:39:35 +02006645 /* find att_beg */
6646 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006647 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006648 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006649
Willy Tarreau24581ba2010-08-31 22:39:35 +02006650 /* find att_end : this is the first character after the last non
6651 * space before the equal. It may be equal to hdr_end.
6652 */
6653 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006654
Willy Tarreau24581ba2010-08-31 22:39:35 +02006655 while (equal < hdr_end) {
6656 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6657 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006658 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006659 continue;
6660 att_end = equal;
6661 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006662
Willy Tarreau24581ba2010-08-31 22:39:35 +02006663 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6664 * is between <att_beg> and <equal>, both may be identical.
6665 */
6666
6667 /* look for end of cookie if there is an equal sign */
6668 if (equal < hdr_end && *equal == '=') {
6669 /* look for the beginning of the value */
6670 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006671 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006672 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006673
Willy Tarreau24581ba2010-08-31 22:39:35 +02006674 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006675 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006676
6677 /* make val_end point to the first white space or delimitor after the value */
6678 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006679 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006680 val_end--;
6681 } else {
6682 /* <equal> points to next comma, semi-colon or EOL */
6683 val_beg = val_end = next = equal;
6684 }
6685
6686 if (next < hdr_end) {
6687 /* Set-Cookie2 supports multiple cookies, and <next> points to
6688 * a colon or semi-colon before the end. So skip all attr-value
6689 * pairs and look for the next comma. For Set-Cookie, since
6690 * commas are permitted in values, skip to the end.
6691 */
6692 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02006693 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006694 else
6695 next = hdr_end;
6696 }
6697
6698 /* Now everything is as on the diagram above */
6699
6700 /* Ignore cookies with no equal sign */
6701 if (equal == val_end)
6702 continue;
6703
6704 /* If there are spaces around the equal sign, we need to
6705 * strip them otherwise we'll get trouble for cookie captures,
6706 * or even for rewrites. Since this happens extremely rarely,
6707 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006708 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006709 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6710 int stripped_before = 0;
6711 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006712
Willy Tarreau24581ba2010-08-31 22:39:35 +02006713 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006714 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006715 equal += stripped_before;
6716 val_beg += stripped_before;
6717 }
6718
6719 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006720 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006721 val_beg += stripped_after;
6722 stripped_before += stripped_after;
6723 }
6724
6725 val_end += stripped_before;
6726 next += stripped_before;
6727 hdr_end += stripped_before;
6728 hdr_next += stripped_before;
6729 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006730 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006731 }
6732
6733 /* First, let's see if we want to capture this cookie. We check
6734 * that we don't already have a server side cookie, because we
6735 * can only capture one. Also as an optimisation, we ignore
6736 * cookies shorter than the declared name.
6737 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006738 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006739 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006740 (val_end - att_beg >= sess->fe->capture_namelen) &&
6741 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006742 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006743 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006744 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01006745 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006746 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006747 if (log_len > sess->fe->capture_len)
6748 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01006749 memcpy(txn->srv_cookie, att_beg, log_len);
6750 txn->srv_cookie[log_len] = 0;
6751 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006752 }
6753
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006754 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006755 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006756 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006757 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6758 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006759 /* assume passive cookie by default */
6760 txn->flags &= ~TX_SCK_MASK;
6761 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006762
6763 /* If the cookie is in insert mode on a known server, we'll delete
6764 * this occurrence because we'll insert another one later.
6765 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006766 * a direct access.
6767 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006768 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006769 /* The "preserve" flag was set, we don't want to touch the
6770 * server's cookie.
6771 */
6772 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006773 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006774 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006775 /* this cookie must be deleted */
6776 if (*prev == ':' && next == hdr_end) {
6777 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006778 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006779 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6780 txn->hdr_idx.used--;
6781 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006782 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006783 hdr_next += delta;
6784 http_msg_move_end(&txn->rsp, delta);
6785 /* note: while both invalid now, <next> and <hdr_end>
6786 * are still equal, so the for() will stop as expected.
6787 */
6788 } else {
6789 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006790 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006791 next = prev;
6792 hdr_end += delta;
6793 hdr_next += delta;
6794 cur_hdr->len += delta;
6795 http_msg_move_end(&txn->rsp, delta);
6796 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006797 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006798 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006799 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006800 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006801 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006802 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006803 * with this server since we know it.
6804 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006805 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006806 next += delta;
6807 hdr_end += delta;
6808 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006809 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006810 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006811
Willy Tarreauf1348312010-10-07 15:54:11 +02006812 txn->flags &= ~TX_SCK_MASK;
6813 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006814 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006815 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006816 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006817 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006818 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006819 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006820 next += delta;
6821 hdr_end += delta;
6822 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006823 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006824 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006825
Willy Tarreau827aee92011-03-10 16:55:02 +01006826 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006827 txn->flags &= ~TX_SCK_MASK;
6828 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006829 }
6830 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006831 /* that's done for this cookie, check the next one on the same
6832 * line when next != hdr_end (only if is_cookie2).
6833 */
6834 }
6835 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006836 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006837 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006838}
6839
6840
Willy Tarreaua15645d2007-03-18 16:22:39 +01006841/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006842 * Parses the Cache-Control and Pragma request header fields to determine if
6843 * the request may be served from the cache and/or if it is cacheable. Updates
6844 * s->txn->flags.
6845 */
6846void check_request_for_cacheability(struct stream *s, struct channel *chn)
6847{
6848 struct http_txn *txn = s->txn;
6849 char *p1, *p2;
6850 char *cur_ptr, *cur_end, *cur_next;
6851 int pragma_found;
6852 int cc_found;
6853 int cur_idx;
6854
Christopher Faulet25a02f62018-10-24 12:00:25 +02006855 if (IS_HTX_STRM(s))
6856 return htx_check_request_for_cacheability(s, chn);
6857
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006858 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
6859 return; /* nothing more to do here */
6860
6861 cur_idx = 0;
6862 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006863 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006864
6865 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6866 struct hdr_idx_elem *cur_hdr;
6867 int val;
6868
6869 cur_hdr = &txn->hdr_idx.v[cur_idx];
6870 cur_ptr = cur_next;
6871 cur_end = cur_ptr + cur_hdr->len;
6872 cur_next = cur_end + cur_hdr->cr + 1;
6873
6874 /* We have one full header between cur_ptr and cur_end, and the
6875 * next header starts at cur_next.
6876 */
6877
6878 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6879 if (val) {
6880 if ((cur_end - (cur_ptr + val) >= 8) &&
6881 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6882 pragma_found = 1;
6883 continue;
6884 }
6885 }
6886
William Lallemand8a16fe02018-05-22 11:04:33 +02006887 /* Don't use the cache and don't try to store if we found the
6888 * Authorization header */
6889 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
6890 if (val) {
6891 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6892 txn->flags |= TX_CACHE_IGNORE;
6893 continue;
6894 }
6895
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006896 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6897 if (!val)
6898 continue;
6899
6900 /* OK, right now we know we have a cache-control header at cur_ptr */
6901 cc_found = 1;
6902 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
6903
6904 if (p1 >= cur_end) /* no more info */
6905 continue;
6906
6907 /* p1 is at the beginning of the value */
6908 p2 = p1;
6909 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
6910 p2++;
6911
6912 /* we have a complete value between p1 and p2. We don't check the
6913 * values after max-age, max-stale nor min-fresh, we simply don't
6914 * use the cache when they're specified.
6915 */
6916 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
6917 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
6918 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
6919 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
6920 txn->flags |= TX_CACHE_IGNORE;
6921 continue;
6922 }
6923
6924 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
6925 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6926 continue;
6927 }
6928 }
6929
6930 /* RFC7234#5.4:
6931 * When the Cache-Control header field is also present and
6932 * understood in a request, Pragma is ignored.
6933 * When the Cache-Control header field is not present in a
6934 * request, caches MUST consider the no-cache request
6935 * pragma-directive as having the same effect as if
6936 * "Cache-Control: no-cache" were present.
6937 */
6938 if (!cc_found && pragma_found)
6939 txn->flags |= TX_CACHE_IGNORE;
6940}
6941
6942/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01006943 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006944 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006945void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006946{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006947 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006948 char *p1, *p2;
6949
6950 char *cur_ptr, *cur_end, *cur_next;
6951 int cur_idx;
6952
Christopher Faulet25a02f62018-10-24 12:00:25 +02006953
6954 if (IS_HTX_STRM(s))
6955 return htx_check_response_for_cacheability(s, rtr);
6956
Willy Tarreau12b32f22017-12-21 16:08:09 +01006957 if (txn->status < 200) {
6958 /* do not try to cache interim responses! */
6959 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006960 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01006961 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006962
6963 /* Iterate through the headers.
6964 * we start with the start line.
6965 */
6966 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006967 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006968
6969 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6970 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006971 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006972
6973 cur_hdr = &txn->hdr_idx.v[cur_idx];
6974 cur_ptr = cur_next;
6975 cur_end = cur_ptr + cur_hdr->len;
6976 cur_next = cur_end + cur_hdr->cr + 1;
6977
6978 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01006979 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006980 */
6981
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006982 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6983 if (val) {
6984 if ((cur_end - (cur_ptr + val) >= 8) &&
6985 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6986 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6987 return;
6988 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006989 }
6990
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006991 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6992 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006993 continue;
6994
6995 /* OK, right now we know we have a cache-control header at cur_ptr */
6996
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006997 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006998
6999 if (p1 >= cur_end) /* no more info */
7000 continue;
7001
7002 /* p1 is at the beginning of the value */
7003 p2 = p1;
7004
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007005 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007006 p2++;
7007
7008 /* we have a complete value between p1 and p2 */
7009 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007010 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7011 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7012 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7013 continue;
7014 }
7015
Willy Tarreaua15645d2007-03-18 16:22:39 +01007016 /* we have something of the form no-cache="set-cookie" */
7017 if ((cur_end - p1 >= 21) &&
7018 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7019 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007020 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007021 continue;
7022 }
7023
7024 /* OK, so we know that either p2 points to the end of string or to a comma */
7025 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007026 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007027 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007028 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007029 return;
7030 }
7031
7032 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007033 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007034 continue;
7035 }
7036 }
7037}
7038
Willy Tarreau58f10d72006-12-04 02:26:12 +01007039
Willy Tarreaub2513902006-12-17 14:52:38 +01007040/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007041 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007042 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007043 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007044 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007045 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007046 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007047 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007048 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007049int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007050{
7051 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007052 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007053 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007054
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007055 if (!uri_auth)
7056 return 0;
7057
Cyril Bonté70be45d2010-10-12 00:14:35 +02007058 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007059 return 0;
7060
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007061 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007062 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007063 return 0;
7064
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007065 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007066 return 0;
7067
Willy Tarreaub2513902006-12-17 14:52:38 +01007068 return 1;
7069}
7070
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007071/* Append the description of what is present in error snapshot <es> into <out>.
7072 * The description must be small enough to always fit in a trash. The output
7073 * buffer may be the trash so the trash must not be used inside this function.
7074 */
7075void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7076{
Christopher Fauleted26fb82018-11-29 22:53:30 +01007077 chunk_appendf(out,
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007078 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7079 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7080 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7081 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7082 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7083 es->ctx.http.m_flags, es->ctx.http.m_clen,
7084 es->ctx.http.m_blen, es->ctx.http.b_flags);
7085}
7086
Willy Tarreau4076a152009-04-02 15:18:36 +02007087/*
7088 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007089 * By default it tries to report the error position as msg->err_pos. However if
7090 * this one is not set, it will then report msg->next, which is the last known
7091 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007092 * displays buffers as a contiguous area starting at buf->p. The direction is
7093 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007094 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007095void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007096 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007097 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007098{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007099 union error_snapshot_ctx ctx;
7100 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007101
Willy Tarreauef3ca732018-09-07 15:47:35 +02007102 /* http-specific part now */
7103 ctx.http.sid = s->uniq_id;
7104 ctx.http.state = state;
7105 ctx.http.b_flags = msg->chn->flags;
7106 ctx.http.s_flags = s->flags;
7107 ctx.http.t_flags = s->txn->flags;
7108 ctx.http.m_flags = msg->flags;
7109 ctx.http.m_clen = msg->chunk_len;
7110 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007111
Willy Tarreauef3ca732018-09-07 15:47:35 +02007112 ofs = msg->chn->total - ci_data(msg->chn);
7113 if (ofs < 0)
7114 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007115
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007116 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007117 other_end, s->target,
7118 strm_sess(s), &msg->chn->buf,
7119 ofs, co_data(msg->chn),
7120 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7121 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007122}
Willy Tarreaub2513902006-12-17 14:52:38 +01007123
Willy Tarreaubaaee002006-06-26 02:48:02 +02007124/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007125 * Print a debug line with a header. Always stop at the first CR or LF char,
7126 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7127 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007128 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007129void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007130{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007131 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007132 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007133
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007134 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007135 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007136 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007137 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 +02007138
7139 for (max = 0; start + max < end; max++)
7140 if (start[max] == '\r' || start[max] == '\n')
7141 break;
7142
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007143 UBOUND(max, trash.size - trash.data - 3);
7144 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7145 trash.area[trash.data++] = '\n';
7146 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007147}
7148
Willy Tarreaueee5b512015-04-03 23:46:31 +02007149
7150/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7151 * The hdr_idx is allocated as well. In case of allocation failure, everything
7152 * allocated is freed and NULL is returned. Otherwise the new transaction is
7153 * assigned to the stream and returned.
7154 */
7155struct http_txn *http_alloc_txn(struct stream *s)
7156{
7157 struct http_txn *txn = s->txn;
7158
7159 if (txn)
7160 return txn;
7161
Willy Tarreaubafbe012017-11-24 17:34:44 +01007162 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007163 if (!txn)
7164 return txn;
7165
7166 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007167 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007168 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007169 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007170 return NULL;
7171 }
7172
7173 s->txn = txn;
7174 return txn;
7175}
7176
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007177void http_txn_reset_req(struct http_txn *txn)
7178{
7179 txn->req.flags = 0;
7180 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7181 txn->req.next = 0;
7182 txn->req.chunk_len = 0LL;
7183 txn->req.body_len = 0LL;
7184 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7185}
7186
7187void http_txn_reset_res(struct http_txn *txn)
7188{
7189 txn->rsp.flags = 0;
7190 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7191 txn->rsp.next = 0;
7192 txn->rsp.chunk_len = 0LL;
7193 txn->rsp.body_len = 0LL;
7194 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7195}
7196
Willy Tarreau0937bc42009-12-22 15:03:09 +01007197/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007198 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007199 * the required fields are properly allocated and that we only need to (re)init
7200 * them. This should be used before processing any new request.
7201 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007202void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007203{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007204 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007205 struct proxy *fe = strm_fe(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02007206 struct conn_stream *cs = objt_cs(s->si[0].end);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007207
Christopher Fauletf2824e62018-10-01 12:12:37 +02007208 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
7209 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
7210 : 0);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007211 txn->status = -1;
Willy Tarreauc9036c02019-01-11 19:38:25 +01007212 *(unsigned int *)txn->cache_hash = 0;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007213
Willy Tarreauf64d1412010-10-07 20:06:11 +02007214 txn->cookie_first_date = 0;
7215 txn->cookie_last_date = 0;
7216
Willy Tarreaueee5b512015-04-03 23:46:31 +02007217 txn->srv_cookie = NULL;
7218 txn->cli_cookie = NULL;
7219 txn->uri = NULL;
7220
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007221 http_txn_reset_req(txn);
7222 http_txn_reset_res(txn);
7223
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007224 txn->req.chn = &s->req;
7225 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007226
7227 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007228
7229 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7230 if (fe->options2 & PR_O2_REQBUG_OK)
7231 txn->req.err_pos = -1; /* let buggy requests pass */
7232
Willy Tarreau0937bc42009-12-22 15:03:09 +01007233 if (txn->hdr_idx.v)
7234 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007235
7236 vars_init(&s->vars_txn, SCOPE_TXN);
7237 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007238}
7239
7240/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007241void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007242{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007243 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007244 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007245
7246 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007247 pool_free(pool_head_requri, txn->uri);
7248 pool_free(pool_head_capture, txn->cli_cookie);
7249 pool_free(pool_head_capture, txn->srv_cookie);
7250 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007251
William Lallemanda73203e2012-03-12 12:48:57 +01007252 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007253 txn->uri = NULL;
7254 txn->srv_cookie = NULL;
7255 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007256
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007257 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007258 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007259 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007260 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007261 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007262 }
7263
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007264 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007265 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007266 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007267 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007268 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007269 }
7270
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007271 if (!LIST_ISEMPTY(&s->vars_txn.head))
7272 vars_prune(&s->vars_txn, s->sess, s);
7273 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7274 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007275}
7276
7277/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007278void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007279{
7280 http_end_txn(s);
7281 http_init_txn(s);
7282
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007283 /* reinitialise the current rule list pointer to NULL. We are sure that
7284 * any rulelist match the NULL pointer.
7285 */
7286 s->current_rule_list = NULL;
7287
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007288 s->be = strm_fe(s);
7289 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007290 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007291 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007292 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007293 /* re-init store persistence */
7294 s->store_count = 0;
Olivier Houcharda798bf52019-03-08 18:52:00 +01007295 s->uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007296
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007297 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007298
Willy Tarreau739cfba2010-01-25 23:11:14 +01007299 /* We must trim any excess data from the response buffer, because we
7300 * may have blocked an invalid response from a server that we don't
Joseph Herlant5ba80252018-11-15 09:25:36 -08007301 * want to accidently forward once we disable the analysers, nor do
Willy Tarreau739cfba2010-01-25 23:11:14 +01007302 * we want those data to come along with next response. A typical
7303 * example of such data would be from a buggy server responding to
7304 * a HEAD with some data, or sending more than the advertised
7305 * content-length.
7306 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007307 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007308 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007309
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007310 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007311 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007312
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007313 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007314 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007315
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007316 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007317 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007318
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007319 s->req.rex = TICK_ETERNITY;
7320 s->req.wex = TICK_ETERNITY;
7321 s->req.analyse_exp = TICK_ETERNITY;
7322 s->res.rex = TICK_ETERNITY;
7323 s->res.wex = TICK_ETERNITY;
7324 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007325 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007326}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007327
Willy Tarreau79e57332018-10-02 16:01:16 +02007328/* This function executes one of the set-{method,path,query,uri} actions. It
7329 * takes the string from the variable 'replace' with length 'len', then modifies
7330 * the relevant part of the request line accordingly. Then it updates various
7331 * pointers to the next elements which were moved, and the total buffer length.
7332 * It finds the action to be performed in p[2], previously filled by function
7333 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7334 * error, though this can be revisited when this code is finally exploited.
7335 *
7336 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7337 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007338 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007339 * In query string case, the mark question '?' must be set at the start of the
7340 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007341 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007342int http_replace_req_line(int action, const char *replace, int len,
7343 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007344{
Willy Tarreau79e57332018-10-02 16:01:16 +02007345 struct http_txn *txn = s->txn;
7346 char *cur_ptr, *cur_end;
7347 int offset = 0;
7348 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007349
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007350 if (IS_HTX_STRM(s))
7351 return htx_req_replace_stline(action, replace, len, px, s);
7352
Willy Tarreau79e57332018-10-02 16:01:16 +02007353 switch (action) {
7354 case 0: // method
7355 cur_ptr = ci_head(&s->req);
7356 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007357
Willy Tarreau79e57332018-10-02 16:01:16 +02007358 /* adjust req line offsets and lengths */
7359 delta = len - offset - (cur_end - cur_ptr);
7360 txn->req.sl.rq.m_l += delta;
7361 txn->req.sl.rq.u += delta;
7362 txn->req.sl.rq.v += delta;
7363 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007364
Willy Tarreau79e57332018-10-02 16:01:16 +02007365 case 1: // path
7366 cur_ptr = http_txn_get_path(txn);
7367 if (!cur_ptr)
7368 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007369
Willy Tarreau79e57332018-10-02 16:01:16 +02007370 cur_end = cur_ptr;
7371 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7372 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007373
Willy Tarreau79e57332018-10-02 16:01:16 +02007374 /* adjust req line offsets and lengths */
7375 delta = len - offset - (cur_end - cur_ptr);
7376 txn->req.sl.rq.u_l += delta;
7377 txn->req.sl.rq.v += delta;
7378 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007379
Willy Tarreau79e57332018-10-02 16:01:16 +02007380 case 2: // query
7381 offset = 1;
7382 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7383 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7384 while (cur_ptr < cur_end && *cur_ptr != '?')
7385 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007386
Willy Tarreau79e57332018-10-02 16:01:16 +02007387 /* skip the question mark or indicate that we must insert it
7388 * (but only if the format string is not empty then).
7389 */
7390 if (cur_ptr < cur_end)
7391 cur_ptr++;
7392 else if (len > 1)
7393 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007394
Willy Tarreau79e57332018-10-02 16:01:16 +02007395 /* adjust req line offsets and lengths */
7396 delta = len - offset - (cur_end - cur_ptr);
7397 txn->req.sl.rq.u_l += delta;
7398 txn->req.sl.rq.v += delta;
7399 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007400
Willy Tarreau79e57332018-10-02 16:01:16 +02007401 case 3: // uri
7402 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7403 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007404
Willy Tarreau79e57332018-10-02 16:01:16 +02007405 /* adjust req line offsets and lengths */
7406 delta = len - offset - (cur_end - cur_ptr);
7407 txn->req.sl.rq.u_l += delta;
7408 txn->req.sl.rq.v += delta;
7409 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007410
Willy Tarreau79e57332018-10-02 16:01:16 +02007411 default:
7412 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007413 }
7414
Willy Tarreau79e57332018-10-02 16:01:16 +02007415 /* commit changes and adjust end of message */
7416 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7417 txn->req.sl.rq.l += delta;
7418 txn->hdr_idx.v[0].len += delta;
7419 http_msg_move_end(&txn->req, delta);
7420 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007421}
7422
Willy Tarreau79e57332018-10-02 16:01:16 +02007423/* This function replace the HTTP status code and the associated message. The
7424 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007425 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007426void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007427{
Willy Tarreau79e57332018-10-02 16:01:16 +02007428 struct http_txn *txn = s->txn;
7429 char *cur_ptr, *cur_end;
7430 int delta;
7431 char *res;
7432 int c_l;
7433 const char *msg = reason;
7434 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007435
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007436 if (IS_HTX_STRM(s))
7437 return htx_res_set_status(status, reason, s);
7438
Willy Tarreau79e57332018-10-02 16:01:16 +02007439 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007440
Willy Tarreau79e57332018-10-02 16:01:16 +02007441 res = ultoa_o(status, trash.area, trash.size);
7442 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007443
Willy Tarreau79e57332018-10-02 16:01:16 +02007444 trash.area[c_l] = ' ';
7445 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007446
Willy Tarreau79e57332018-10-02 16:01:16 +02007447 /* Do we have a custom reason format string? */
7448 if (msg == NULL)
7449 msg = http_get_reason(status);
7450 msg_len = strlen(msg);
7451 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7452 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007453
Willy Tarreau79e57332018-10-02 16:01:16 +02007454 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7455 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007456
Willy Tarreau79e57332018-10-02 16:01:16 +02007457 /* commit changes and adjust message */
7458 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7459 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007460
Willy Tarreau79e57332018-10-02 16:01:16 +02007461 /* adjust res line offsets and lengths */
7462 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7463 txn->rsp.sl.st.c_l = c_l;
7464 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007465
Willy Tarreau79e57332018-10-02 16:01:16 +02007466 delta = trash.data - (cur_end - cur_ptr);
7467 txn->rsp.sl.st.l += delta;
7468 txn->hdr_idx.v[0].len += delta;
7469 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007470}
7471
Willy Tarreau58f10d72006-12-04 02:26:12 +01007472/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007473 * Local variables:
7474 * c-indent-level: 8
7475 * c-basic-offset: 8
7476 * End:
7477 */