blob: 0a91091b25403e552b1b79f281ab8e14e256b839 [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>
William Lallemand9ed62032016-11-21 17:49:11 +010056#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020058#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020059#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010061#include <proto/hdr_idx.h>
Patrick Hemmere3faf022018-08-22 10:02:00 -040062#include <proto/hlua.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010063#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020064#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020065#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010066#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020067#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020068#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010069#include <proto/server.h>
Olivier Houchard985f1392018-11-30 17:31:52 +010070#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020071#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010072#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020073#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020074#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020075#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020076
Willy Tarreau2d3d94c2008-11-30 20:20:08 +010077/* This function handles a server error at the stream interface level. The
78 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +010079 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +010080 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +010081 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +020082 */
Willy Tarreau87b09662015-04-03 00:22:06 +020083static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau83061a82018-07-13 11:56:34 +020084 int err, int finst, const struct buffer *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +020085{
Christopher Faulet9768c262018-10-22 09:34:31 +020086 if (IS_HTX_STRM(s))
87 return htx_server_error(s, si, err, finst, msg);
88
Willy Tarreau2019f952017-03-14 11:07:31 +010089 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +010090 channel_auto_read(si_oc(si));
91 channel_abort(si_oc(si));
92 channel_auto_close(si_oc(si));
93 channel_erase(si_oc(si));
94 channel_auto_close(si_ic(si));
95 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +020096 if (msg)
Willy Tarreau843b7cb2018-07-13 10:54:26 +020097 co_inject(si_ic(si), msg->area, msg->data);
Willy Tarreaue7dff022015-04-03 01:14:29 +020098 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +020099 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200100 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200101 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102}
103
Willy Tarreau87b09662015-04-03 00:22:06 +0200104/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100105 * and message.
106 */
107
Willy Tarreau83061a82018-07-13 11:56:34 +0200108struct buffer *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100109{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200110 const int msgnum = http_get_status_idx(s->txn->status);
111
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100112 if (IS_HTX_STRM(s))
113 return htx_error_message(s);
114
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200115 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200116 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200117 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200118 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100119 else
120 return &http_err_chunks[msgnum];
121}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100123void
Willy Tarreau83061a82018-07-13 11:56:34 +0200124http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100125{
Christopher Faulet9768c262018-10-22 09:34:31 +0200126 if (IS_HTX_STRM(s))
127 return htx_reply_and_close(s, status, msg);
128
Christopher Fauletd7c91962015-04-30 11:48:27 +0200129 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100130 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100131 si_retnclose(&s->si[0], msg);
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100132}
133
Willy Tarreau21d2af32008-02-14 20:25:24 +0100134/* Parse the URI from the given transaction (which is assumed to be in request
135 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
136 * It is returned otherwise.
137 */
Willy Tarreau6b952c82018-09-10 17:45:34 +0200138char *http_txn_get_path(const struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100139{
Willy Tarreau6b952c82018-09-10 17:45:34 +0200140 struct ist ret;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100141
Willy Tarreau9d9ccdb2018-10-28 20:13:12 +0100142 if (!txn->req.chn->buf.size)
143 return NULL;
144
Willy Tarreau6b952c82018-09-10 17:45:34 +0200145 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 +0100146
Willy Tarreau6b952c82018-09-10 17:45:34 +0200147 return ret.ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +0100148}
149
Willy Tarreau71241ab2012-12-27 11:30:54 +0100150/* Returns a 302 for a redirectable request that reaches a server working in
151 * in redirect mode. This may only be called just after the stream interface
152 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
153 * follow normal proxy processing. NOTE: this function is designed to support
154 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100155 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200156void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100157{
158 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100159 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100160 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200161 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100162
Christopher Fauletfefc73d2018-10-24 21:18:04 +0200163 if (IS_HTX_STRM(s))
164 return htx_perform_server_redirect(s, si);
165
Willy Tarreauefb453c2008-10-26 20:49:47 +0100166 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200167 trash.data = strlen(HTTP_302);
168 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100169
Willy Tarreaub05e48a2018-09-20 11:12:58 +0200170 srv = __objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100171
Willy Tarreauefb453c2008-10-26 20:49:47 +0100172 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200173 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100174 return;
175
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100176 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100177 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200178 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
179 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100180 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100181
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200182 /* 3: add the request URI. Since it was already forwarded, we need
183 * to temporarily rewind the buffer.
184 */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200185 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200186 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200187
Willy Tarreau6b952c82018-09-10 17:45:34 +0200188 path = http_txn_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200189 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 +0200190
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200191 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200192
Willy Tarreauefb453c2008-10-26 20:49:47 +0100193 if (!path)
194 return;
195
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200196 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100197 return;
198
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200199 memcpy(trash.area + trash.data, path, len);
200 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100201
202 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200203 memcpy(trash.area + trash.data,
204 "\r\nProxy-Connection: close\r\n\r\n", 29);
205 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100206 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200207 memcpy(trash.area + trash.data,
208 "\r\nConnection: close\r\n\r\n", 23);
209 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100210 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100211
212 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200213 si_shutr(si);
214 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100215 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100216 si->state = SI_ST_CLO;
217
218 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +0100219 txn->status = 302;
220 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100221
222 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100223 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500224 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100225}
226
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100227/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100228 * that the server side is closed. Note that err_type is actually a
229 * bitmask, where almost only aborts may be cumulated with other
230 * values. We consider that aborted operations are more important
231 * than timeouts or errors due to the fact that nobody else in the
232 * logs might explain incomplete retries. All others should avoid
233 * being cumulated. It should normally not be possible to have multiple
234 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100235 * Note that connection errors appearing on the second request of a keep-alive
236 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100237 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200238void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100239{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100240 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100241
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200242 /* set s->txn->status for http_error_message(s) */
243 s->txn->status = 503;
244
Willy Tarreauefb453c2008-10-26 20:49:47 +0100245 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200246 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100247 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100248 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200249 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100250 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200251 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100252 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200253 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100254 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100255 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200256 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100257 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100258 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200259 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100260 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200261 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100262 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200263 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100264 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200265 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200266 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200267 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100268 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200269 http_error_message(s));
270 else { /* SI_ET_CONN_OTHER and others */
271 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200272 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100273 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200274 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100275}
276
Willy Tarreau42250582007-04-01 01:30:43 +0200277extern const char sess_term_cond[8];
278extern const char sess_fin_state[8];
279extern const char *monthname[12];
Willy Tarreau8ceae722018-11-26 11:58:30 +0100280
281DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
282DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
283
284struct pool_head *pool_head_requri = NULL;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100285struct pool_head *pool_head_capture = NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100286
Willy Tarreau117f59e2007-03-04 18:17:17 +0100287/*
288 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +0200289 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +0100290 */
Christopher Faulet10079f52018-10-03 15:17:28 +0200291void http_capture_headers(char *som, struct hdr_idx *idx,
292 char **cap, struct cap_hdr *cap_hdr)
Willy Tarreau117f59e2007-03-04 18:17:17 +0100293{
294 char *eol, *sol, *col, *sov;
295 int cur_idx;
296 struct cap_hdr *h;
297 int len;
298
299 sol = som + hdr_idx_first_pos(idx);
300 cur_idx = hdr_idx_first_idx(idx);
301
302 while (cur_idx) {
303 eol = sol + idx->v[cur_idx].len;
304
305 col = sol;
306 while (col < eol && *col != ':')
307 col++;
308
309 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100310 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +0100311 sov++;
312
313 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +0200314 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +0100315 (strncasecmp(sol, h->name, h->namelen) == 0)) {
316 if (cap[h->index] == NULL)
317 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +0100318 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100319
320 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100321 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +0100322 continue;
323 }
324
325 len = eol - sov;
326 if (len > h->len)
327 len = h->len;
328
329 memcpy(cap[h->index], sov, len);
330 cap[h->index][len]=0;
331 }
332 }
333 sol = eol + idx->v[cur_idx].cr + 1;
334 cur_idx = idx->v[cur_idx].next;
335 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100336}
337
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200338/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
339 * conversion succeeded, 0 in case of error. If the request was already 1.X,
340 * nothing is done and 1 is returned.
341 */
Willy Tarreau79e57332018-10-02 16:01:16 +0200342int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200343{
344 int delta;
345 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100346 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200347
348 if (msg->sl.rq.v_l != 0)
349 return 1;
350
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300351 /* RFC 1945 allows only GET for HTTP/0.9 requests */
352 if (txn->meth != HTTP_METH_GET)
353 return 0;
354
Willy Tarreauf37954d2018-06-15 18:31:02 +0200355 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200356
357 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300358 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
359 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200360 }
361 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200362 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +0100363 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200364 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200365 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200366 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +0200367 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200368 NULL, NULL);
369 if (unlikely(!cur_end))
370 return 0;
371
372 /* we have a full HTTP/1.0 request now and we know that
373 * we have either a CR or an LF at <ptr>.
374 */
375 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
376 return 1;
377}
378
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100379/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100380 * and "keep-alive" values. If we already know that some headers may safely
381 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100382 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
383 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +0100384 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100385 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
386 * found, and TX_CON_*_SET is adjusted depending on what is left so only
387 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100388 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +0100389 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100390void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +0100391{
Willy Tarreau5b154472009-12-21 20:11:07 +0100392 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100393 const char *hdr_val = "Connection";
394 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +0100395
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100396 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +0100397 return;
398
Willy Tarreau88d349d2010-01-25 12:15:43 +0100399 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
400 hdr_val = "Proxy-Connection";
401 hdr_len = 16;
402 }
403
Willy Tarreau5b154472009-12-21 20:11:07 +0100404 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100405 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200406 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100407 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
408 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100409 if (to_del & 2)
410 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100411 else
412 txn->flags |= TX_CON_KAL_SET;
413 }
414 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
415 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100416 if (to_del & 1)
417 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100418 else
419 txn->flags |= TX_CON_CLO_SET;
420 }
Willy Tarreau50fc7772012-11-11 22:19:57 +0100421 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
422 txn->flags |= TX_HDR_CONN_UPG;
423 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100424 }
425
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100426 txn->flags |= TX_HDR_CONN_PRS;
427 return;
428}
Willy Tarreau5b154472009-12-21 20:11:07 +0100429
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100430/* Apply desired changes on the Connection: header. Values may be removed and/or
431 * added depending on the <wanted> flags, which are exclusively composed of
432 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
433 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
434 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100435void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100436{
437 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100438 const char *hdr_val = "Connection";
439 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100440
441 ctx.idx = 0;
442
Willy Tarreau88d349d2010-01-25 12:15:43 +0100443
444 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
445 hdr_val = "Proxy-Connection";
446 hdr_len = 16;
447 }
448
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100449 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200450 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100451 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
452 if (wanted & TX_CON_KAL_SET)
453 txn->flags |= TX_CON_KAL_SET;
454 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100455 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +0100456 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100457 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
458 if (wanted & TX_CON_CLO_SET)
459 txn->flags |= TX_CON_CLO_SET;
460 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100461 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +0100462 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100463 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100464
465 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
466 return;
467
468 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
469 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100470 hdr_val = "Connection: close";
471 hdr_len = 17;
472 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
473 hdr_val = "Proxy-Connection: close";
474 hdr_len = 23;
475 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100476 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100477 }
478
479 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
480 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100481 hdr_val = "Connection: keep-alive";
482 hdr_len = 22;
483 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
484 hdr_val = "Proxy-Connection: keep-alive";
485 hdr_len = 28;
486 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100487 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100488 }
489 return;
Willy Tarreau5b154472009-12-21 20:11:07 +0100490}
491
Willy Tarreau87b09662015-04-03 00:22:06 +0200492void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200493{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200494 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200495 int tmp = TX_CON_WANT_KAL;
496
Christopher Fauletf2824e62018-10-01 12:12:37 +0200497 if (IS_HTX_STRM(s))
Christopher Faulet0f226952018-10-22 09:29:56 +0200498 return htx_adjust_conn_mode(s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200499
Christopher Faulet315b39c2018-09-21 16:26:19 +0200500 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
501 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
502 tmp = TX_CON_WANT_TUN;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200503
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200504 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Faulet315b39c2018-09-21 16:26:19 +0200505 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200506 tmp = TX_CON_WANT_SCL;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200507
Christopher Faulet315b39c2018-09-21 16:26:19 +0200508 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
509 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200510 tmp = TX_CON_WANT_CLO;
511
512 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
513 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
514
515 if (!(txn->flags & TX_HDR_CONN_PRS) &&
516 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
517 /* parse the Connection header and possibly clean it */
518 int to_del = 0;
519 if ((msg->flags & HTTP_MSGF_VER_11) ||
520 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200521 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200522 to_del |= 2; /* remove "keep-alive" */
523 if (!(msg->flags & HTTP_MSGF_VER_11))
524 to_del |= 1; /* remove "close" */
525 http_parse_connection_header(txn, msg, to_del);
526 }
527
528 /* check if client or config asks for explicit close in KAL/SCL */
529 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
530 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
531 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
532 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
533 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200534 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200535 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
536}
William Lallemand82fe75c2012-10-23 10:25:10 +0200537
Willy Tarreaud787e662009-07-07 10:14:51 +0200538/* This stream analyser waits for a complete HTTP request. It returns 1 if the
539 * processing can continue on next analysers, or zero if it either needs more
540 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100541 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200542 * when it has nothing left to do, and may remove any analyser when it wants to
543 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100544 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200545int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100546{
Willy Tarreau59234e92008-11-30 23:51:27 +0100547 /*
548 * We will parse the partial (or complete) lines.
549 * We will check the request syntax, and also join multi-line
550 * headers. An index of all the lines will be elaborated while
551 * parsing.
552 *
553 * For the parsing, we use a 28 states FSM.
554 *
555 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200556 * ci_head(req) = beginning of request
557 * ci_head(req) + msg->eoh = end of processed headers / start of current one
558 * ci_tail(req) = end of input data
559 * msg->eol = end of current header or line (LF or CRLF)
560 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200561 *
562 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200563 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200564 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
565 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100566 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100567
Willy Tarreau59234e92008-11-30 23:51:27 +0100568 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200569 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200570 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100571 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200572 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100573
Christopher Faulete0768eb2018-10-03 16:38:02 +0200574 if (IS_HTX_STRM(s))
575 return htx_wait_for_request(s, req, an_bit);
576
Christopher Faulet45073512018-07-20 10:16:29 +0200577 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 +0100578 now_ms, __FUNCTION__,
579 s,
580 req,
581 req->rex, req->wex,
582 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200583 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100584 req->analysers);
585
Willy Tarreau52a0c602009-08-16 22:45:38 +0200586 /* we're speaking HTTP here, so let's speak HTTP to the client */
587 s->srv_error = http_return_srv_error;
588
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300589 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200590 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300591 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
592
Willy Tarreau83e3af02009-12-28 17:39:57 +0100593 /* There's a protected area at the end of the buffer for rewriting
594 * purposes. We don't want to start to parse the request if the
595 * protected area is affected, because we may have to move processed
596 * data later, which is much more complicated.
597 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200598 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200599 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100600 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200601 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100602 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100603 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200604 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200605 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100606 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100607 return 0;
608 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200609 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200610 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200611 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100612 }
613
Willy Tarreauf37954d2018-06-15 18:31:02 +0200614 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100615 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100616 }
617
Willy Tarreau59234e92008-11-30 23:51:27 +0100618 /* 1: we might have to print this header in debug mode */
619 if (unlikely((global.mode & MODE_DEBUG) &&
620 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200621 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100622 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100623
Willy Tarreauf37954d2018-06-15 18:31:02 +0200624 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200625 /* this is a bit complex : in case of error on the request line,
626 * we know that rq.l is still zero, so we display only the part
627 * up to the end of the line (truncated by debug_hdr).
628 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200629 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100630 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100631
Willy Tarreau59234e92008-11-30 23:51:27 +0100632 sol += hdr_idx_first_pos(&txn->hdr_idx);
633 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100634
Willy Tarreau59234e92008-11-30 23:51:27 +0100635 while (cur_idx) {
636 eol = sol + txn->hdr_idx.v[cur_idx].len;
637 debug_hdr("clihdr", s, sol, eol);
638 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
639 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100640 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100641 }
642
Willy Tarreau58f10d72006-12-04 02:26:12 +0100643
Willy Tarreau59234e92008-11-30 23:51:27 +0100644 /*
645 * Now we quickly check if we have found a full valid request.
646 * If not so, we check the FD and buffer states before leaving.
647 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100648 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100649 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200650 * on a keep-alive stream, if we encounter and error, close, t/o,
651 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100652 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200653 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200654 * Last, we may increase some tracked counters' http request errors on
655 * the cases that are deliberately the client's fault. For instance,
656 * a timeout or connection reset is not counted as an error. However
657 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100658 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100659
Willy Tarreau655dce92009-11-08 13:10:58 +0100660 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100661 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100662 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100663 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100664 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200665 stream_inc_http_req_ctr(s);
666 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200667 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100668 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100669 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100670
Willy Tarreau59234e92008-11-30 23:51:27 +0100671 /* 1: Since we are in header mode, if there's no space
672 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200673 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100674 * must terminate it now.
675 */
Willy Tarreau23752332018-06-15 14:54:53 +0200676 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100677 /* FIXME: check if URI is set and return Status
678 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100679 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200680 stream_inc_http_req_ctr(s);
681 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200682 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +0200683 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +0200684 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +0100685 goto return_bad_req;
686 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100687
Willy Tarreau59234e92008-11-30 23:51:27 +0100688 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200689 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200690 if (!(s->flags & SF_ERR_MASK))
691 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100692
Willy Tarreaufcffa692010-01-10 14:21:19 +0100693 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100694 goto failed_keep_alive;
695
Willy Tarreau0f228a02015-05-01 15:37:53 +0200696 if (sess->fe->options & PR_O_IGNORE_PRB)
697 goto failed_keep_alive;
698
Willy Tarreau59234e92008-11-30 23:51:27 +0100699 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +0200700 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200701 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +0200702 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200703 }
704
Willy Tarreaudc979f22012-12-04 10:39:01 +0100705 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +0100706 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +0100707 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100708 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +0100709 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +0200710 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200711 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100712 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200713 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100714 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200715
Willy Tarreaue7dff022015-04-03 01:14:29 +0200716 if (!(s->flags & SF_FINST_MASK))
717 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +0100718 return 0;
719 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200720
Willy Tarreau59234e92008-11-30 23:51:27 +0100721 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200722 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200723 if (!(s->flags & SF_ERR_MASK))
724 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100725
Willy Tarreaufcffa692010-01-10 14:21:19 +0100726 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100727 goto failed_keep_alive;
728
Willy Tarreau0f228a02015-05-01 15:37:53 +0200729 if (sess->fe->options & PR_O_IGNORE_PRB)
730 goto failed_keep_alive;
731
Willy Tarreau59234e92008-11-30 23:51:27 +0100732 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +0200733 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200734 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +0200735 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200736 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100737 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +0100738 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +0100739 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200740 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +0100741 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200742
Willy Tarreau87b09662015-04-03 00:22:06 +0200743 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200744 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100745 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200746 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100747 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200748
Willy Tarreaue7dff022015-04-03 01:14:29 +0200749 if (!(s->flags & SF_FINST_MASK))
750 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +0100751 return 0;
752 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200753
Willy Tarreau59234e92008-11-30 23:51:27 +0100754 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200755 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200756 if (!(s->flags & SF_ERR_MASK))
757 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100758
Willy Tarreaufcffa692010-01-10 14:21:19 +0100759 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100760 goto failed_keep_alive;
761
Willy Tarreau0f228a02015-05-01 15:37:53 +0200762 if (sess->fe->options & PR_O_IGNORE_PRB)
763 goto failed_keep_alive;
764
Willy Tarreau4076a152009-04-02 15:18:36 +0200765 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200766 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100767 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +0100768 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +0100769 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200770 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +0100771 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +0200772 stream_inc_http_err_ctr(s);
773 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200774 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100775 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200776 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100777 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200778
Willy Tarreaue7dff022015-04-03 01:14:29 +0200779 if (!(s->flags & SF_FINST_MASK))
780 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +0200781 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100782 }
783
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200784 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200785 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100786 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100787
788 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req)) {
Willy Tarreau5e205522011-12-17 16:34:27 +0100789 /* We need more data, we have to re-enable quick-ack in case we
790 * previously disabled it, otherwise we might cause the client
791 * to delay next data.
792 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100793 conn_set_quickack(objt_conn(sess->origin), 1);
Willy Tarreau5e205522011-12-17 16:34:27 +0100794 }
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100795
Willy Tarreaufcffa692010-01-10 14:21:19 +0100796 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
797 /* If the client starts to talk, let's fall back to
798 * request timeout processing.
799 */
800 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +0100801 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +0100802 }
803
Willy Tarreau59234e92008-11-30 23:51:27 +0100804 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +0100805 if (!tick_isset(req->analyse_exp)) {
806 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
807 (txn->flags & TX_WAIT_NEXT_RQ) &&
808 tick_isset(s->be->timeout.httpka))
809 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
810 else
811 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
812 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100813
Willy Tarreau59234e92008-11-30 23:51:27 +0100814 /* we're not ready yet */
815 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +0100816
817 failed_keep_alive:
818 /* Here we process low-level errors for keep-alive requests. In
819 * short, if the request is not the first one and it experiences
820 * a timeout, read error or shutdown, we just silently close so
821 * that the client can try again.
822 */
823 txn->status = 0;
824 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +0100825 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +0100826 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +0200827 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100828 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100829 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +0100830 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +0100831 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100832
Willy Tarreaud787e662009-07-07 10:14:51 +0200833 /* OK now we have a complete HTTP request with indexed headers. Let's
834 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200835 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +0100836 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +0100837 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +0100838 * byte after the last LF. msg->sov points to the first byte of data.
839 * msg->eol cannot be trusted because it may have been left uninitialized
840 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +0200841 */
Willy Tarreau9cdde232007-05-02 20:58:19 +0200842
Willy Tarreau87b09662015-04-03 00:22:06 +0200843 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200844 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +0100845
Willy Tarreaub16a5742010-01-10 14:46:16 +0100846 if (txn->flags & TX_WAIT_NEXT_RQ) {
847 /* kill the pending keep-alive timeout */
848 txn->flags &= ~TX_WAIT_NEXT_RQ;
849 req->analyse_exp = TICK_ETERNITY;
850 }
851
852
Willy Tarreaud787e662009-07-07 10:14:51 +0200853 /* Maybe we found in invalid header name while we were configured not
854 * to block on that, so we have to capture it now.
855 */
856 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200857 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +0200858
Willy Tarreau59234e92008-11-30 23:51:27 +0100859 /*
860 * 1: identify the method
861 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200862 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +0100863
864 /* we can make use of server redirect on GET and HEAD */
865 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200866 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +0100867 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +0200868 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +0100869 /* PRI is reserved for the HTTP/2 preface */
870 msg->err_pos = 0;
871 goto return_bad_req;
872 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200873
Willy Tarreau59234e92008-11-30 23:51:27 +0100874 /*
875 * 2: check if the URI matches the monitor_uri.
876 * We have to do this for every request which gets in, because
877 * the monitor-uri is defined by the frontend.
878 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200879 if (unlikely((sess->fe->monitor_uri_len != 0) &&
880 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +0200881 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200882 sess->fe->monitor_uri,
883 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100884 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100885 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +0100886 */
Willy Tarreau59234e92008-11-30 23:51:27 +0100887 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +0100888
Willy Tarreaue7dff022015-04-03 01:14:29 +0200889 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100890 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +0100891
Willy Tarreau59234e92008-11-30 23:51:27 +0100892 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200893 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +0200894 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +0200895
Willy Tarreau59234e92008-11-30 23:51:27 +0100896 ret = acl_pass(ret);
897 if (cond->pol == ACL_COND_UNLESS)
898 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +0100899
Willy Tarreau59234e92008-11-30 23:51:27 +0100900 if (ret) {
901 /* we fail this request, let's return 503 service unavail */
902 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200903 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +0200904 if (!(s->flags & SF_ERR_MASK))
905 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +0100906 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +0100907 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100908 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100909
Joseph Herlant5ba80252018-11-15 09:25:36 -0800910 /* nothing to fail, let's reply normally */
Willy Tarreau59234e92008-11-30 23:51:27 +0100911 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200912 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +0200913 if (!(s->flags & SF_ERR_MASK))
914 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +0100915 goto return_prx_cond;
916 }
917
918 /*
919 * 3: Maybe we have to copy the original REQURI for the logs ?
920 * Note: we cannot log anymore if the request has been
921 * classified as invalid.
922 */
923 if (unlikely(s->logs.logwait & LW_REQ)) {
924 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100925 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100926 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100927
Stéphane Cottin23e9e932017-05-18 08:58:41 +0200928 if (urilen >= global.tune.requri_len )
929 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +0200930 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +0100931 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100932
Willy Tarreaud79a3b22012-12-28 09:40:16 +0100933 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +0100934 s->do_log(s);
935 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100936 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100937 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100938 }
Willy Tarreau06619262006-12-17 08:37:22 +0100939
Willy Tarreau91852eb2015-05-01 13:26:00 +0200940 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
941 * exactly one digit "." one digit. This check may be disabled using
942 * option accept-invalid-http-request.
943 */
944 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
945 if (msg->sl.rq.v_l != 8) {
946 msg->err_pos = msg->sl.rq.v;
947 goto return_bad_req;
948 }
949
Willy Tarreauf37954d2018-06-15 18:31:02 +0200950 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
951 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
952 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
953 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +0200954 msg->err_pos = msg->sl.rq.v + 4;
955 goto return_bad_req;
956 }
957 }
Willy Tarreau13317662015-05-01 13:47:08 +0200958 else {
959 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
960 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
961 goto return_bad_req;
962 }
Willy Tarreau91852eb2015-05-01 13:26:00 +0200963
Willy Tarreau5b154472009-12-21 20:11:07 +0100964 /* ... and check if the request is HTTP/1.1 or above */
965 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +0200966 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
967 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
968 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +0100969 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +0100970
971 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +0100972 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 +0100973
Willy Tarreau88d349d2010-01-25 12:15:43 +0100974 /* if the frontend has "option http-use-proxy-header", we'll check if
975 * we have what looks like a proxied connection instead of a connection,
976 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
977 * Note that this is *not* RFC-compliant, however browsers and proxies
978 * happen to do that despite being non-standard :-(
979 * We consider that a request not beginning with either '/' or '*' is
980 * a proxied connection, which covers both "scheme://location" and
981 * CONNECT ip:port.
982 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200983 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +0200984 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +0100985 txn->flags |= TX_USE_PX_CONN;
986
Willy Tarreaue8e785b2009-12-26 15:34:26 +0100987 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +0100988 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +0100989
Willy Tarreau59234e92008-11-30 23:51:27 +0100990 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200991 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +0200992 http_capture_headers(ci_head(req), &txn->hdr_idx,
993 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +0200994
Willy Tarreau557f1992015-05-01 10:05:17 +0200995 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
996 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +0100997 *
Willy Tarreau557f1992015-05-01 10:05:17 +0200998 * The length of a message body is determined by one of the following
999 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001000 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001001 * 1. Any response to a HEAD request and any response with a 1xx
1002 * (Informational), 204 (No Content), or 304 (Not Modified) status
1003 * code is always terminated by the first empty line after the
1004 * header fields, regardless of the header fields present in the
1005 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001006 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001007 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1008 * the connection will become a tunnel immediately after the empty
1009 * line that concludes the header fields. A client MUST ignore any
1010 * Content-Length or Transfer-Encoding header fields received in
1011 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001012 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001013 * 3. If a Transfer-Encoding header field is present and the chunked
1014 * transfer coding (Section 4.1) is the final encoding, the message
1015 * body length is determined by reading and decoding the chunked
1016 * data until the transfer coding indicates the data is complete.
1017 *
1018 * If a Transfer-Encoding header field is present in a response and
1019 * the chunked transfer coding is not the final encoding, the
1020 * message body length is determined by reading the connection until
1021 * it is closed by the server. If a Transfer-Encoding header field
1022 * is present in a request and the chunked transfer coding is not
1023 * the final encoding, the message body length cannot be determined
1024 * reliably; the server MUST respond with the 400 (Bad Request)
1025 * status code and then close the connection.
1026 *
1027 * If a message is received with both a Transfer-Encoding and a
1028 * Content-Length header field, the Transfer-Encoding overrides the
1029 * Content-Length. Such a message might indicate an attempt to
1030 * perform request smuggling (Section 9.5) or response splitting
1031 * (Section 9.4) and ought to be handled as an error. A sender MUST
1032 * remove the received Content-Length field prior to forwarding such
1033 * a message downstream.
1034 *
1035 * 4. If a message is received without Transfer-Encoding and with
1036 * either multiple Content-Length header fields having differing
1037 * field-values or a single Content-Length header field having an
1038 * invalid value, then the message framing is invalid and the
1039 * recipient MUST treat it as an unrecoverable error. If this is a
1040 * request message, the server MUST respond with a 400 (Bad Request)
1041 * status code and then close the connection. If this is a response
1042 * message received by a proxy, the proxy MUST close the connection
1043 * to the server, discard the received response, and send a 502 (Bad
1044 * Gateway) response to the client. If this is a response message
1045 * received by a user agent, the user agent MUST close the
1046 * connection to the server and discard the received response.
1047 *
1048 * 5. If a valid Content-Length header field is present without
1049 * Transfer-Encoding, its decimal value defines the expected message
1050 * body length in octets. If the sender closes the connection or
1051 * the recipient times out before the indicated number of octets are
1052 * received, the recipient MUST consider the message to be
1053 * incomplete and close the connection.
1054 *
1055 * 6. If this is a request message and none of the above are true, then
1056 * the message body length is zero (no message body is present).
1057 *
1058 * 7. Otherwise, this is a response message without a declared message
1059 * body length, so the message body length is determined by the
1060 * number of octets received prior to the server closing the
1061 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001062 */
1063
Willy Tarreau32b47f42009-10-18 20:55:02 +02001064 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001065 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001066 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001067 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001068 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001069 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001070 /* chunked not last, return badreq */
1071 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001072 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001073 }
1074
Willy Tarreau1c913912015-04-30 10:57:51 +02001075 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001076 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001077 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001078 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001079 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1080 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001081 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001082 signed long long cl;
1083
Willy Tarreauad14f752011-09-02 20:33:27 +02001084 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001085 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001086 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001087 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001088
Willy Tarreauad14f752011-09-02 20:33:27 +02001089 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
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; /* parse failure */
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 (cl < 0) {
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;
Willy Tarreauad14f752011-09-02 20:33:27 +02001097 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001098
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001099 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
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; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001102 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001103
Christopher Fauletbe821b92017-03-30 11:21:53 +02001104 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001105 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001106 }
1107
Willy Tarreau34dfc602015-05-01 10:09:49 +02001108 /* even bodyless requests have a known length */
1109 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001110
Willy Tarreau179085c2014-04-28 16:48:56 +02001111 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1112 * only change if both the request and the config reference something else.
1113 * Option httpclose by itself sets tunnel mode where headers are mangled.
1114 * However, if another mode is set, it will affect it (eg: server-close/
1115 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1116 * if FE and BE have the same settings (common). The method consists in
1117 * checking if options changed between the two calls (implying that either
1118 * one is non-null, or one of them is non-null and we are there for the first
1119 * time.
1120 */
1121 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001122 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001123 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001124
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001125 /* we may have to wait for the request's body */
1126 if ((s->be->options & PR_O_WREQ_BODY) &&
1127 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1128 req->analysers |= AN_REQ_HTTP_BODY;
1129
Willy Tarreau83ece462017-12-21 15:13:09 +01001130 /*
1131 * RFC7234#4:
1132 * A cache MUST write through requests with methods
1133 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1134 * the origin server; i.e., a cache is not allowed
1135 * to generate a reply to such a request before
1136 * having forwarded the request and having received
1137 * a corresponding response.
1138 *
1139 * RFC7231#4.2.1:
1140 * Of the request methods defined by this
1141 * specification, the GET, HEAD, OPTIONS, and TRACE
1142 * methods are defined to be safe.
1143 */
1144 if (likely(txn->meth == HTTP_METH_GET ||
1145 txn->meth == HTTP_METH_HEAD ||
1146 txn->meth == HTTP_METH_OPTIONS ||
1147 txn->meth == HTTP_METH_TRACE))
1148 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1149
Willy Tarreaud787e662009-07-07 10:14:51 +02001150 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001151 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001152 req->analyse_exp = TICK_ETERNITY;
1153 return 1;
1154
1155 return_bad_req:
1156 /* We centralize bad requests processing here */
1157 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1158 /* we detected a parsing error. We want to archive this request
1159 * in the dedicated proxy area for later troubleshooting.
1160 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001161 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001162 }
1163
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001164 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001165 txn->req.msg_state = HTTP_MSG_ERROR;
1166 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001167 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001168
Olivier Houcharda798bf52019-03-08 18:52:00 +01001169 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001170 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001171 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001172
1173 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001174 if (!(s->flags & SF_ERR_MASK))
1175 s->flags |= SF_ERR_PRXCOND;
1176 if (!(s->flags & SF_FINST_MASK))
1177 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001178
Christopher Faulet0184ea72017-01-05 14:06:34 +01001179 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001180 req->analyse_exp = TICK_ETERNITY;
1181 return 0;
1182}
1183
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001184
Willy Tarreau347a35d2013-11-22 17:51:09 +01001185/* This function prepares an applet to handle the stats. It can deal with the
1186 * "100-continue" expectation, check that admin rules are met for POST requests,
1187 * and program a response message if something was unexpected. It cannot fail
1188 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001189 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001190 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001191 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001192 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001193int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001194{
1195 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001196 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001197 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001198 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001199 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001200 struct uri_auth *uri_auth = s->be->uri_auth;
1201 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001202 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001203
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001204 appctx = si_appctx(si);
1205 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1206 appctx->st1 = appctx->st2 = 0;
1207 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1208 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001209 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001210 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001211
Willy Tarreauf37954d2018-06-15 18:31:02 +02001212 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001213 lookup = uri + uri_auth->uri_len;
1214
1215 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1216 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001217 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001218 break;
1219 }
1220 }
1221
1222 if (uri_auth->refresh) {
1223 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1224 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001225 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001226 break;
1227 }
1228 }
1229 }
1230
1231 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1232 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001233 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001234 break;
1235 }
1236 }
1237
Willy Tarreau1e62df92016-01-11 18:57:53 +01001238 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1239 if (memcmp(h, ";typed", 6) == 0) {
1240 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1241 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1242 break;
1243 }
1244 }
1245
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001246 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1247 if (memcmp(h, ";st=", 4) == 0) {
1248 int i;
1249 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001250 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001251 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1252 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001253 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001254 break;
1255 }
1256 }
1257 break;
1258 }
1259 }
1260
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001261 appctx->ctx.stats.scope_str = 0;
1262 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001263 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1264 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1265 int itx = 0;
1266 const char *h2;
1267 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1268 const char *err;
1269
1270 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1271 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001272 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001273 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1274 itx++;
1275 h++;
1276 }
1277
1278 if (itx > STAT_SCOPE_TXT_MAXLEN)
1279 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001280 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001281
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001282 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001283 memcpy(scope_txt, h2, itx);
1284 scope_txt[itx] = '\0';
1285 err = invalid_char(scope_txt);
1286 if (err) {
1287 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001288 appctx->ctx.stats.scope_str = 0;
1289 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001290 }
1291 break;
1292 }
1293 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001294
1295 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001296 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001297 int ret = 1;
1298
1299 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001300 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 +01001301 ret = acl_pass(ret);
1302 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1303 ret = !ret;
1304 }
1305
1306 if (ret) {
1307 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001308 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001309 break;
1310 }
1311 }
1312
Christopher Faulet5d45e382019-02-27 15:15:23 +01001313 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
1314 appctx->st0 = STAT_HTTP_HEAD;
1315 else if (txn->meth == HTTP_METH_POST && (msg->flags & HTTP_MSGF_CNT_LEN)) {
Christopher Fauletbcf242a2019-03-01 11:36:26 +01001316 if (appctx->ctx.stats.flags & STAT_ADMIN)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001317 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01001318 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01001319 /* POST without admin level */
1320 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001321 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1322 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001323 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001324 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001325 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01001326 /* Unsupported method or chunked POST */
1327 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
1328 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
1329 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001330 }
1331
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001332 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001333 return 1;
1334}
1335
Willy Tarreau87b09662015-04-03 00:22:06 +02001336int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001337 const char* name, unsigned int name_len,
1338 const char *str, struct my_regex *re,
1339 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001340{
Willy Tarreaueee5b512015-04-03 23:46:31 +02001341 struct hdr_idx *idx = &s->txn->hdr_idx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001342 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001343
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001344 /* Choose the header browsing function. */
1345 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001346 case ACT_HTTP_REPLACE_VAL:
Willy Tarreauc5a4fd52018-12-11 11:42:27 +01001347 return http_legacy_replace_header(idx, msg, name, name_len, str, re, output);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001348 case ACT_HTTP_REPLACE_HDR:
Willy Tarreauc5a4fd52018-12-11 11:42:27 +01001349 return http_legacy_replace_full_header(idx, msg, name, name_len, str, re, output);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001350 default: /* impossible */
1351 return -1;
1352 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001353}
1354
Willy Tarreau87b09662015-04-03 00:22:06 +02001355static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001356 const char* name, unsigned int name_len,
1357 struct list *fmt, struct my_regex *re,
1358 int action)
1359{
Willy Tarreau83061a82018-07-13 11:56:34 +02001360 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001361 int ret = -1;
1362
1363 replace = alloc_trash_chunk();
1364 if (!replace)
1365 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001366
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001367 replace->data = build_logline(s, replace->area, replace->size, fmt);
1368 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001369 goto leave;
1370
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001371 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1372 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001373
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001374 leave:
1375 free_trash_chunk(replace);
1376 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001377}
1378
Christopher Faulet87451fd2019-03-01 11:16:34 +01001379/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. */
1380static void http_handle_expect_hdr(struct stream *s, struct channel *req, struct http_msg *msg)
1381{
1382 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
1383 * then we must send an HTTP/1.1 100 Continue intermediate response.
1384 */
1385 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
1386 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
1387 struct hdr_ctx ctx;
1388 ctx.idx = 0;
1389 /* Expect is allowed in 1.1, look for it */
1390 if (http_find_header2("Expect", 6, ci_head(req), &s->txn->hdr_idx, &ctx) &&
1391 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
1392 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
1393 http_remove_header2(&s->txn->req, &s->txn->hdr_idx, &ctx);
1394 }
1395 }
1396}
1397
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001398/*
1399 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
1400 * built according to <fmt> log line format.
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001401 * If <early_hints> is NULL, it is allocated and the HTTP 103 response first
1402 * line is inserted before the header. If an error occurred <early_hints> is
1403 * released and NULL is returned. On success the updated buffer is returned.
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001404 */
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001405static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints,
1406 const char* name, unsigned int name_len,
1407 struct list *fmt)
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001408{
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001409 if (!early_hints) {
1410 early_hints = alloc_trash_chunk();
1411 if (!early_hints)
1412 goto fail;
1413 if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len))
1414 goto fail;
1415 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001416
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001417 if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2))
1418 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001419
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001420 early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt);
1421 if (!chunk_memcat(early_hints, "\r\n", 2))
1422 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001423
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001424 return early_hints;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001425
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001426 fail:
1427 free_trash_chunk(early_hints);
1428 return NULL;
1429}
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001430
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001431/* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the
1432 * response is added. If an error occurred or if another response was already
1433 * sent, this function does nothing.
1434 */
1435static void http_send_early_hints(struct stream *s, struct buffer *early_hints)
1436{
1437 struct channel *chn = s->txn->rsp.chn;
1438 char *cur_ptr = ci_head(chn);
1439 int ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001440
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001441 /* If a response was already sent, skip early hints */
1442 if (s->txn->status > 0)
1443 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001444
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001445 if (!chunk_memcat(early_hints, "\r\n", 2))
1446 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001447
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001448 ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints));
1449 c_adv(chn, ret);
1450 chn->total += ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001451}
1452
Willy Tarreau87b09662015-04-03 00:22:06 +02001453/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001454 * transaction <txn>. Returns the verdict of the first rule that prevents
1455 * further processing of the request (auth, deny, ...), and defaults to
1456 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1457 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001458 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1459 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1460 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001461 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001462enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001463http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001464{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001465 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001466 struct http_txn *txn = s->txn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001467 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001468 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001469 const char *auth_realm;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001470 struct buffer *early_hints = NULL;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001471 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001472 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001473 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001474
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001475 /* If "the current_rule_list" match the executed rule list, we are in
1476 * resume condition. If a resume is needed it is always in the action
1477 * and never in the ACL or converters. In this case, we initialise the
1478 * current rule, and go to the action execution point.
1479 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001480 if (s->current_rule) {
1481 rule = s->current_rule;
1482 s->current_rule = NULL;
1483 if (s->current_rule_list == rules)
1484 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001485 }
1486 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001487
Willy Tarreauff011f22011-01-06 17:51:27 +01001488 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001489
Willy Tarreau96257ec2012-12-27 10:46:37 +01001490 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001491 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001492 int ret;
1493
Willy Tarreau192252e2015-04-04 01:47:55 +02001494 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001495 ret = acl_pass(ret);
1496
Willy Tarreauff011f22011-01-06 17:51:27 +01001497 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001498 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001499
1500 if (!ret) /* condition not matched */
1501 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001502 }
1503
Willy Tarreauacc98002015-09-27 23:34:39 +02001504 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001505resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001506 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001507 case ACT_ACTION_ALLOW:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001508 rule_ret = HTTP_RULE_RES_STOP;
1509 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001510
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001511 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001512 if (deny_status)
1513 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001514 rule_ret = HTTP_RULE_RES_DENY;
1515 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001516
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001517 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001518 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02001519 if (deny_status)
1520 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001521 rule_ret = HTTP_RULE_RES_DENY;
1522 goto end;
Willy Tarreauccbcc372012-12-27 12:37:57 +01001523
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001524 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001525 /* Be sure to send any pending HTTP 103 response first */
1526 if (early_hints) {
1527 http_send_early_hints(s, early_hints);
1528 free_trash_chunk(early_hints);
1529 early_hints = NULL;
1530 }
Willy Tarreauae3c0102014-04-28 23:22:08 +02001531 /* Auth might be performed on regular http-req rules as well as on stats */
1532 auth_realm = rule->arg.auth.realm;
1533 if (!auth_realm) {
1534 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1535 auth_realm = STATS_DEFAULT_REALM;
1536 else
1537 auth_realm = px->id;
1538 }
1539 /* send 401/407 depending on whether we use a proxy or not. We still
1540 * count one error, because normal browsing won't significantly
1541 * increase the counter but brute force attempts will.
1542 */
1543 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1544 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001545 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001546 stream_inc_http_err_ctr(s);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001547 rule_ret = HTTP_RULE_RES_ABRT;
1548 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001549
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001550 case ACT_HTTP_REDIR:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001551 /* Be sure to send any pending HTTP 103 response first */
1552 if (early_hints) {
1553 http_send_early_hints(s, early_hints);
1554 free_trash_chunk(early_hints);
1555 early_hints = NULL;
1556 }
Christopher Fauletea827bd2018-11-15 15:34:11 +01001557 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau0b748332014-04-29 00:13:29 +02001558 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Fauletea827bd2018-11-15 15:34:11 +01001559 rule_ret = HTTP_RULE_RES_BADREQ;
1560 goto end;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001561
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001562 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001563 s->task->nice = rule->arg.nice;
1564 break;
1565
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001566 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001567 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001568 break;
1569
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001570 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001571 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Willy Tarreau51347ed2013-06-11 19:34:13 +02001572 break;
1573
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001574 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001575 s->logs.level = rule->arg.loglevel;
1576 break;
1577
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001578 case ACT_HTTP_REPLACE_HDR:
1579 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001580 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1581 rule->arg.hdr_add.name_len,
1582 &rule->arg.hdr_add.fmt,
Christopher Fauletea827bd2018-11-15 15:34:11 +01001583 &rule->arg.hdr_add.re, rule->action)) {
1584 rule_ret = HTTP_RULE_RES_BADREQ;
1585 goto end;
1586 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001587 break;
1588
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001589 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001590 ctx.idx = 0;
1591 /* remove all occurrences of the header */
1592 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001593 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001594 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001595 }
Willy Tarreau85603282015-01-21 20:39:27 +01001596 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001597
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001598 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001599 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001600 /* The scope of the trash buffer must be limited to this function. The
1601 * build_logline() function can execute a lot of other function which
1602 * can use the trash buffer. So for limiting the scope of this global
1603 * buffer, we build first the header value using build_logline, and
1604 * after we store the header name.
1605 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001606 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001607
1608 replace = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001609 if (!replace) {
1610 rule_ret = HTTP_RULE_RES_BADREQ;
1611 goto end;
1612 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001613
Thierry Fournier4b788f72016-06-01 13:35:36 +02001614 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001615 len += build_logline(s, replace->area + len,
1616 replace->size - len,
1617 &rule->arg.hdr_add.fmt);
1618 memcpy(replace->area, rule->arg.hdr_add.name,
1619 rule->arg.hdr_add.name_len);
1620 replace->area[rule->arg.hdr_add.name_len] = ':';
1621 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1622 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001623
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001624 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001625 /* remove all occurrences of the header */
1626 ctx.idx = 0;
1627 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001628 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001629 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1630 }
1631 }
1632
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001633 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001634 static unsigned char rate_limit = 0;
1635
1636 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001637 replace->area[rule->arg.hdr_add.name_len] = 0;
1638 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,
1639 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001640 }
1641
Olivier Houcharda798bf52019-03-08 18:52:00 +01001642 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001643 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001644 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001645 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001646 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001647 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001648
1649 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001650 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001651 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001652
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001653 case ACT_HTTP_DEL_ACL:
1654 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001655 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001656 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001657
1658 /* collect reference */
1659 ref = pat_ref_lookup(rule->arg.map.ref);
1660 if (!ref)
1661 continue;
1662
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001663 /* allocate key */
1664 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001665 if (!key) {
1666 rule_ret = HTTP_RULE_RES_BADREQ;
1667 goto end;
1668 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001669
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001670 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001671 key->data = build_logline(s, key->area, key->size,
1672 &rule->arg.map.key);
1673 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001674
1675 /* perform update */
1676 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001677 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001678 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001679 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001680
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001681 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001682 break;
1683 }
1684
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001685 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001686 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001687 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001688
1689 /* collect reference */
1690 ref = pat_ref_lookup(rule->arg.map.ref);
1691 if (!ref)
1692 continue;
1693
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001694 /* allocate key */
1695 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001696 if (!key) {
1697 rule_ret = HTTP_RULE_RES_BADREQ;
1698 goto end;
1699 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001700
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001701 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001702 key->data = build_logline(s, key->area, key->size,
1703 &rule->arg.map.key);
1704 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001705
1706 /* perform update */
1707 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001708 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001709 if (pat_ref_find_elt(ref, key->area) == NULL)
1710 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001711 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001712
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001713 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001714 break;
1715 }
1716
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001717 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001718 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001719 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001720
1721 /* collect reference */
1722 ref = pat_ref_lookup(rule->arg.map.ref);
1723 if (!ref)
1724 continue;
1725
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001726 /* allocate key */
1727 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001728 if (!key) {
1729 rule_ret = HTTP_RULE_RES_BADREQ;
1730 goto end;
1731 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001732
1733 /* allocate value */
1734 value = alloc_trash_chunk();
1735 if (!value) {
1736 free_trash_chunk(key);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001737 rule_ret = HTTP_RULE_RES_BADREQ;
1738 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001739 }
1740
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001741 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001742 key->data = build_logline(s, key->area, key->size,
1743 &rule->arg.map.key);
1744 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001745
1746 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001747 value->data = build_logline(s, value->area,
1748 value->size,
1749 &rule->arg.map.value);
1750 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001751
1752 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001753 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001754 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001755 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001756 else
1757 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001758 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001759
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001760 free_trash_chunk(key);
1761 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001762 break;
1763 }
William Lallemand73025dd2014-04-24 14:38:37 +02001764
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001765 case ACT_HTTP_EARLY_HINT:
1766 if (!(txn->req.flags & HTTP_MSGF_VER_11))
1767 break;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001768 early_hints = http_apply_early_hint_rule(s, early_hints,
1769 rule->arg.early_hint.name,
1770 rule->arg.early_hint.name_len,
1771 &rule->arg.early_hint.fmt);
1772 if (!early_hints) {
Christopher Fauletea827bd2018-11-15 15:34:11 +01001773 rule_ret = HTTP_RULE_RES_DONE;
1774 goto end;
1775 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001776 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02001777 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01001778 if ((s->req.flags & CF_READ_ERROR) ||
1779 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01001780 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02001781 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02001782
Willy Tarreauacc98002015-09-27 23:34:39 +02001783 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02001784 case ACT_RET_ERR:
1785 case ACT_RET_CONT:
1786 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02001787 case ACT_RET_STOP:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001788 rule_ret = HTTP_RULE_RES_DONE;
1789 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02001790 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02001791 s->current_rule = rule;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001792 rule_ret = HTTP_RULE_RES_YIELD;
1793 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001794 }
William Lallemand73025dd2014-04-24 14:38:37 +02001795 break;
1796
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001797 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02001798 /* Note: only the first valid tracking parameter of each
1799 * applies.
1800 */
1801
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001802 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02001803 struct stktable *t;
1804 struct stksess *ts;
1805 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001806 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02001807
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02001808 t = rule->arg.trk_ctr.table.t;
1809 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 +02001810
1811 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001812 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02001813
1814 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001815 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1816 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1817 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001818 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02001819
Emeric Brun819fc6f2017-06-13 19:37:32 +02001820 if (ptr1)
1821 stktable_data_cast(ptr1, http_req_cnt)++;
1822
1823 if (ptr2)
1824 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
1825 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
1826
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001827 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01001828
1829 /* If data was modified, we need to touch to re-schedule sync */
1830 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001831 }
Willy Tarreau09448f72014-06-25 18:12:15 +02001832
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001833 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001834 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001835 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02001836 }
1837 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02001838 break;
1839
Joseph Herlant5ba80252018-11-15 09:25:36 -08001840 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02001841 default:
1842 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001843 }
1844 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01001845
Christopher Fauletea827bd2018-11-15 15:34:11 +01001846 end:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001847 if (early_hints) {
1848 http_send_early_hints(s, early_hints);
1849 free_trash_chunk(early_hints);
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001850 }
1851
Willy Tarreau96257ec2012-12-27 10:46:37 +01001852 /* we reached the end of the rules, nothing to report */
Christopher Fauletea827bd2018-11-15 15:34:11 +01001853 return rule_ret;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001854}
1855
Willy Tarreau71241ab2012-12-27 11:30:54 +01001856
Willy Tarreau51d861a2015-05-22 17:30:48 +02001857/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
1858 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
1859 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
1860 * is returned, the process can continue the evaluation of next rule list. If
1861 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
1862 * is returned, it means the operation could not be processed and a server error
1863 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
1864 * deny rule. If *YIELD is returned, the caller must call again the function
1865 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001866 */
Christopher Faulet10079f52018-10-03 15:17:28 +02001867enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001868http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001869{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001870 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001871 struct http_txn *txn = s->txn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001872 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001873 struct hdr_ctx ctx;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001874 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001875 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001876
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001877 /* If "the current_rule_list" match the executed rule list, we are in
1878 * resume condition. If a resume is needed it is always in the action
1879 * and never in the ACL or converters. In this case, we initialise the
1880 * current rule, and go to the action execution point.
1881 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001882 if (s->current_rule) {
1883 rule = s->current_rule;
1884 s->current_rule = NULL;
1885 if (s->current_rule_list == rules)
1886 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001887 }
1888 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001889
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001890 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001891
1892 /* check optional condition */
1893 if (rule->cond) {
1894 int ret;
1895
Willy Tarreau192252e2015-04-04 01:47:55 +02001896 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001897 ret = acl_pass(ret);
1898
1899 if (rule->cond->pol == ACL_COND_UNLESS)
1900 ret = !ret;
1901
1902 if (!ret) /* condition not matched */
1903 continue;
1904 }
1905
Willy Tarreauacc98002015-09-27 23:34:39 +02001906 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001907resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001908 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001909 case ACT_ACTION_ALLOW:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001910 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
1911 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001912
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001913 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001914 txn->flags |= TX_SVDENY;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001915 rule_ret = HTTP_RULE_RES_STOP;
1916 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001917
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001918 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001919 s->task->nice = rule->arg.nice;
1920 break;
1921
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001922 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001923 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001924 break;
1925
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001926 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001927 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Willy Tarreau51347ed2013-06-11 19:34:13 +02001928 break;
1929
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001930 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001931 s->logs.level = rule->arg.loglevel;
1932 break;
1933
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001934 case ACT_HTTP_REPLACE_HDR:
1935 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001936 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
1937 rule->arg.hdr_add.name_len,
1938 &rule->arg.hdr_add.fmt,
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001939 &rule->arg.hdr_add.re, rule->action)) {
1940 rule_ret = HTTP_RULE_RES_BADREQ;
1941 goto end;
1942 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001943 break;
1944
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001945 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001946 ctx.idx = 0;
1947 /* remove all occurrences of the header */
1948 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001949 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001950 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
1951 }
Willy Tarreau85603282015-01-21 20:39:27 +01001952 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001953
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001954 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001955 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02001956 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001957
1958 replace = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001959 if (!replace) {
1960 rule_ret = HTTP_RULE_RES_BADREQ;
1961 goto end;
1962 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001963
1964 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001965 memcpy(replace->area, rule->arg.hdr_add.name,
1966 rule->arg.hdr_add.name_len);
1967 replace->data = rule->arg.hdr_add.name_len;
1968 replace->area[replace->data++] = ':';
1969 replace->area[replace->data++] = ' ';
1970 replace->data += build_logline(s,
1971 replace->area + replace->data,
1972 replace->size - replace->data,
1973 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01001974
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001975 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001976 /* remove all occurrences of the header */
1977 ctx.idx = 0;
1978 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001979 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001980 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
1981 }
1982 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001983
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001984 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001985 static unsigned char rate_limit = 0;
1986
1987 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001988 replace->area[rule->arg.hdr_add.name_len] = 0;
1989 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,
1990 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001991 }
1992
Olivier Houcharda798bf52019-03-08 18:52:00 +01001993 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001994 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001995 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001996 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001997 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001998 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001999 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02002000 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002001
2002 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002003 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002004 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002005
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002006 case ACT_HTTP_DEL_ACL:
2007 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002008 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002009 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002010
2011 /* collect reference */
2012 ref = pat_ref_lookup(rule->arg.map.ref);
2013 if (!ref)
2014 continue;
2015
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002016 /* allocate key */
2017 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002018 if (!key) {
2019 rule_ret = HTTP_RULE_RES_BADREQ;
2020 goto end;
2021 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002022
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002023 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002024 key->data = build_logline(s, key->area, key->size,
2025 &rule->arg.map.key);
2026 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002027
2028 /* perform update */
2029 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002030 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002031 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002032 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002033
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002034 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002035 break;
2036 }
2037
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002038 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002039 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002040 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002041
2042 /* collect reference */
2043 ref = pat_ref_lookup(rule->arg.map.ref);
2044 if (!ref)
2045 continue;
2046
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002047 /* allocate key */
2048 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002049 if (!key) {
2050 rule_ret = HTTP_RULE_RES_BADREQ;
2051 goto end;
2052 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002053
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002054 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002055 key->data = build_logline(s, key->area, key->size,
2056 &rule->arg.map.key);
2057 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002058
2059 /* perform update */
2060 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002061 if (pat_ref_find_elt(ref, key->area) == NULL)
2062 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002063
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002064 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002065 break;
2066 }
2067
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002068 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002069 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002070 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002071
2072 /* collect reference */
2073 ref = pat_ref_lookup(rule->arg.map.ref);
2074 if (!ref)
2075 continue;
2076
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002077 /* allocate key */
2078 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002079 if (!key) {
2080 rule_ret = HTTP_RULE_RES_BADREQ;
2081 goto end;
2082 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002083
2084 /* allocate value */
2085 value = alloc_trash_chunk();
2086 if (!value) {
2087 free_trash_chunk(key);
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002088 rule_ret = HTTP_RULE_RES_BADREQ;
2089 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002090 }
2091
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002092 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002093 key->data = build_logline(s, key->area, key->size,
2094 &rule->arg.map.key);
2095 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002096
2097 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002098 value->data = build_logline(s, value->area,
2099 value->size,
2100 &rule->arg.map.value);
2101 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002102
2103 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002104 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002105 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002106 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002107 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002108 else
2109 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002110 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002111 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002112 free_trash_chunk(key);
2113 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002114 break;
2115 }
William Lallemand73025dd2014-04-24 14:38:37 +02002116
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002117 case ACT_HTTP_REDIR:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002118 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002119 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002120 rule_ret = HTTP_RULE_RES_BADREQ;
2121 goto end;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002122
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002123 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2124 /* Note: only the first valid tracking parameter of each
2125 * applies.
2126 */
2127
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002128 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002129 struct stktable *t;
2130 struct stksess *ts;
2131 struct stktable_key *key;
2132 void *ptr;
2133
2134 t = rule->arg.trk_ctr.table.t;
2135 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2136
2137 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002138 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002139
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002140 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002141
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002142 /* let's count a new HTTP request as it's the first time we do it */
2143 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2144 if (ptr)
2145 stktable_data_cast(ptr, http_req_cnt)++;
2146
2147 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2148 if (ptr)
2149 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2150 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2151
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002152 /* When the client triggers a 4xx from the server, it's most often due
2153 * to a missing object or permission. These events should be tracked
2154 * because if they happen often, it may indicate a brute force or a
2155 * vulnerability scan. Normally this is done when receiving the response
2156 * but here we're tracking after this ought to have been done so we have
2157 * to do it on purpose.
2158 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002159 if ((unsigned)(txn->status - 400) < 100) {
2160 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2161 if (ptr)
2162 stktable_data_cast(ptr, http_err_cnt)++;
2163
2164 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2165 if (ptr)
2166 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2167 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2168 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002169
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002170 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002171
Emeric Brun0fed0b02017-11-29 16:15:07 +01002172 /* If data was modified, we need to touch to re-schedule sync */
2173 stktable_touch_local(t, ts, 0);
2174
Emeric Brun819fc6f2017-06-13 19:37:32 +02002175 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2176 if (sess->fe != s->be)
2177 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2178
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002179 }
2180 }
2181 break;
2182
Thierry FOURNIER42148732015-09-02 17:17:33 +02002183 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002184 if ((s->req.flags & CF_READ_ERROR) ||
2185 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002186 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002187 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002188
Willy Tarreauacc98002015-09-27 23:34:39 +02002189 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002190 case ACT_RET_ERR:
2191 case ACT_RET_CONT:
2192 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002193 case ACT_RET_STOP:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002194 rule_ret = HTTP_RULE_RES_STOP;
2195 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002196 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002197 s->current_rule = rule;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002198 rule_ret = HTTP_RULE_RES_YIELD;
2199 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002200 }
William Lallemand73025dd2014-04-24 14:38:37 +02002201 break;
2202
Joseph Herlant5ba80252018-11-15 09:25:36 -08002203 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002204 default:
2205 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002206 }
2207 }
2208
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002209 end:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002210 /* we reached the end of the rules, nothing to report */
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002211 return rule_ret;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002212}
2213
2214
Willy Tarreau71241ab2012-12-27 11:30:54 +01002215/* Perform an HTTP redirect based on the information in <rule>. The function
2216 * returns non-zero on success, or zero in case of a, irrecoverable error such
2217 * as too large a request to build a valid response.
2218 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002219int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002220{
Willy Tarreaub329a312015-05-22 16:27:37 +02002221 struct http_msg *req = &txn->req;
2222 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002223 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002224 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002225 int ret = 0;
2226
Christopher Fauletf2824e62018-10-01 12:12:37 +02002227 if (IS_HTX_STRM(s))
2228 return htx_apply_redirect_rule(rule, s, txn);
2229
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002230 chunk = alloc_trash_chunk();
2231 if (!chunk)
2232 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002233
2234 /* build redirect message */
2235 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002236 case 308:
2237 msg_fmt = HTTP_308;
2238 break;
2239 case 307:
2240 msg_fmt = HTTP_307;
2241 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002242 case 303:
2243 msg_fmt = HTTP_303;
2244 break;
2245 case 301:
2246 msg_fmt = HTTP_301;
2247 break;
2248 case 302:
2249 default:
2250 msg_fmt = HTTP_302;
2251 break;
2252 }
2253
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002254 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2255 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002256
2257 switch(rule->type) {
2258 case REDIRECT_TYPE_SCHEME: {
2259 const char *path;
2260 const char *host;
2261 struct hdr_ctx ctx;
2262 int pathlen;
2263 int hostlen;
2264
2265 host = "";
2266 hostlen = 0;
2267 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002268 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002269 host = ctx.line + ctx.val;
2270 hostlen = ctx.vlen;
2271 }
2272
Willy Tarreau6b952c82018-09-10 17:45:34 +02002273 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002274 /* build message using path */
2275 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002276 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002277 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2278 int qs = 0;
2279 while (qs < pathlen) {
2280 if (path[qs] == '?') {
2281 pathlen = qs;
2282 break;
2283 }
2284 qs++;
2285 }
2286 }
2287 } else {
2288 path = "/";
2289 pathlen = 1;
2290 }
2291
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002292 if (rule->rdr_str) { /* this is an old "redirect" rule */
2293 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002294 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002295 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002296
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002297 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002298 memcpy(chunk->area + chunk->data, rule->rdr_str,
2299 rule->rdr_len);
2300 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002301 }
2302 else {
2303 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002304 chunk->data += build_logline(s,
2305 chunk->area + chunk->data,
2306 chunk->size - chunk->data,
2307 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002308
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002309 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002310 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002311 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002312 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002313 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002314 memcpy(chunk->area + chunk->data, "://", 3);
2315 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002316
2317 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002318 memcpy(chunk->area + chunk->data, host, hostlen);
2319 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002320
2321 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002322 memcpy(chunk->area + chunk->data, path, pathlen);
2323 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002324
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002325 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002326 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002327 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002328 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002329 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002330 chunk->area[chunk->data] = '/';
2331 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002332 }
2333
2334 break;
2335 }
2336 case REDIRECT_TYPE_PREFIX: {
2337 const char *path;
2338 int pathlen;
2339
Willy Tarreau6b952c82018-09-10 17:45:34 +02002340 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002341 /* build message using path */
2342 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002343 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002344 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2345 int qs = 0;
2346 while (qs < pathlen) {
2347 if (path[qs] == '?') {
2348 pathlen = qs;
2349 break;
2350 }
2351 qs++;
2352 }
2353 }
2354 } else {
2355 path = "/";
2356 pathlen = 1;
2357 }
2358
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002359 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002360 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002361 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002362
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002363 /* add prefix. Note that if prefix == "/", we don't want to
2364 * add anything, otherwise it makes it hard for the user to
2365 * configure a self-redirection.
2366 */
2367 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002368 memcpy(chunk->area + chunk->data,
2369 rule->rdr_str, rule->rdr_len);
2370 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002371 }
2372 }
2373 else {
2374 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002375 chunk->data += build_logline(s,
2376 chunk->area + chunk->data,
2377 chunk->size - chunk->data,
2378 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002379
2380 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002381 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002382 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002383 }
2384
2385 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002386 memcpy(chunk->area + chunk->data, path, pathlen);
2387 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002388
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002389 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002390 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002391 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002392 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002393 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002394 chunk->area[chunk->data] = '/';
2395 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002396 }
2397
2398 break;
2399 }
2400 case REDIRECT_TYPE_LOCATION:
2401 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002402 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002403 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002404 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002405
2406 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002407 memcpy(chunk->area + chunk->data, rule->rdr_str,
2408 rule->rdr_len);
2409 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002410 }
2411 else {
2412 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002413 chunk->data += build_logline(s,
2414 chunk->area + chunk->data,
2415 chunk->size - chunk->data,
2416 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002417
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002418 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002419 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002420 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002421 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002422 break;
2423 }
2424
2425 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002426 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2427 chunk->data += 14;
2428 memcpy(chunk->area + chunk->data, rule->cookie_str,
2429 rule->cookie_len);
2430 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002431 }
2432
Willy Tarreau19b14122017-02-28 09:48:11 +01002433 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002434 txn->status = rule->code;
2435 /* let's log the request time */
2436 s->logs.tv_request = now;
2437
Christopher Fauletbe821b92017-03-30 11:21:53 +02002438 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002439 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2440 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2441 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002442 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002443 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002444 memcpy(chunk->area + chunk->data,
2445 "\r\nProxy-Connection: keep-alive", 30);
2446 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002447 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002448 memcpy(chunk->area + chunk->data,
2449 "\r\nConnection: keep-alive", 24);
2450 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002451 }
2452 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002453 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2454 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002455 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002456 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002457 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002458 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002459 req->next -= req->sov;
2460 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002461 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002462 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002463 req->msg_state = HTTP_MSG_CLOSED;
2464 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002465 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002466 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002467 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002468 /* let the server side turn to SI_ST_CLO */
2469 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002470 } else {
2471 /* keep-alive not possible */
2472 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002473 memcpy(chunk->area + chunk->data,
2474 "\r\nProxy-Connection: close\r\n\r\n", 29);
2475 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002476 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002477 memcpy(chunk->area + chunk->data,
2478 "\r\nConnection: close\r\n\r\n", 23);
2479 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002480 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002481 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002482 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002483 }
2484
Willy Tarreaue7dff022015-04-03 01:14:29 +02002485 if (!(s->flags & SF_ERR_MASK))
2486 s->flags |= SF_ERR_LOCAL;
2487 if (!(s->flags & SF_FINST_MASK))
2488 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002489
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002490 ret = 1;
2491 leave:
2492 free_trash_chunk(chunk);
2493 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002494}
2495
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002496/* This stream analyser runs all HTTP request processing which is common to
2497 * frontends and backends, which means blocking ACLs, filters, connection-close,
2498 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002499 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002500 * either needs more data or wants to immediately abort the request (eg: deny,
2501 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002502 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002503int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002504{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002505 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002506 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002507 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002508 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002509 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002510 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002511 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002512 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002513
Christopher Faulete0768eb2018-10-03 16:38:02 +02002514 if (IS_HTX_STRM(s))
2515 return htx_process_req_common(s, req, an_bit, px);
2516
Willy Tarreau655dce92009-11-08 13:10:58 +01002517 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002518 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002519 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002520 }
2521
Christopher Faulet45073512018-07-20 10:16:29 +02002522 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 +02002523 now_ms, __FUNCTION__,
2524 s,
2525 req,
2526 req->rex, req->wex,
2527 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002528 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002529 req->analysers);
2530
Willy Tarreau65410832014-04-28 21:25:43 +02002531 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002532 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002533
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002534 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002535 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002536 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002537
Willy Tarreau0b748332014-04-29 00:13:29 +02002538 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002539 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2540 goto return_prx_yield;
2541
Willy Tarreau0b748332014-04-29 00:13:29 +02002542 case HTTP_RULE_RES_CONT:
2543 case HTTP_RULE_RES_STOP: /* nothing to do */
2544 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002545
Willy Tarreau0b748332014-04-29 00:13:29 +02002546 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2547 if (txn->flags & TX_CLTARPIT)
2548 goto tarpit;
2549 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002550
Willy Tarreau0b748332014-04-29 00:13:29 +02002551 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2552 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002553
Willy Tarreau0b748332014-04-29 00:13:29 +02002554 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002555 goto done;
2556
Willy Tarreau0b748332014-04-29 00:13:29 +02002557 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2558 goto return_bad_req;
2559 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002560 }
2561
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002562 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2563 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002564 struct hdr_ctx ctx;
2565
2566 ctx.idx = 0;
2567 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002568 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002569 if (unlikely(http_header_add_tail2(&txn->req,
2570 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002571 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002572 goto return_bad_req;
2573 }
2574 }
2575
2576 }
2577
Willy Tarreau52542592014-04-28 18:33:26 +02002578 /* OK at this stage, we know that the request was accepted according to
2579 * the http-request rules, we can check for the stats. Note that the
2580 * URI is detected *before* the req* rules in order not to be affected
2581 * by a possible reqrep, while they are processed *after* so that a
2582 * reqdeny can still block them. This clearly needs to change in 1.6!
2583 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002584 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002585 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01002586 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002587 txn->status = 500;
2588 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002589 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002590
Willy Tarreaue7dff022015-04-03 01:14:29 +02002591 if (!(s->flags & SF_ERR_MASK))
2592 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002593 goto return_prx_cond;
2594 }
2595
2596 /* parse the whole stats request and extract the relevant information */
2597 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002598 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002599 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002600
Willy Tarreau0b748332014-04-29 00:13:29 +02002601 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2602 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002603
Willy Tarreau0b748332014-04-29 00:13:29 +02002604 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2605 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002606 }
2607
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002608 /* evaluate the req* rules except reqadd */
2609 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002610 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002611 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002612
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002613 if (txn->flags & TX_CLDENY)
2614 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002615
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002616 if (txn->flags & TX_CLTARPIT) {
2617 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002618 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002619 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002620 }
Willy Tarreau06619262006-12-17 08:37:22 +01002621
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002622 /* add request headers from the rule sets in the same order */
2623 list_for_each_entry(wl, &px->req_add, list) {
2624 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002625 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002626 ret = acl_pass(ret);
2627 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2628 ret = !ret;
2629 if (!ret)
2630 continue;
2631 }
2632
Christopher Faulet10079f52018-10-03 15:17:28 +02002633 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002634 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002635 }
2636
Willy Tarreau52542592014-04-28 18:33:26 +02002637
2638 /* Proceed with the stats now. */
Christopher Faulet2571bc62019-03-01 11:44:26 +01002639 if (unlikely(objt_applet(s->target))) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002640 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002641 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +01002642 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002643
Christopher Fauletbcf242a2019-03-01 11:36:26 +01002644 http_handle_expect_hdr(s, req, msg);
2645
Willy Tarreaue7dff022015-04-03 01:14:29 +02002646 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2647 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2648 if (!(s->flags & SF_FINST_MASK))
2649 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002650
Willy Tarreau70730dd2014-04-24 18:06:27 +02002651 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002652 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2653 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002654 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +01002655
2656 req->flags |= CF_SEND_DONTWAIT;
2657 s->flags |= SF_ASSIGNED;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002658 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002659 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002660
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002661 /* check whether we have some ACLs set to redirect this request */
2662 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002663 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002664 int ret;
2665
Willy Tarreau192252e2015-04-04 01:47:55 +02002666 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002667 ret = acl_pass(ret);
2668 if (rule->cond->pol == ACL_COND_UNLESS)
2669 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002670 if (!ret)
2671 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01002672 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002673 if (!http_apply_redirect_rule(rule, s, txn))
2674 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002675 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002676 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002677
Willy Tarreau2be39392010-01-03 17:24:51 +01002678 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2679 * If this happens, then the data will not come immediately, so we must
2680 * send all what we have without waiting. Note that due to the small gain
2681 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002682 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01002683 * itself once used.
2684 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002685 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01002686
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002687 done: /* done with this analyser, continue with next ones that the calling
2688 * points will have set, if any.
2689 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002690 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002691 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
2692 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002693 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002694
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002695 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02002696 /* Allow cookie logging
2697 */
2698 if (s->be->cookie_name || sess->fe->capture_name)
2699 manage_client_side_cookies(s, req);
2700
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002701 /* When a connection is tarpitted, we use the tarpit timeout,
2702 * which may be the same as the connect timeout if unspecified.
2703 * If unset, then set it to zero because we really want it to
2704 * eventually expire. We build the tarpit as an analyser.
2705 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002706 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002707
2708 /* wipe the request out so that we can drop the connection early
2709 * if the client closes first.
2710 */
2711 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002712
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002713 txn->status = http_err_codes[deny_status];
2714
Christopher Faulet0184ea72017-01-05 14:06:34 +01002715 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002716 req->analysers |= AN_REQ_HTTP_TARPIT;
2717 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2718 if (!req->analyse_exp)
2719 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02002720 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +01002721 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002722 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002723 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002724 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002725 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002726 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002727
2728 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002729
2730 /* Allow cookie logging
2731 */
2732 if (s->be->cookie_name || sess->fe->capture_name)
2733 manage_client_side_cookies(s, req);
2734
Willy Tarreau0b748332014-04-29 00:13:29 +02002735 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002736 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002737 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002738 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02002739 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +01002740 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002741 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002742 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002743 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002744 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002745 goto return_prx_cond;
2746
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002747 return_bad_req:
2748 /* We centralize bad requests processing here */
2749 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2750 /* we detected a parsing error. We want to archive this request
2751 * in the dedicated proxy area for later troubleshooting.
2752 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02002753 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002754 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002755
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002756 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002757 txn->req.msg_state = HTTP_MSG_ERROR;
2758 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002759 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002760
Olivier Houcharda798bf52019-03-08 18:52:00 +01002761 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002762 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002763 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002764
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002765 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002766 if (!(s->flags & SF_ERR_MASK))
2767 s->flags |= SF_ERR_PRXCOND;
2768 if (!(s->flags & SF_FINST_MASK))
2769 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002770
Christopher Faulet0184ea72017-01-05 14:06:34 +01002771 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002772 req->analyse_exp = TICK_ETERNITY;
2773 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002774
2775 return_prx_yield:
2776 channel_dont_connect(req);
2777 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002778}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002779
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002780/* This function performs all the processing enabled for the current request.
2781 * It returns 1 if the processing can continue on next analysers, or zero if it
2782 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002783 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002784 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002785int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002786{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002787 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002788 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002789 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02002790 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002791
Christopher Faulete0768eb2018-10-03 16:38:02 +02002792 if (IS_HTX_STRM(s))
2793 return htx_process_request(s, req, an_bit);
2794
Willy Tarreau655dce92009-11-08 13:10:58 +01002795 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002796 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002797 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002798 return 0;
2799 }
2800
Christopher Faulet45073512018-07-20 10:16:29 +02002801 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 +02002802 now_ms, __FUNCTION__,
2803 s,
2804 req,
2805 req->rex, req->wex,
2806 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002807 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002808 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002809
Willy Tarreau59234e92008-11-30 23:51:27 +01002810 /*
2811 * Right now, we know that we have processed the entire headers
2812 * and that unwanted requests have been filtered out. We can do
2813 * whatever we want with the remaining request. Also, now we
2814 * may have separate values for ->fe, ->be.
2815 */
Willy Tarreau06619262006-12-17 08:37:22 +01002816
Willy Tarreau59234e92008-11-30 23:51:27 +01002817 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002818 * If HTTP PROXY is set we simply get remote server address parsing
2819 * incoming request. Note that this requires that a connection is
2820 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01002821 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002822 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002823 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002824 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002825
Willy Tarreau9471b8c2013-12-15 13:31:35 +01002826 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002827 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002828 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002829 txn->req.msg_state = HTTP_MSG_ERROR;
2830 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002831 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002832 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002833
Willy Tarreaue7dff022015-04-03 01:14:29 +02002834 if (!(s->flags & SF_ERR_MASK))
2835 s->flags |= SF_ERR_RESOURCE;
2836 if (!(s->flags & SF_FINST_MASK))
2837 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002838
2839 return 0;
2840 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002841
Willy Tarreau6b952c82018-09-10 17:45:34 +02002842 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02002843 if (url2sa(ci_head(req) + msg->sl.rq.u,
2844 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02002845 &conn->addr.to, NULL) == -1)
2846 goto return_bad_req;
2847
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002848 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02002849 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
2850 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002851 * u_l characters by a single "/".
2852 */
2853 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002854 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002855 char *cur_end = cur_ptr + txn->req.sl.rq.l;
2856 int delta;
2857
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002858 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002859 http_msg_move_end(&txn->req, delta);
2860 cur_end += delta;
2861 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
2862 goto return_bad_req;
2863 }
2864 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002865 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002866 char *cur_end = cur_ptr + txn->req.sl.rq.l;
2867 int delta;
2868
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002869 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002870 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002871 http_msg_move_end(&txn->req, delta);
2872 cur_end += delta;
2873 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
2874 goto return_bad_req;
2875 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002876 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002877
Willy Tarreau59234e92008-11-30 23:51:27 +01002878 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002879 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002880 * Note that doing so might move headers in the request, but
2881 * the fields will stay coherent and the URI will not move.
2882 * This should only be performed in the backend.
2883 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002884 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002885 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002886
William Lallemanda73203e2012-03-12 12:48:57 +01002887 /* add unique-id if "header-unique-id" is specified */
2888
Thierry Fournierf4011dd2016-03-29 17:23:51 +02002889 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01002890 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002891 goto return_bad_req;
2892 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002893 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002894 }
William Lallemanda73203e2012-03-12 12:48:57 +01002895
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002896 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02002897 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01002898 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002899 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01002900 goto return_bad_req;
2901 }
2902
Cyril Bontéb21570a2009-11-29 20:04:48 +01002903 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002904 * 9: add X-Forwarded-For if either the frontend or the backend
2905 * asks for it.
2906 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002907 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02002908 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002909 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
2910 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
2911 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002912 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02002913 /* The header is set to be added only if none is present
2914 * and we found it, so don't do anything.
2915 */
2916 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002917 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002918 /* Add an X-Forwarded-For header unless the source IP is
2919 * in the 'except' network range.
2920 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002921 if ((!sess->fe->except_mask.s_addr ||
2922 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
2923 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01002924 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002925 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01002926 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002927 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002928 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002929 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002930
2931 /* Note: we rely on the backend to get the header name to be used for
2932 * x-forwarded-for, because the header is really meant for the backends.
2933 * However, if the backend did not specify any option, we have to rely
2934 * on the frontend's header name.
2935 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002936 if (s->be->fwdfor_hdr_len) {
2937 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002938 memcpy(trash.area,
2939 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002940 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002941 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002942 memcpy(trash.area,
2943 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002944 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002945 len += snprintf(trash.area + len,
2946 trash.size - len,
2947 ": %d.%d.%d.%d", pn[0], pn[1],
2948 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002949
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002950 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01002951 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002952 }
2953 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002954 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002955 /* FIXME: for the sake of completeness, we should also support
2956 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002957 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002958 int len;
2959 char pn[INET6_ADDRSTRLEN];
2960 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002961 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01002962 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002963
Willy Tarreau59234e92008-11-30 23:51:27 +01002964 /* Note: we rely on the backend to get the header name to be used for
2965 * x-forwarded-for, because the header is really meant for the backends.
2966 * However, if the backend did not specify any option, we have to rely
2967 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002968 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002969 if (s->be->fwdfor_hdr_len) {
2970 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002971 memcpy(trash.area, s->be->fwdfor_hdr_name,
2972 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01002973 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002974 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002975 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
2976 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002977 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002978 len += snprintf(trash.area + len, trash.size - len,
2979 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002980
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002981 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01002982 goto return_bad_req;
2983 }
2984 }
2985
2986 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02002987 * 10: add X-Original-To if either the frontend or the backend
2988 * asks for it.
2989 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002990 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02002991
2992 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002993 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02002994 /* Add an X-Original-To header unless the destination IP is
2995 * in the 'except' network range.
2996 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002997 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02002998
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002999 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003000 ((!sess->fe->except_mask_to.s_addr ||
3001 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3002 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003003 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003004 (((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 +02003005 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003006 int len;
3007 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003008 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003009
3010 /* Note: we rely on the backend to get the header name to be used for
3011 * x-original-to, because the header is really meant for the backends.
3012 * However, if the backend did not specify any option, we have to rely
3013 * on the frontend's header name.
3014 */
3015 if (s->be->orgto_hdr_len) {
3016 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003017 memcpy(trash.area,
3018 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003019 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003020 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003021 memcpy(trash.area,
3022 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003023 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003024 len += snprintf(trash.area + len,
3025 trash.size - len,
3026 ": %d.%d.%d.%d", pn[0], pn[1],
3027 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003028
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003029 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003030 goto return_bad_req;
3031 }
3032 }
3033 }
3034
Willy Tarreau50fc7772012-11-11 22:19:57 +01003035 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3036 * If an "Upgrade" token is found, the header is left untouched in order not to have
3037 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3038 * "Upgrade" is present in the Connection header.
3039 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003040 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003041 unsigned int want_flags = 0;
3042
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003043 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003044 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003045 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003046 want_flags |= TX_CON_CLO_SET;
3047 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003048 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003049 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003050 want_flags |= TX_CON_KAL_SET;
3051 }
3052
3053 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003054 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003055 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003056
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003057
Willy Tarreau522d6c02009-12-06 18:49:18 +01003058 /* If we have no server assigned yet and we're balancing on url_param
3059 * with a POST request, we may be interested in checking the body for
3060 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003061 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003062 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +01003063 s->txn->meth == HTTP_METH_POST &&
3064 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003065 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003066 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003067 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003068 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003069
Christopher Fauletbe821b92017-03-30 11:21:53 +02003070 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3071 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +01003072
Christopher Fauletbe821b92017-03-30 11:21:53 +02003073 /* We expect some data from the client. Unless we know for sure
3074 * we already have a full request, we have to re-enable quick-ack
3075 * in case we previously disabled it, otherwise we might cause
3076 * the client to delay further data.
3077 */
3078 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletbe821b92017-03-30 11:21:53 +02003079 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003080 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau1a18b542018-12-11 16:37:42 +01003081 conn_set_quickack(cli_conn, 1);
Willy Tarreau03945942009-12-22 16:50:27 +01003082
Willy Tarreau59234e92008-11-30 23:51:27 +01003083 /*************************************************************
3084 * OK, that's finished for the headers. We have done what we *
3085 * could. Let's switch to the DATA state. *
3086 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003087 req->analyse_exp = TICK_ETERNITY;
3088 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003089
Willy Tarreau59234e92008-11-30 23:51:27 +01003090 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003091 /* OK let's go on with the BODY now */
3092 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003093
Willy Tarreau59234e92008-11-30 23:51:27 +01003094 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003095 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003096 /* we detected a parsing error. We want to archive this request
3097 * in the dedicated proxy area for later troubleshooting.
3098 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003099 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003100 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003101
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003102 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003103 txn->req.msg_state = HTTP_MSG_ERROR;
3104 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003105 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003106 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003107
Olivier Houcharda798bf52019-03-08 18:52:00 +01003108 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003109 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003110 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003111
Willy Tarreaue7dff022015-04-03 01:14:29 +02003112 if (!(s->flags & SF_ERR_MASK))
3113 s->flags |= SF_ERR_PRXCOND;
3114 if (!(s->flags & SF_FINST_MASK))
3115 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003116 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003117}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003118
Willy Tarreau60b85b02008-11-30 23:28:40 +01003119/* This function is an analyser which processes the HTTP tarpit. It always
3120 * returns zero, at the beginning because it prevents any other processing
3121 * from occurring, and at the end because it terminates the request.
3122 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003123int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003124{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003125 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003126
Christopher Faulete0768eb2018-10-03 16:38:02 +02003127 if (IS_HTX_STRM(s))
3128 return htx_process_tarpit(s, req, an_bit);
3129
Willy Tarreau60b85b02008-11-30 23:28:40 +01003130 /* This connection is being tarpitted. The CLIENT side has
3131 * already set the connect expiration date to the right
3132 * timeout. We just have to check that the client is still
3133 * there and that the timeout has not expired.
3134 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003135 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003136 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003137 !tick_is_expired(req->analyse_exp, now_ms))
3138 return 0;
3139
3140 /* We will set the queue timer to the time spent, just for
3141 * logging purposes. We fake a 500 server error, so that the
3142 * attacker will not suspect his connection has been tarpitted.
3143 * It will not cause trouble to the logs because we can exclude
3144 * the tarpitted connections by filtering on the 'PT' status flags.
3145 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003146 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3147
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003148 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003149 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003150
Christopher Faulet0184ea72017-01-05 14:06:34 +01003151 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003152 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003153
Willy Tarreaue7dff022015-04-03 01:14:29 +02003154 if (!(s->flags & SF_ERR_MASK))
3155 s->flags |= SF_ERR_PRXCOND;
3156 if (!(s->flags & SF_FINST_MASK))
3157 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003158 return 0;
3159}
3160
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003161/* This function is an analyser which waits for the HTTP request body. It waits
3162 * for either the buffer to be full, or the full advertised contents to have
3163 * reached the buffer. It must only be called after the standard HTTP request
3164 * processing has occurred, because it expects the request to be parsed and will
3165 * look for the Expect header. It may send a 100-Continue interim response. It
3166 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3167 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3168 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003169 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003170int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003171{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003172 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003173 struct http_txn *txn = s->txn;
3174 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003175
Christopher Faulete0768eb2018-10-03 16:38:02 +02003176 if (IS_HTX_STRM(s))
3177 return htx_wait_for_request_body(s, req, an_bit);
3178
Christopher Faulet87451fd2019-03-01 11:16:34 +01003179 if (msg->msg_state < HTTP_MSG_BODY)
3180 goto missing_data;
3181
Willy Tarreaud34af782008-11-30 23:36:37 +01003182 /* We have to parse the HTTP request body to find any required data.
3183 * "balance url_param check_post" should have been the only way to get
3184 * into this. We were brought here after HTTP header analysis, so all
3185 * related structures are ready.
3186 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003187 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Christopher Faulet87451fd2019-03-01 11:16:34 +01003188 http_handle_expect_hdr(s, req, msg);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003189
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003190 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003191 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003192 * must save the body in msg->next because it survives buffer
3193 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003194 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003195 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003196
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003197 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003198 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3199 else
3200 msg->msg_state = HTTP_MSG_DATA;
3201 }
3202
Willy Tarreau890988f2014-04-10 11:59:33 +02003203 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3204 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003205 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003206 goto missing_data;
3207
3208 /* OK we have everything we need now */
3209 goto http_end;
3210 }
3211
3212 /* OK here we're parsing a chunked-encoded message */
3213
Willy Tarreau522d6c02009-12-06 18:49:18 +01003214 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003215 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003216 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003217 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003218 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003219 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003220 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003221
Willy Tarreau115acb92009-12-26 13:56:06 +01003222 if (!ret)
3223 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003224 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003225 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003226 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003227 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003228 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003229 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003230 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003231
3232 msg->chunk_len = chunk;
3233 msg->body_len += chunk;
3234
3235 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003236 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003237 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003238 }
3239
Willy Tarreaud98cf932009-12-27 22:54:55 +01003240 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003241 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3242 * for at least a whole chunk or the whole content length bytes after
3243 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003244 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003245 if (msg->msg_state == HTTP_MSG_TRAILERS)
3246 goto http_end;
3247
Willy Tarreaue115b492015-05-01 23:05:14 +02003248 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003249 goto http_end;
3250
3251 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003252 /* we get here if we need to wait for more data. If the buffer is full,
3253 * we have the maximum we can expect.
3254 */
Willy Tarreau23752332018-06-15 14:54:53 +02003255 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003256 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003257
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003258 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003259 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003260 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003261
Willy Tarreaue7dff022015-04-03 01:14:29 +02003262 if (!(s->flags & SF_ERR_MASK))
3263 s->flags |= SF_ERR_CLITO;
3264 if (!(s->flags & SF_FINST_MASK))
3265 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003266 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003267 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003268
3269 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003270 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003271 /* Not enough data. We'll re-use the http-request
3272 * timeout here. Ideally, we should set the timeout
3273 * relative to the accept() date. We just set the
3274 * request timeout once at the beginning of the
3275 * request.
3276 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003277 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003278 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003279 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003280 return 0;
3281 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003282
3283 http_end:
3284 /* The situation will not evolve, so let's give up on the analysis. */
3285 s->logs.tv_request = now; /* update the request timer to reflect full request */
3286 req->analysers &= ~an_bit;
3287 req->analyse_exp = TICK_ETERNITY;
3288 return 1;
3289
3290 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003291 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003292 txn->req.msg_state = HTTP_MSG_ERROR;
3293 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003294 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003295
Willy Tarreaue7dff022015-04-03 01:14:29 +02003296 if (!(s->flags & SF_ERR_MASK))
3297 s->flags |= SF_ERR_PRXCOND;
3298 if (!(s->flags & SF_FINST_MASK))
3299 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003300
Willy Tarreau522d6c02009-12-06 18:49:18 +01003301 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003302 req->analysers &= AN_REQ_FLT_END;
Olivier Houcharda798bf52019-03-08 18:52:00 +01003303 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003304 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003305 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003306 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003307}
3308
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003309/* send a server's name with an outgoing request over an established connection.
3310 * Note: this function is designed to be called once the request has been scheduled
3311 * for being forwarded. This is the reason why it rewinds the buffer before
3312 * proceeding.
3313 */
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003314int http_send_name_header(struct stream *s, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003315
3316 struct hdr_ctx ctx;
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003317 struct http_txn *txn = s->txn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003318 char *hdr_name = be->server_id_hdr_name;
3319 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003320 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003321 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003322 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003323
Christopher Faulet64159df2018-10-24 21:15:35 +02003324 if (IS_HTX_STRM(s))
3325 return htx_send_name_header(s, be, srv_name);
William Lallemandd9e90662012-01-30 17:27:17 +01003326 ctx.idx = 0;
3327
Willy Tarreau211cdec2014-04-17 20:18:08 +02003328 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003329 if (old_o) {
3330 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003331 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003332 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003333 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003334 }
3335
Willy Tarreauf37954d2018-06-15 18:31:02 +02003336 old_i = ci_data(chn);
3337 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 -05003338 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003339 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003340 }
3341
3342 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003343 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003344 memcpy(hdr_val, hdr_name, hdr_name_len);
3345 hdr_val += hdr_name_len;
3346 *hdr_val++ = ':';
3347 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003348 hdr_val += strlcpy2(hdr_val, srv_name,
3349 trash.area + trash.size - hdr_val);
3350 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3351 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003352
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003353 if (old_o) {
3354 /* If this was a forwarded request, we must readjust the amount of
3355 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003356 * variations. Note that the current state is >= HTTP_MSG_BODY,
3357 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003358 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003359 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003360 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003361 txn->req.next -= old_o;
3362 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003363 }
3364
Mark Lamourinec2247f02012-01-04 13:02:01 -05003365 return 0;
3366}
3367
Willy Tarreau610ecce2010-01-04 21:15:02 +01003368/* Terminate current transaction and prepare a new one. This is very tricky
3369 * right now but it works.
3370 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003371void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003372{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003373 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003374 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003375 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003376 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003377 struct connection *srv_conn;
3378 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003379 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003380
Willy Tarreau610ecce2010-01-04 21:15:02 +01003381 /* FIXME: We need a more portable way of releasing a backend's and a
3382 * server's connections. We need a safer way to reinitialize buffer
3383 * flags. We also need a more accurate method for computing per-request
3384 * data.
3385 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003386 cs = objt_cs(s->si[1].end);
3387 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003388
Willy Tarreau4213a112013-12-15 10:25:42 +01003389 /* unless we're doing keep-alive, we want to quickly close the connection
3390 * to the server.
3391 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003392 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003393 !si_conn_ready(&s->si[1]) || !srv_conn->owner) {
Willy Tarreau350f4872014-11-28 14:42:25 +01003394 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3395 si_shutr(&s->si[1]);
3396 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003397 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003398
Willy Tarreaue7dff022015-04-03 01:14:29 +02003399 if (s->flags & SF_BE_ASSIGNED) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003400 _HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003401 if (unlikely(s->srv_conn))
3402 sess_change_server(s, NULL);
3403 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003404
3405 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003406 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003407
Willy Tarreaueee5b512015-04-03 23:46:31 +02003408 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003409 int n;
3410
Willy Tarreaueee5b512015-04-03 23:46:31 +02003411 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003412 if (n < 1 || n > 5)
3413 n = 0;
3414
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003415 if (fe->mode == PR_MODE_HTTP) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003416 _HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003417 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003418 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003419 (be->mode == PR_MODE_HTTP)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003420 _HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3421 _HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003422 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003423 }
3424
3425 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003426 s->logs.bytes_in -= ci_data(&s->req);
3427 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003428
Willy Tarreau66425e32018-07-25 06:55:12 +02003429 /* we may need to know the position in the queue */
3430 pendconn_free(s);
3431
Willy Tarreau610ecce2010-01-04 21:15:02 +01003432 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003433 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003434 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003435 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003436 s->do_log(s);
3437 }
3438
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003439 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003440 stream_stop_content_counters(s);
3441 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003442
Willy Tarreau9efd7452018-05-31 14:48:54 +02003443 /* reset the profiling counter */
3444 s->task->calls = 0;
3445 s->task->cpu_time = 0;
3446 s->task->lat_time = 0;
3447 s->task->call_date = (profiling & HA_PROF_TASKS) ? now_mono_time() : 0;
3448
Willy Tarreau610ecce2010-01-04 21:15:02 +01003449 s->logs.accept_date = date; /* user-visible date for logging */
3450 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003451 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3452 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003453 tv_zero(&s->logs.tv_request);
3454 s->logs.t_queue = -1;
3455 s->logs.t_connect = -1;
3456 s->logs.t_data = -1;
3457 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003458 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3459 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003460
Willy Tarreauf37954d2018-06-15 18:31:02 +02003461 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3462 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003463
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003464 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003465 if (s->flags & SF_CURR_SESS) {
3466 s->flags &= ~SF_CURR_SESS;
Olivier Houcharda798bf52019-03-08 18:52:00 +01003467 _HA_ATOMIC_SUB(&__objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003468 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003469 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003470 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003471 }
3472
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003473 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003474
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003475
3476 /* If we're doing keepalive, first call the mux detach() method
3477 * to let it know we want to detach without freing the connection.
3478 * We then can call si_release_endpoint() to destroy the conn_stream
Willy Tarreau4213a112013-12-15 10:25:42 +01003479 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003480 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houcharda70e1762018-12-13 18:01:00 +01003481 !si_conn_ready(&s->si[1]) ||
3482 (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 +02003483 srv_conn = NULL;
Olivier Houchard985f1392018-11-30 17:31:52 +01003484 else if (!srv_conn->owner) {
3485 srv_conn->owner = s->sess;
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003486 /* Add it unconditionally to the session list, it'll be removed
3487 * later if needed by session_check_idle_conn(), once we'll
3488 * have released the endpoint and know if it no longer has
3489 * attached streams, and so an idling connection
3490 */
Olivier Houchard351411f2018-12-27 17:20:54 +01003491 if (!session_add_conn(s->sess, srv_conn, s->target)) {
3492 srv_conn->owner = NULL;
3493 /* Try to add the connection to the server idle list.
3494 * If it fails, as the connection no longer has an
3495 * owner, it will be destroy later by
3496 * si_release_endpoint(), anyway
3497 */
3498 srv_add_to_idle_list(objt_server(srv_conn->target), srv_conn);
3499 srv_conn = NULL;
3500
3501 }
Olivier Houchard985f1392018-11-30 17:31:52 +01003502 }
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003503 si_release_endpoint(&s->si[1]);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003504 if (srv_conn && srv_conn->owner == s->sess) {
3505 if (session_check_idle_conn(s->sess, srv_conn) != 0)
3506 srv_conn = NULL;
3507 }
3508
Willy Tarreau4213a112013-12-15 10:25:42 +01003509
Willy Tarreau350f4872014-11-28 14:42:25 +01003510 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3511 s->si[1].err_type = SI_ET_NONE;
3512 s->si[1].conn_retries = 0; /* used for logging too */
3513 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003514 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 +01003515 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 +02003516 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 +02003517 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3518 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3519 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003520
Patrick Hemmere3faf022018-08-22 10:02:00 -04003521 hlua_ctx_destroy(s->hlua);
3522 s->hlua = NULL;
3523
Willy Tarreaueee5b512015-04-03 23:46:31 +02003524 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003525 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003526 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003527
3528 if (prev_status == 401 || prev_status == 407) {
3529 /* In HTTP keep-alive mode, if we receive a 401, we still have
3530 * a chance of being able to send the visitor again to the same
3531 * server over the same connection. This is required by some
3532 * broken protocols such as NTLM, and anyway whenever there is
3533 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003534 * it's better to do it (at least it helps with debugging), at
3535 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003536 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003537 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003538 }
3539
Willy Tarreau53f96852016-02-02 18:50:47 +01003540 /* Never ever allow to reuse a connection from a non-reuse backend */
3541 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3542 srv_conn->flags |= CO_FL_PRIVATE;
3543
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003544 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003545 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003546
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003547 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003548 s->req.flags |= CF_NEVER_WAIT;
3549 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003550 }
3551
Willy Tarreau714ea782015-11-25 20:11:11 +01003552 /* we're removing the analysers, we MUST re-enable events detection.
3553 * We don't enable close on the response channel since it's either
3554 * already closed, or in keep-alive with an idle connection handler.
3555 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003556 channel_auto_read(&s->req);
3557 channel_auto_close(&s->req);
3558 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003559
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003560 /* we're in keep-alive with an idle connection, monitor it if not already done */
3561 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003562 srv = objt_server(srv_conn->target);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003563 if (srv) {
3564 if (srv_conn->flags & CO_FL_PRIVATE)
3565 LIST_ADD(&srv->priv_conns[tid], &srv_conn->list);
3566 else if (prev_flags & TX_NOT_FIRST)
3567 /* note: we check the request, not the connection, but
3568 * this is valid for strategies SAFE and AGGR, and in
3569 * case of ALWS, we don't care anyway.
3570 */
3571 LIST_ADD(&srv->safe_conns[tid], &srv_conn->list);
3572 else
3573 LIST_ADD(&srv->idle_conns[tid], &srv_conn->list);
3574 }
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003575 }
Christopher Faulete6006242017-03-10 11:52:44 +01003576 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3577 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003578}
3579
3580
3581/* This function updates the request state machine according to the response
3582 * state machine and buffer flags. It returns 1 if it changes anything (flag
3583 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3584 * it is only used to find when a request/response couple is complete. Both
3585 * this function and its equivalent should loop until both return zero. It
3586 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3587 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003588int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003589{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003590 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003591 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003592 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003593 unsigned int old_state = txn->req.msg_state;
3594
Christopher Faulet4be98032017-07-18 10:48:24 +02003595 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003596 return 0;
3597
3598 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003599 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003600 * We can shut the read side unless we want to abort_on_close,
3601 * or we have a POST request. The issue with POST requests is
3602 * that some browsers still send a CRLF after the request, and
3603 * this CRLF must be read so that it does not remain in the kernel
3604 * buffers, otherwise a close could cause an RST on some systems
3605 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003606 * Note that if we're using keep-alive on the client side, we'd
3607 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003608 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003609 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003610 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003611 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3612 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Christopher Faulet769d0e92019-03-22 14:23:18 +01003613 !(s->be->options & PR_O_ABRT_CLOSE) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003614 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003615 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003616
Willy Tarreau40f151a2012-12-20 12:10:09 +01003617 /* if the server closes the connection, we want to immediately react
3618 * and close the socket to save packets and syscalls.
3619 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003620 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003621
Willy Tarreau7f876a12015-11-18 11:59:55 +01003622 /* In any case we've finished parsing the request so we must
3623 * disable Nagle when sending data because 1) we're not going
3624 * to shut this side, and 2) the server is waiting for us to
3625 * send pending data.
3626 */
3627 chn->flags |= CF_NEVER_WAIT;
3628
Willy Tarreau610ecce2010-01-04 21:15:02 +01003629 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3630 goto wait_other_side;
3631
3632 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3633 /* The server has not finished to respond, so we
3634 * don't want to move in order not to upset it.
3635 */
3636 goto wait_other_side;
3637 }
3638
Willy Tarreau610ecce2010-01-04 21:15:02 +01003639 /* When we get here, it means that both the request and the
3640 * response have finished receiving. Depending on the connection
3641 * mode, we'll have to wait for the last bytes to leave in either
3642 * direction, and sometimes for a close to be effective.
3643 */
3644
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003645 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3646 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003647 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3648 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003649 }
3650 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3651 /* Option forceclose is set, or either side wants to close,
3652 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003653 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003654 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003655 *
3656 * However, there is an exception if the response
3657 * length is undefined. In this case, we need to wait
3658 * the close from the server. The response will be
3659 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003660 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003661 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3662 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3663 goto check_channel_flags;
3664
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003665 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3666 channel_shutr_now(chn);
3667 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003668 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003669 }
3670 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003671 /* The last possible modes are keep-alive and tunnel. Tunnel mode
3672 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003673 */
Willy Tarreau4213a112013-12-15 10:25:42 +01003674 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
3675 channel_auto_read(chn);
3676 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003677 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003678 }
3679
Christopher Faulet4be98032017-07-18 10:48:24 +02003680 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003681 }
3682
3683 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3684 http_msg_closing:
3685 /* nothing else to forward, just waiting for the output buffer
3686 * to be empty and for the shutw_now to take effect.
3687 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003688 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003689 txn->req.msg_state = HTTP_MSG_CLOSED;
3690 goto http_msg_closed;
3691 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003692 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003693 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003694 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003695 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003696 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003697 }
3698
3699 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3700 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01003701 /* if we don't know whether the server will close, we need to hard close */
3702 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
3703 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3704
Willy Tarreau3988d932013-12-27 23:03:08 +01003705 /* see above in MSG_DONE why we only do this in these states */
3706 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3707 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Christopher Faulet769d0e92019-03-22 14:23:18 +01003708 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01003709 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003710 goto wait_other_side;
3711 }
3712
Christopher Faulet4be98032017-07-18 10:48:24 +02003713 check_channel_flags:
3714 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
3715 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
3716 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02003717 txn->req.msg_state = HTTP_MSG_CLOSING;
3718 goto http_msg_closing;
3719 }
3720
3721
Willy Tarreau610ecce2010-01-04 21:15:02 +01003722 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003723 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003724}
3725
3726
3727/* This function updates the response state machine according to the request
3728 * state machine and buffer flags. It returns 1 if it changes anything (flag
3729 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3730 * it is only used to find when a request/response couple is complete. Both
3731 * this function and its equivalent should loop until both return zero. It
3732 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3733 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003734int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003735{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003736 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003737 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003738 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003739 unsigned int old_state = txn->rsp.msg_state;
3740
Christopher Faulet4be98032017-07-18 10:48:24 +02003741 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003742 return 0;
3743
3744 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3745 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003746 * still monitor the server connection for a possible close
3747 * while the request is being uploaded, so we don't disable
3748 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003749 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003750 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003751
3752 if (txn->req.msg_state == HTTP_MSG_ERROR)
3753 goto wait_other_side;
3754
3755 if (txn->req.msg_state < HTTP_MSG_DONE) {
3756 /* The client seems to still be sending data, probably
3757 * because we got an error response during an upload.
3758 * We have the choice of either breaking the connection
3759 * or letting it pass through. Let's do the later.
3760 */
3761 goto wait_other_side;
3762 }
3763
Willy Tarreau610ecce2010-01-04 21:15:02 +01003764 /* When we get here, it means that both the request and the
3765 * response have finished receiving. Depending on the connection
3766 * mode, we'll have to wait for the last bytes to leave in either
3767 * direction, and sometimes for a close to be effective.
3768 */
3769
3770 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3771 /* Server-close mode : shut read and wait for the request
3772 * side to close its output buffer. The caller will detect
3773 * when we're in DONE and the other is in CLOSED and will
3774 * catch that for the final cleanup.
3775 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003776 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
3777 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003778 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003779 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3780 /* Option forceclose is set, or either side wants to close,
3781 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003782 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003783 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003784 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01003785 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003786 channel_shutr_now(chn);
3787 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003788 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003789 }
3790 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003791 /* The last possible modes are keep-alive and tunnel. Tunnel will
3792 * need to forward remaining data. Keep-alive will need to monitor
3793 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003794 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003795 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02003796 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01003797 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
3798 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003799 }
3800
Christopher Faulet4be98032017-07-18 10:48:24 +02003801 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003802 }
3803
3804 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3805 http_msg_closing:
3806 /* nothing else to forward, just waiting for the output buffer
3807 * to be empty and for the shutw_now to take effect.
3808 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003809 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003810 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3811 goto http_msg_closed;
3812 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003813 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02003814 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003815 txn->rsp.msg_state = HTTP_MSG_ERROR;
Olivier Houcharda798bf52019-03-08 18:52:00 +01003816 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003817 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01003818 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003819 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003820 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003821 }
3822
3823 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3824 http_msg_closed:
3825 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01003826 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003827 channel_auto_close(chn);
3828 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003829 goto wait_other_side;
3830 }
3831
Christopher Faulet4be98032017-07-18 10:48:24 +02003832 check_channel_flags:
3833 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
3834 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
3835 /* if we've just closed an output, let's switch */
3836 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3837 goto http_msg_closing;
3838 }
3839
Willy Tarreau610ecce2010-01-04 21:15:02 +01003840 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02003841 /* We force the response to leave immediately if we're waiting for the
3842 * other side, since there is no pending shutdown to push it out.
3843 */
3844 if (!channel_is_empty(chn))
3845 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003846 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003847}
3848
3849
Christopher Faulet894da4c2017-07-18 11:29:07 +02003850/* Resync the request and response state machines. */
3851void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003852{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003853 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02003854#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01003855 int old_req_state = txn->req.msg_state;
3856 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02003857#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01003858
Willy Tarreau610ecce2010-01-04 21:15:02 +01003859 http_sync_req_state(s);
3860 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003861 if (!http_sync_res_state(s))
3862 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003863 if (!http_sync_req_state(s))
3864 break;
3865 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02003866
Christopher Faulet894da4c2017-07-18 11:29:07 +02003867 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
3868 "req->analysers=0x%08x res->analysers=0x%08x\n",
3869 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02003870 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
3871 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02003872 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02003873
3874
Willy Tarreau610ecce2010-01-04 21:15:02 +01003875 /* OK, both state machines agree on a compatible state.
3876 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01003877 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3878 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02003879 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
3880 * means we must abort the request.
3881 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
3882 * corresponding channel.
3883 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
3884 * on the response with server-close mode means we've completed one
3885 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003886 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02003887 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3888 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01003889 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003890 channel_auto_close(&s->req);
3891 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003892 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003893 channel_auto_close(&s->res);
3894 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003895 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02003896 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3897 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01003898 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01003899 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003900 channel_auto_close(&s->res);
3901 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003902 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003903 channel_abort(&s->req);
3904 channel_auto_close(&s->req);
3905 channel_auto_read(&s->req);
3906 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003907 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02003908 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3909 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3910 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3911 s->req.analysers &= AN_REQ_FLT_END;
3912 if (HAS_REQ_DATA_FILTERS(s))
3913 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
3914 }
3915 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3916 s->res.analysers &= AN_RES_FLT_END;
3917 if (HAS_RSP_DATA_FILTERS(s))
3918 s->res.analysers |= AN_RES_FLT_XFER_DATA;
3919 }
3920 channel_auto_close(&s->req);
3921 channel_auto_read(&s->req);
3922 channel_auto_close(&s->res);
3923 channel_auto_read(&s->res);
3924 }
Willy Tarreau4213a112013-12-15 10:25:42 +01003925 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
3926 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003927 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01003928 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3929 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3930 /* server-close/keep-alive: terminate this transaction,
3931 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02003932 * a fresh-new transaction, but only once we're sure there's
3933 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02003934 * another request. They must not hold any pending output data
3935 * and the response buffer must realigned
3936 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01003937 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003938 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02003939 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02003940 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02003941 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02003942 else {
3943 s->req.analysers = AN_REQ_FLT_END;
3944 s->res.analysers = AN_RES_FLT_END;
3945 txn->flags |= TX_WAIT_CLEANUP;
3946 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003947 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003948}
3949
Willy Tarreaud98cf932009-12-27 22:54:55 +01003950/* This function is an analyser which forwards request body (including chunk
3951 * sizes if any). It is called as soon as we must forward, even if we forward
3952 * zero byte. The only situation where it must not be called is when we're in
3953 * tunnel mode and we want to forward till the close. It's used both to forward
3954 * remaining data and to resync after end of body. It expects the msg_state to
3955 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02003956 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01003957 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02003958 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003959 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003960int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01003961{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003962 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003963 struct http_txn *txn = s->txn;
3964 struct http_msg *msg = &s->txn->req;
Christopher Faulet93e02d82019-03-08 14:18:50 +01003965 short status = 0;
Christopher Faulet3e344292015-11-24 16:24:13 +01003966 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003967
Christopher Faulete0768eb2018-10-03 16:38:02 +02003968 if (IS_HTX_STRM(s))
3969 return htx_request_forward_body(s, req, an_bit);
3970
Christopher Faulet45073512018-07-20 10:16:29 +02003971 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 +02003972 now_ms, __FUNCTION__,
3973 s,
3974 req,
3975 req->rex, req->wex,
3976 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003977 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02003978 req->analysers);
3979
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003980 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3981 return 0;
3982
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003983 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003984 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02003985 /* Output closed while we were sending data. We must abort and
3986 * wake the other side up.
3987 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003988 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02003989 msg->msg_state = HTTP_MSG_ERROR;
3990 http_resync_states(s);
Christopher Faulet2f5c7842019-03-19 14:55:11 +01003991 return 1;
Willy Tarreau082b01c2010-01-02 23:58:04 +01003992 }
3993
Willy Tarreaud98cf932009-12-27 22:54:55 +01003994 /* Note that we don't have to send 100-continue back because we don't
3995 * need the data to complete our job, and it's up to the server to
3996 * decide whether to return 100, 417 or anything else in return of
3997 * an "Expect: 100-continue" header.
3998 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02003999 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004000 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4001 ? HTTP_MSG_CHUNK_SIZE
4002 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004003
4004 /* TODO/filters: when http-buffer-request option is set or if a
4005 * rule on url_param exists, the first chunk size could be
4006 * already parsed. In that case, msg->next is after the chunk
4007 * size (including the CRLF after the size). So this case should
4008 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004009 }
4010
Willy Tarreau7ba23542014-04-17 21:50:00 +02004011 /* Some post-connect processing might want us to refrain from starting to
4012 * forward data. Currently, the only reason for this is "balance url_param"
4013 * whichs need to parse/process the request after we've enabled forwarding.
4014 */
4015 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004016 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004017 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004018 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004019 channel_dont_close(req); /* don't fail on early shutr */
4020 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004021 }
4022 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4023 }
4024
Willy Tarreau80a92c02014-03-12 10:41:13 +01004025 /* in most states, we should abort in case of early close */
4026 channel_auto_close(req);
4027
Willy Tarreauefdf0942014-04-24 20:08:57 +02004028 if (req->to_forward) {
4029 /* We can't process the buffer's contents yet */
4030 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004031 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004032 }
4033
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004034 if (msg->msg_state < HTTP_MSG_DONE) {
4035 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4036 ? http_msg_forward_chunked_body(s, msg)
4037 : http_msg_forward_body(s, msg));
4038 if (!ret)
4039 goto missing_data_or_waiting;
4040 if (ret < 0)
4041 goto return_bad_req;
4042 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004043
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004044 /* other states, DONE...TUNNEL */
4045 /* we don't want to forward closes on DONE except in tunnel mode. */
4046 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4047 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004048
Christopher Faulet894da4c2017-07-18 11:29:07 +02004049 http_resync_states(s);
4050 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004051 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4052 if (req->flags & CF_SHUTW) {
4053 /* request errors are most likely due to the
4054 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01004055 goto return_srv_abort;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004056 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004057 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004058 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004059 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004060 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004061 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004062 }
4063
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004064 /* If "option abortonclose" is set on the backend, we want to monitor
4065 * the client's connection and forward any shutdown notification to the
4066 * server, which will decide whether to close or to go on processing the
4067 * request. We only do that in tunnel mode, and not in other modes since
4068 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004069 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004070 channel_auto_read(req);
4071 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4072 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4073 s->si[1].flags |= SI_FL_NOLINGER;
4074 channel_auto_close(req);
4075 }
4076 else if (s->txn->meth == HTTP_METH_POST) {
4077 /* POST requests may require to read extra CRLF sent by broken
4078 * browsers and which could cause an RST to be sent upon close
4079 * on some systems (eg: Linux). */
4080 channel_auto_read(req);
4081 }
4082 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004083
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004084 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004085 /* stop waiting for data if the input is closed before the end */
Christopher Faulet93e02d82019-03-08 14:18:50 +01004086 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
4087 goto return_cli_abort;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004088
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004089 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004090 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004091 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01004092 goto return_srv_abort;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004093
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004094 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004095 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004096 * And when content-length is used, we never want to let the possible
4097 * shutdown be forwarded to the other side, as the state machine will
4098 * take care of it once the client responds. It's also important to
4099 * prevent TIME_WAITs from accumulating on the backend side, and for
4100 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004101 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004102 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004103 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004104
Willy Tarreau5c620922011-05-11 19:56:11 +02004105 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004106 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004107 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004108 * modes are already handled by the stream sock layer. We must not do
4109 * this in content-length mode because it could present the MSG_MORE
4110 * flag with the last block of forwarded data, which would cause an
4111 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004112 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004113 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004114 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004115
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004116 return 0;
4117
Christopher Faulet93e02d82019-03-08 14:18:50 +01004118 return_cli_abort:
4119 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4120 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
4121 if (objt_server(s->target))
4122 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
4123 if (!(s->flags & SF_ERR_MASK))
4124 s->flags |= SF_ERR_CLICL;
4125 status = 400;
4126 goto return_error;
4127
4128 return_srv_abort:
4129 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4130 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
4131 if (objt_server(s->target))
4132 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
4133 if (!(s->flags & SF_ERR_MASK))
4134 s->flags |= SF_ERR_SRVCL;
4135 status = 502;
4136 goto return_error;
4137
4138 return_bad_req: /* let's centralize all bad requests */
Olivier Houcharda798bf52019-03-08 18:52:00 +01004139 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004140 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01004141 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02004142 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01004143 s->flags |= SF_ERR_CLICL;
4144 status = 400;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004145
Christopher Faulet93e02d82019-03-08 14:18:50 +01004146 return_error:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004147 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004148 txn->req.msg_state = HTTP_MSG_ERROR;
Christopher Faulet93e02d82019-03-08 14:18:50 +01004149 if (txn->status > 0) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004150 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004151 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004152 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01004153 txn->status = status;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004154 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004155 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004156 req->analysers &= AN_REQ_FLT_END;
4157 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Faulet93e02d82019-03-08 14:18:50 +01004158 if (!(s->flags & SF_FINST_MASK))
4159 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004160 return 0;
4161}
4162
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004163/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4164 * processing can continue on next analysers, or zero if it either needs more
4165 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004166 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004167 * when it has nothing left to do, and may remove any analyser when it wants to
4168 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004169 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004170int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004171{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004172 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004173 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004174 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004175 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004176 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004177 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004178 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004179 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004180
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004181 srv_conn = cs_conn(objt_cs(s->si[1].end));
4182
Christopher Faulete0768eb2018-10-03 16:38:02 +02004183 if (IS_HTX_STRM(s))
4184 return htx_wait_for_response(s, rep, an_bit);
4185
Christopher Faulet45073512018-07-20 10:16:29 +02004186 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 +02004187 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004188 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004189 rep,
4190 rep->rex, rep->wex,
4191 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004192 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004193 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004194
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004195 /*
4196 * Now parse the partial (or complete) lines.
4197 * We will check the response syntax, and also join multi-line
4198 * headers. An index of all the lines will be elaborated while
4199 * parsing.
4200 *
4201 * For the parsing, we use a 28 states FSM.
4202 *
4203 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004204 * ci_head(rep) = beginning of response
4205 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4206 * ci_tail(rep) = end of input data
4207 * msg->eol = end of current header or line (LF or CRLF)
4208 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004209 */
4210
Willy Tarreau628c40c2014-04-24 19:11:26 +02004211 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004212 /* There's a protected area at the end of the buffer for rewriting
4213 * purposes. We don't want to start to parse the request if the
4214 * protected area is affected, because we may have to move processed
4215 * data later, which is much more complicated.
4216 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004217 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004218 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004219 /* some data has still not left the buffer, wake us once that's done */
4220 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4221 goto abort_response;
4222 channel_dont_close(rep);
4223 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004224 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004225 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004226 }
4227
Willy Tarreau188e2302018-06-15 11:11:53 +02004228 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004229 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004230 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004231
Willy Tarreauf37954d2018-06-15 18:31:02 +02004232 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004233 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004234 }
4235
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004236 /* 1: we might have to print this header in debug mode */
4237 if (unlikely((global.mode & MODE_DEBUG) &&
4238 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004239 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004240 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004241
Willy Tarreauf37954d2018-06-15 18:31:02 +02004242 sol = ci_head(rep);
4243 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004244 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004245
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004246 sol += hdr_idx_first_pos(&txn->hdr_idx);
4247 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004248
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004249 while (cur_idx) {
4250 eol = sol + txn->hdr_idx.v[cur_idx].len;
4251 debug_hdr("srvhdr", s, sol, eol);
4252 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4253 cur_idx = txn->hdr_idx.v[cur_idx].next;
4254 }
4255 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004256
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004257 /*
4258 * Now we quickly check if we have found a full valid response.
4259 * If not so, we check the FD and buffer states before leaving.
4260 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004261 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004262 * responses are checked first.
4263 *
4264 * Depending on whether the client is still there or not, we
4265 * may send an error response back or not. Note that normally
4266 * we should only check for HTTP status there, and check I/O
4267 * errors somewhere else.
4268 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004269
Willy Tarreau655dce92009-11-08 13:10:58 +01004270 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004271 /* Invalid response */
4272 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4273 /* we detected a parsing error. We want to archive this response
4274 * in the dedicated proxy area for later troubleshooting.
4275 */
4276 hdr_response_bad:
4277 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004278 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004279
Olivier Houcharda798bf52019-03-08 18:52:00 +01004280 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004281 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004282 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004283 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004284 }
Willy Tarreau64648412010-03-05 10:41:54 +01004285 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004286 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004287 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004288 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004289 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004290 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004291 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004292
Willy Tarreaue7dff022015-04-03 01:14:29 +02004293 if (!(s->flags & SF_ERR_MASK))
4294 s->flags |= SF_ERR_PRXCOND;
4295 if (!(s->flags & SF_FINST_MASK))
4296 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004297
4298 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004299 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004300
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004301 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004302 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004303 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004304 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004305 goto hdr_response_bad;
4306 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004307
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004308 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004309 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004310 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004311 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004312 else if (txn->flags & TX_NOT_FIRST)
4313 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004314
Olivier Houcharda798bf52019-03-08 18:52:00 +01004315 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004316 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004317 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004318 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004319 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004320
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004321 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004322 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004323 txn->status = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004324
4325 /* Check to see if the server refused the early data.
4326 * If so, just send a 425
4327 */
4328 if (objt_cs(s->si[1].end)) {
4329 struct connection *conn = objt_cs(s->si[1].end)->conn;
4330
4331 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4332 txn->status = 425;
4333 }
4334
Willy Tarreau350f4872014-11-28 14:42:25 +01004335 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004336 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004337 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004338
Willy Tarreaue7dff022015-04-03 01:14:29 +02004339 if (!(s->flags & SF_ERR_MASK))
4340 s->flags |= SF_ERR_SRVCL;
4341 if (!(s->flags & SF_FINST_MASK))
4342 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004343 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004344 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004345
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004346 /* read timeout : return a 504 to the client. */
4347 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004348 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004349 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004350
Olivier Houcharda798bf52019-03-08 18:52:00 +01004351 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004352 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004353 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004354 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004355 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004356
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004357 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004358 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004359 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004360 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004361 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004362 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004363
Willy Tarreaue7dff022015-04-03 01:14:29 +02004364 if (!(s->flags & SF_ERR_MASK))
4365 s->flags |= SF_ERR_SRVTO;
4366 if (!(s->flags & SF_FINST_MASK))
4367 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004368 return 0;
4369 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004370
Willy Tarreauf003d372012-11-26 13:35:37 +01004371 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004372 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004373 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4374 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004375 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004376 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004377
Christopher Faulet0184ea72017-01-05 14:06:34 +01004378 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004379 channel_auto_close(rep);
4380
4381 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004382 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004383 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004384
Willy Tarreaue7dff022015-04-03 01:14:29 +02004385 if (!(s->flags & SF_ERR_MASK))
4386 s->flags |= SF_ERR_CLICL;
4387 if (!(s->flags & SF_FINST_MASK))
4388 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004389
Willy Tarreau87b09662015-04-03 00:22:06 +02004390 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004391 return 0;
4392 }
4393
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004394 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004395 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004396 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004397 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004398 else if (txn->flags & TX_NOT_FIRST)
4399 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004400
Olivier Houcharda798bf52019-03-08 18:52:00 +01004401 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004402 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004403 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004404 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004405 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004406
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004407 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004408 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004409 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004410 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004411 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004412 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004413
Willy Tarreaue7dff022015-04-03 01:14:29 +02004414 if (!(s->flags & SF_ERR_MASK))
4415 s->flags |= SF_ERR_SRVCL;
4416 if (!(s->flags & SF_FINST_MASK))
4417 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004418 return 0;
4419 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004420
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004421 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004422 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004423 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004424 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004425 else if (txn->flags & TX_NOT_FIRST)
4426 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004427
Olivier Houcharda798bf52019-03-08 18:52:00 +01004428 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004429 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004430 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004431
Willy Tarreaue7dff022015-04-03 01:14:29 +02004432 if (!(s->flags & SF_ERR_MASK))
4433 s->flags |= SF_ERR_CLICL;
4434 if (!(s->flags & SF_FINST_MASK))
4435 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004436
Willy Tarreau87b09662015-04-03 00:22:06 +02004437 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004438 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004439 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004440
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004441 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004442 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004443 return 0;
4444 }
4445
4446 /* More interesting part now : we know that we have a complete
4447 * response which at least looks like HTTP. We have an indicator
4448 * of each header's length, so we can parse them quickly.
4449 */
4450
4451 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004452 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004453
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004454 /*
4455 * 1: get the status code
4456 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004457 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004458 if (n < 1 || n > 5)
4459 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004460 /* when the client triggers a 4xx from the server, it's most often due
4461 * to a missing object or permission. These events should be tracked
4462 * because if they happen often, it may indicate a brute force or a
4463 * vulnerability scan.
4464 */
4465 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004466 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004467
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004468 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004469 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004470
Willy Tarreau91852eb2015-05-01 13:26:00 +02004471 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4472 * exactly one digit "." one digit. This check may be disabled using
4473 * option accept-invalid-http-response.
4474 */
4475 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4476 if (msg->sl.st.v_l != 8) {
4477 msg->err_pos = 0;
4478 goto hdr_response_bad;
4479 }
4480
Willy Tarreauf37954d2018-06-15 18:31:02 +02004481 if (ci_head(rep)[4] != '/' ||
4482 !isdigit((unsigned char)ci_head(rep)[5]) ||
4483 ci_head(rep)[6] != '.' ||
4484 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004485 msg->err_pos = 4;
4486 goto hdr_response_bad;
4487 }
4488 }
4489
Willy Tarreau5b154472009-12-21 20:11:07 +01004490 /* check if the response is HTTP/1.1 or above */
4491 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004492 ((ci_head(rep)[5] > '1') ||
4493 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004494 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004495
4496 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004497 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 +01004498
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004499 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004500 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004501
Willy Tarreauf37954d2018-06-15 18:31:02 +02004502 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004503
Willy Tarreau39650402010-03-15 19:44:39 +01004504 /* Adjust server's health based on status code. Note: status codes 501
4505 * and 505 are triggered on demand by client request, so we must not
4506 * count them as server failures.
4507 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004508 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004509 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004510 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004511 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004512 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004513 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004514
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004515 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004516 * We may be facing a 100-continue response, or any other informational
4517 * 1xx response which is non-final, in which case this is not the right
4518 * response, and we're waiting for the next one. Let's allow this response
4519 * to go to the client and wait for the next one. There's an exception for
4520 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004521 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004522 if (txn->status < 200 &&
4523 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004524 hdr_idx_init(&txn->hdr_idx);
4525 msg->next -= channel_forward(rep, msg->next);
4526 msg->msg_state = HTTP_MSG_RPBEFORE;
4527 txn->status = 0;
4528 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004529 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004530 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004531 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004532
Willy Tarreaua14ad722017-07-07 11:36:32 +02004533 /*
4534 * 2: check for cacheability.
4535 */
4536
4537 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004538 case 200:
4539 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004540 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004541 case 206:
4542 case 300:
4543 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004544 case 404:
4545 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004546 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004547 case 414:
4548 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004549 break;
4550 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004551 /* RFC7231#6.1:
4552 * Responses with status codes that are defined as
4553 * cacheable by default (e.g., 200, 203, 204, 206,
4554 * 300, 301, 404, 405, 410, 414, and 501 in this
4555 * specification) can be reused by a cache with
4556 * heuristic expiration unless otherwise indicated
4557 * by the method definition or explicit cache
4558 * controls [RFC7234]; all other status codes are
4559 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004560 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004561 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004562 break;
4563 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004564
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004565 /*
4566 * 3: we may need to capture headers
4567 */
4568 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004569 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004570 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4571 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004572
Willy Tarreau557f1992015-05-01 10:05:17 +02004573 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4574 * by RFC7230#3.3.3 :
4575 *
4576 * The length of a message body is determined by one of the following
4577 * (in order of precedence):
4578 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004579 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4580 * the connection will become a tunnel immediately after the empty
4581 * line that concludes the header fields. A client MUST ignore
4582 * any Content-Length or Transfer-Encoding header fields received
4583 * in such a message. Any 101 response (Switching Protocols) is
4584 * managed in the same manner.
4585 *
4586 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004587 * (Informational), 204 (No Content), or 304 (Not Modified) status
4588 * code is always terminated by the first empty line after the
4589 * header fields, regardless of the header fields present in the
4590 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004591 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004592 * 3. If a Transfer-Encoding header field is present and the chunked
4593 * transfer coding (Section 4.1) is the final encoding, the message
4594 * body length is determined by reading and decoding the chunked
4595 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004596 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004597 * If a Transfer-Encoding header field is present in a response and
4598 * the chunked transfer coding is not the final encoding, the
4599 * message body length is determined by reading the connection until
4600 * it is closed by the server. If a Transfer-Encoding header field
4601 * is present in a request and the chunked transfer coding is not
4602 * the final encoding, the message body length cannot be determined
4603 * reliably; the server MUST respond with the 400 (Bad Request)
4604 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004605 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004606 * If a message is received with both a Transfer-Encoding and a
4607 * Content-Length header field, the Transfer-Encoding overrides the
4608 * Content-Length. Such a message might indicate an attempt to
4609 * perform request smuggling (Section 9.5) or response splitting
4610 * (Section 9.4) and ought to be handled as an error. A sender MUST
4611 * remove the received Content-Length field prior to forwarding such
4612 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004613 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004614 * 4. If a message is received without Transfer-Encoding and with
4615 * either multiple Content-Length header fields having differing
4616 * field-values or a single Content-Length header field having an
4617 * invalid value, then the message framing is invalid and the
4618 * recipient MUST treat it as an unrecoverable error. If this is a
4619 * request message, the server MUST respond with a 400 (Bad Request)
4620 * status code and then close the connection. If this is a response
4621 * message received by a proxy, the proxy MUST close the connection
4622 * to the server, discard the received response, and send a 502 (Bad
4623 * Gateway) response to the client. If this is a response message
4624 * received by a user agent, the user agent MUST close the
4625 * connection to the server and discard the received response.
4626 *
4627 * 5. If a valid Content-Length header field is present without
4628 * Transfer-Encoding, its decimal value defines the expected message
4629 * body length in octets. If the sender closes the connection or
4630 * the recipient times out before the indicated number of octets are
4631 * received, the recipient MUST consider the message to be
4632 * incomplete and close the connection.
4633 *
4634 * 6. If this is a request message and none of the above are true, then
4635 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004636 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004637 * 7. Otherwise, this is a response message without a declared message
4638 * body length, so the message body length is determined by the
4639 * number of octets received prior to the server closing the
4640 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004641 */
4642
4643 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004644 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004645 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004646 * FIXME: should we parse anyway and return an error on chunked encoding ?
4647 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004648 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4649 txn->status == 101)) {
4650 /* Either we've established an explicit tunnel, or we're
4651 * switching the protocol. In both cases, we're very unlikely
4652 * to understand the next protocols. We have to switch to tunnel
4653 * mode, so that we transfer the request and responses then let
4654 * this protocol pass unmodified. When we later implement specific
4655 * parsers for such protocols, we'll want to check the Upgrade
4656 * header which contains information about that protocol for
4657 * responses with status 101 (eg: see RFC2817 about TLS).
4658 */
4659 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4660 msg->flags |= HTTP_MSGF_XFER_LEN;
4661 goto end;
4662 }
4663
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004664 if (txn->meth == HTTP_METH_HEAD ||
4665 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004666 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004667 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004668 goto skip_content_length;
4669 }
4670
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004671 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004672 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004673 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004674 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004675 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4676 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004677 /* bad transfer-encoding (chunked followed by something else) */
4678 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004679 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004680 break;
4681 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004682 }
4683
Willy Tarreau1c913912015-04-30 10:57:51 +02004684 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004685 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02004686 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004687 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02004688 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4689 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02004690 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004691 signed long long cl;
4692
Willy Tarreauad14f752011-09-02 20:33:27 +02004693 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004694 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004695 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004696 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004697
Willy Tarreauad14f752011-09-02 20:33:27 +02004698 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004699 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004700 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004701 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004702
Willy Tarreauad14f752011-09-02 20:33:27 +02004703 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004704 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004705 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004706 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004707
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004708 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004709 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004710 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004711 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004712
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004713 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004714 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004715 }
4716
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004717 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
4718 * 407 (Proxy-Authenticate) responses and set the connection to private
4719 */
4720 if (srv_conn && txn->status == 401) {
4721 /* check for Negotiate/NTLM WWW-Authenticate headers */
4722 ctx.idx = 0;
4723 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
4724 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
4725 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
4726 srv_conn->flags |= CO_FL_PRIVATE;
4727 }
4728 } else if (srv_conn && txn->status == 407) {
4729 /* check for Negotiate/NTLM Proxy-Authenticate headers */
4730 ctx.idx = 0;
4731 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
4732 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
4733 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
4734 srv_conn->flags |= CO_FL_PRIVATE;
4735 }
4736 }
4737
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004738 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01004739 /* Now we have to check if we need to modify the Connection header.
4740 * This is more difficult on the response than it is on the request,
4741 * because we can have two different HTTP versions and we don't know
4742 * how the client will interprete a response. For instance, let's say
4743 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4744 * HTTP/1.1 response without any header. Maybe it will bound itself to
4745 * HTTP/1.0 because it only knows about it, and will consider the lack
4746 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4747 * the lack of header as a keep-alive. Thus we will use two flags
4748 * indicating how a request MAY be understood by the client. In case
4749 * of multiple possibilities, we'll fix the header to be explicit. If
4750 * ambiguous cases such as both close and keepalive are seen, then we
4751 * will fall back to explicit close. Note that we won't take risks with
4752 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004753 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004754 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004755 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02004756 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01004757 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004758
Willy Tarreau60466522010-01-18 19:08:45 +01004759 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02004760 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01004761 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004762
Willy Tarreau60466522010-01-18 19:08:45 +01004763 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02004764 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01004765 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004766 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004767 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004768 }
Willy Tarreau60466522010-01-18 19:08:45 +01004769 else { /* SCL / KAL */
4770 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004771 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004772 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004773 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004774
Willy Tarreau60466522010-01-18 19:08:45 +01004775 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004776 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004777
Willy Tarreau60466522010-01-18 19:08:45 +01004778 /* Some keep-alive responses are converted to Server-close if
4779 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004780 */
Willy Tarreau60466522010-01-18 19:08:45 +01004781 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4782 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004783 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004784 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004785 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004786 }
4787
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004788 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02004789 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02004790 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02004791
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004792 /* end of job, return OK */
4793 rep->analysers &= ~an_bit;
4794 rep->analyse_exp = TICK_ETERNITY;
4795 channel_auto_close(rep);
4796 return 1;
4797
4798 abort_keep_alive:
4799 /* A keep-alive request to the server failed on a network error.
4800 * The client is required to retry. We need to close without returning
4801 * any other information so that the client retries.
4802 */
4803 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004804 rep->analysers &= AN_RES_FLT_END;
4805 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004806 channel_auto_close(rep);
4807 s->logs.logwait = 0;
4808 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004809 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01004810 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004811 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004812 return 0;
4813}
4814
4815/* This function performs all the processing enabled for the current response.
4816 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004817 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004818 * other functions. It works like process_request (see indications above).
4819 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004820int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004821{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004822 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004823 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004824 struct http_msg *msg = &txn->rsp;
4825 struct proxy *cur_proxy;
4826 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004827 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004828
Christopher Faulete0768eb2018-10-03 16:38:02 +02004829 if (IS_HTX_STRM(s))
4830 return htx_process_res_common(s, rep, an_bit, px);
4831
Christopher Faulet45073512018-07-20 10:16:29 +02004832 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 +02004833 now_ms, __FUNCTION__,
4834 s,
4835 rep,
4836 rep->rex, rep->wex,
4837 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004838 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004839 rep->analysers);
4840
4841 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
4842 return 0;
4843
Willy Tarreau70730dd2014-04-24 18:06:27 +02004844 /* The stats applet needs to adjust the Connection header but we don't
4845 * apply any filter there.
4846 */
Willy Tarreau612adb82015-03-10 15:25:54 +01004847 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
4848 rep->analysers &= ~an_bit;
4849 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02004850 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01004851 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02004852
Willy Tarreau58975672014-04-24 21:13:57 +02004853 /*
4854 * We will have to evaluate the filters.
4855 * As opposed to version 1.2, now they will be evaluated in the
4856 * filters order and not in the header order. This means that
4857 * each filter has to be validated among all headers.
4858 *
4859 * Filters are tried with ->be first, then with ->fe if it is
4860 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004861 *
4862 * Maybe we are in resume condiion. In this case I choose the
4863 * "struct proxy" which contains the rule list matching the resume
4864 * pointer. If none of theses "struct proxy" match, I initialise
4865 * the process with the first one.
4866 *
4867 * In fact, I check only correspondance betwwen the current list
4868 * pointer and the ->fe rule list. If it doesn't match, I initialize
4869 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02004870 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004871 if (s->current_rule_list == &sess->fe->http_res_rules)
4872 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004873 else
4874 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02004875 while (1) {
4876 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004877
Willy Tarreau58975672014-04-24 21:13:57 +02004878 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02004879 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004880 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004881
Willy Tarreau51d861a2015-05-22 17:30:48 +02004882 if (ret == HTTP_RULE_RES_BADREQ)
4883 goto return_srv_prx_502;
4884
4885 if (ret == HTTP_RULE_RES_DONE) {
4886 rep->analysers &= ~an_bit;
4887 rep->analyse_exp = TICK_ETERNITY;
4888 return 1;
4889 }
4890 }
4891
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004892 /* we need to be called again. */
4893 if (ret == HTTP_RULE_RES_YIELD) {
4894 channel_dont_close(rep);
4895 return 0;
4896 }
4897
Willy Tarreau58975672014-04-24 21:13:57 +02004898 /* try headers filters */
4899 if (rule_set->rsp_exp != NULL) {
4900 if (apply_filters_to_response(s, rep, rule_set) < 0) {
4901 return_bad_resp:
4902 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004903 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004904 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004905 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01004906 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02004907 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004908 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02004909 txn->status = 502;
4910 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01004911 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004912 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004913 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02004914 if (!(s->flags & SF_ERR_MASK))
4915 s->flags |= SF_ERR_PRXCOND;
4916 if (!(s->flags & SF_FINST_MASK))
4917 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02004918 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004919 }
Willy Tarreau58975672014-04-24 21:13:57 +02004920 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004921
Willy Tarreau58975672014-04-24 21:13:57 +02004922 /* has the response been denied ? */
4923 if (txn->flags & TX_SVDENY) {
4924 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004925 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004926
Olivier Houcharda798bf52019-03-08 18:52:00 +01004927 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
4928 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004929 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01004930 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004931
Willy Tarreau58975672014-04-24 21:13:57 +02004932 goto return_srv_prx_502;
4933 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004934
Willy Tarreau58975672014-04-24 21:13:57 +02004935 /* add response headers from the rule sets in the same order */
4936 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02004937 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004938 break;
Willy Tarreau58975672014-04-24 21:13:57 +02004939 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004940 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02004941 ret = acl_pass(ret);
4942 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4943 ret = !ret;
4944 if (!ret)
4945 continue;
4946 }
Christopher Faulet10079f52018-10-03 15:17:28 +02004947 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02004948 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004949 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004950
Willy Tarreau58975672014-04-24 21:13:57 +02004951 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004952 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02004953 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004954 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02004955 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004956
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004957 /* After this point, this anayzer can't return yield, so we can
4958 * remove the bit corresponding to this analyzer from the list.
4959 *
4960 * Note that the intermediate returns and goto found previously
4961 * reset the analyzers.
4962 */
4963 rep->analysers &= ~an_bit;
4964 rep->analyse_exp = TICK_ETERNITY;
4965
Willy Tarreau58975672014-04-24 21:13:57 +02004966 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02004967 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02004968 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004969
Willy Tarreau58975672014-04-24 21:13:57 +02004970 /*
4971 * Now check for a server cookie.
4972 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02004973 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02004974 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004975
Willy Tarreau58975672014-04-24 21:13:57 +02004976 /*
4977 * Check for cache-control or pragma headers if required.
4978 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01004979 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02004980 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004981
Willy Tarreau58975672014-04-24 21:13:57 +02004982 /*
4983 * Add server cookie in the response if needed
4984 */
4985 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
4986 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004987 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02004988 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
4989 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
4990 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
4991 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
4992 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004993 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02004994 /* the server is known, it's not the one the client requested, or the
4995 * cookie's last seen date needs to be refreshed. We have to
4996 * insert a set-cookie here, except if we want to insert only on POST
4997 * requests and this one isn't. Note that servers which don't have cookies
4998 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01004999 */
Willy Tarreau58975672014-04-24 21:13:57 +02005000 if (!objt_server(s->target)->cookie) {
5001 chunk_printf(&trash,
5002 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5003 s->be->cookie_name);
5004 }
5005 else {
5006 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005007
Willy Tarreau58975672014-04-24 21:13:57 +02005008 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5009 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005010 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5011 s30tob64((date.tv_sec+3) >> 2,
5012 trash.area + trash.data);
5013 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005014
Willy Tarreau58975672014-04-24 21:13:57 +02005015 if (s->be->cookie_maxlife) {
5016 /* emit first_date, which is either the original one or
5017 * the current date.
5018 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005019 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005020 s30tob64(txn->cookie_first_date ?
5021 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005022 (date.tv_sec+3) >> 2,
5023 trash.area + trash.data);
5024 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005025 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005026 }
Willy Tarreau58975672014-04-24 21:13:57 +02005027 chunk_appendf(&trash, "; path=/");
5028 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005029
Willy Tarreau58975672014-04-24 21:13:57 +02005030 if (s->be->cookie_domain)
5031 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005032
Willy Tarreau58975672014-04-24 21:13:57 +02005033 if (s->be->ck_opts & PR_CK_HTTPONLY)
5034 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005035
Willy Tarreau58975672014-04-24 21:13:57 +02005036 if (s->be->ck_opts & PR_CK_SECURE)
5037 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005038
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005039 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005040 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005041
Willy Tarreau58975672014-04-24 21:13:57 +02005042 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005043 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005044 /* the server did not change, only the date was updated */
5045 txn->flags |= TX_SCK_UPDATED;
5046 else
5047 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005048
Willy Tarreau58975672014-04-24 21:13:57 +02005049 /* Here, we will tell an eventual cache on the client side that we don't
5050 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5051 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5052 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005053 */
Willy Tarreau58975672014-04-24 21:13:57 +02005054 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005055
Willy Tarreau58975672014-04-24 21:13:57 +02005056 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005057
Willy Tarreau58975672014-04-24 21:13:57 +02005058 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5059 "Cache-control: private", 22) < 0))
5060 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005061 }
Willy Tarreau58975672014-04-24 21:13:57 +02005062 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005063
Willy Tarreau58975672014-04-24 21:13:57 +02005064 /*
5065 * Check if result will be cacheable with a cookie.
5066 * We'll block the response if security checks have caught
5067 * nasty things such as a cacheable cookie.
5068 */
5069 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5070 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5071 (s->be->options & PR_O_CHK_CACHE)) {
5072 /* we're in presence of a cacheable response containing
5073 * a set-cookie header. We'll block it as requested by
5074 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005075 */
Willy Tarreau58975672014-04-24 21:13:57 +02005076 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01005077 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005078
Olivier Houcharda798bf52019-03-08 18:52:00 +01005079 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5080 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005081 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01005082 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005083
Christopher Faulet767a84b2017-11-24 16:50:31 +01005084 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5085 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005086 send_log(s->be, LOG_ALERT,
5087 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5088 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5089 goto return_srv_prx_502;
5090 }
Willy Tarreau03945942009-12-22 16:50:27 +01005091
Willy Tarreau70730dd2014-04-24 18:06:27 +02005092 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005093 /*
5094 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5095 * If an "Upgrade" token is found, the header is left untouched in order
5096 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005097 * if anything but "Upgrade" is present in the Connection header. We don't
5098 * want to touch any 101 response either since it's switching to another
5099 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005100 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005101 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005102 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005103 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005104
Willy Tarreau58975672014-04-24 21:13:57 +02005105 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5106 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5107 /* we want a keep-alive response here. Keep-alive header
5108 * required if either side is not 1.1.
5109 */
5110 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5111 want_flags |= TX_CON_KAL_SET;
5112 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005113 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005114 /* we want a close response here. Close header required if
5115 * the server is 1.1, regardless of the client.
5116 */
5117 if (msg->flags & HTTP_MSGF_VER_11)
5118 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005119 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005120
Willy Tarreau58975672014-04-24 21:13:57 +02005121 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5122 http_change_connection_header(txn, msg, want_flags);
5123 }
5124
5125 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005126 /* Always enter in the body analyzer */
5127 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5128 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005129
Willy Tarreau58975672014-04-24 21:13:57 +02005130 /* if the user wants to log as soon as possible, without counting
5131 * bytes from the server, then this is the right moment. We have
5132 * to temporarily assign bytes_out to log what we currently have.
5133 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005134 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005135 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5136 s->logs.bytes_out = txn->rsp.eoh;
5137 s->do_log(s);
5138 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005139 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005140 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005141}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005142
Willy Tarreaud98cf932009-12-27 22:54:55 +01005143/* This function is an analyser which forwards response body (including chunk
5144 * sizes if any). It is called as soon as we must forward, even if we forward
5145 * zero byte. The only situation where it must not be called is when we're in
5146 * tunnel mode and we want to forward till the close. It's used both to forward
5147 * remaining data and to resync after end of body. It expects the msg_state to
5148 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005149 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005150 *
5151 * It is capable of compressing response data both in content-length mode and
5152 * in chunked mode. The state machines follows different flows depending on
5153 * whether content-length and chunked modes are used, since there are no
5154 * trailers in content-length :
5155 *
5156 * chk-mode cl-mode
5157 * ,----- BODY -----.
5158 * / \
5159 * V size > 0 V chk-mode
5160 * .--> SIZE -------------> DATA -------------> CRLF
5161 * | | size == 0 | last byte |
5162 * | v final crlf v inspected |
5163 * | TRAILERS -----------> DONE |
5164 * | |
5165 * `----------------------------------------------'
5166 *
5167 * Compression only happens in the DATA state, and must be flushed in final
5168 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5169 * is performed at once on final states for all bytes parsed, or when leaving
5170 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005171 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005172int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005173{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005174 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005175 struct http_txn *txn = s->txn;
5176 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005177 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005178
Christopher Faulete0768eb2018-10-03 16:38:02 +02005179 if (IS_HTX_STRM(s))
5180 return htx_response_forward_body(s, res, an_bit);
5181
Christopher Faulet45073512018-07-20 10:16:29 +02005182 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 +02005183 now_ms, __FUNCTION__,
5184 s,
5185 res,
5186 res->rex, res->wex,
5187 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005188 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005189 res->analysers);
5190
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005191 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5192 return 0;
5193
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005194 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005195 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Faulet93e02d82019-03-08 14:18:50 +01005196 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005197 /* Output closed while we were sending data. We must abort and
5198 * wake the other side up.
5199 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005200 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005201 msg->msg_state = HTTP_MSG_ERROR;
5202 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005203 return 1;
5204 }
5205
Willy Tarreau4fe41902010-06-07 22:27:41 +02005206 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005207 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005208
Christopher Fauletd7c91962015-04-30 11:48:27 +02005209 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005210 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5211 ? HTTP_MSG_CHUNK_SIZE
5212 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005213 }
5214
Willy Tarreauefdf0942014-04-24 20:08:57 +02005215 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005216 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005217 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005218 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005219 }
5220
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005221 if (msg->msg_state < HTTP_MSG_DONE) {
5222 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5223 ? http_msg_forward_chunked_body(s, msg)
5224 : http_msg_forward_body(s, msg));
5225 if (!ret)
5226 goto missing_data_or_waiting;
5227 if (ret < 0)
5228 goto return_bad_res;
5229 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005230
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005231 /* other states, DONE...TUNNEL */
5232 /* for keep-alive we don't want to forward closes on DONE */
5233 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5234 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5235 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005236
Christopher Faulet894da4c2017-07-18 11:29:07 +02005237 http_resync_states(s);
5238 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005239 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5240 if (res->flags & CF_SHUTW) {
5241 /* response errors are most likely due to the
5242 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01005243 goto return_cli_abort;
Willy Tarreau5523b322009-12-29 12:05:52 +01005244 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005245 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005246 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005247 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005248 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005249 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005250 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005251 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005252
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005253 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005254 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01005255 goto return_cli_abort;
Willy Tarreauf003d372012-11-26 13:35:37 +01005256
5257 /* stop waiting for data if the input is closed before the end. If the
5258 * client side was already closed, it means that the client has aborted,
5259 * so we don't want to count this as a server abort. Otherwise it's a
5260 * server abort.
5261 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005262 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005263 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01005264 goto return_cli_abort;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005265 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01005266 if (!ci_data(res))
5267 goto return_srv_abort;
Willy Tarreau40dba092010-03-04 18:14:51 +01005268 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005269
Willy Tarreau40dba092010-03-04 18:14:51 +01005270 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005271 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005272 goto return_bad_res;
5273
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005274 /* When TE: chunked is used, we need to get there again to parse
5275 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005276 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5277 * or if there are filters registered on the stream, we don't want to
5278 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005279 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005280 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005281 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005282 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5283 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005284 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005285
Willy Tarreau5c620922011-05-11 19:56:11 +02005286 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005287 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005288 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005289 * modes are already handled by the stream sock layer. We must not do
5290 * this in content-length mode because it could present the MSG_MORE
5291 * flag with the last block of forwarded data, which would cause an
5292 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005293 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005294 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005295 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005296
Willy Tarreau87b09662015-04-03 00:22:06 +02005297 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005298 return 0;
5299
Christopher Faulet93e02d82019-03-08 14:18:50 +01005300 return_srv_abort:
5301 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
5302 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005303 if (objt_server(s->target))
Christopher Faulet93e02d82019-03-08 14:18:50 +01005304 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
5305 if (!(s->flags & SF_ERR_MASK))
5306 s->flags |= SF_ERR_SRVCL;
5307 goto return_error;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005308
Christopher Faulet93e02d82019-03-08 14:18:50 +01005309 return_cli_abort:
5310 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5311 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005312 if (objt_server(s->target))
Christopher Faulet93e02d82019-03-08 14:18:50 +01005313 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
5314 if (!(s->flags & SF_ERR_MASK))
5315 s->flags |= SF_ERR_CLICL;
5316 goto return_error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005317
Christopher Faulet93e02d82019-03-08 14:18:50 +01005318 return_bad_res:
5319 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
5320 if (objt_server(s->target)) {
5321 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
5322 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
5323 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005324 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01005325 s->flags |= SF_ERR_SRVCL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005326
Christopher Faulet93e02d82019-03-08 14:18:50 +01005327 return_error:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005328 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005329 txn->rsp.msg_state = HTTP_MSG_ERROR;
5330 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005331 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005332 res->analysers &= AN_RES_FLT_END;
5333 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 +02005334 if (!(s->flags & SF_FINST_MASK))
5335 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005336 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005337}
5338
5339
Christopher Faulet10079f52018-10-03 15:17:28 +02005340int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005341{
5342 struct channel *chn = msg->chn;
5343 int ret;
5344
5345 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5346
5347 if (msg->msg_state == HTTP_MSG_ENDING)
5348 goto ending;
5349
5350 /* Neither content-length, nor transfer-encoding was found, so we must
5351 * read the body until the server connection is closed. In that case, we
5352 * eat data as they come. Of course, this happens for response only. */
5353 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005354 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005355 msg->chunk_len += len;
5356 msg->body_len += len;
5357 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005358 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005359 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005360 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005361 msg->next += ret;
5362 msg->chunk_len -= ret;
5363 if (msg->chunk_len) {
5364 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005365 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005366 chn->flags |= CF_WAKE_WRITE;
5367 goto missing_data_or_waiting;
5368 }
5369
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005370 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5371 * always set for a request. */
5372 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5373 /* The server still sending data that should be filtered */
5374 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5375 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005376 msg->msg_state = HTTP_MSG_TUNNEL;
5377 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005378 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005379
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005380 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet297d3e22019-03-22 14:16:14 +01005381 chn->flags |= CF_EOI;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005382
5383 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005384 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005385 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005386 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5387 /* default_ret */ msg->next,
5388 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005389 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005390 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005391 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5392 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005393 if (msg->next)
5394 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005395
Christopher Fauletda02e172015-12-04 09:25:05 +01005396 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5397 /* default_ret */ 1,
5398 /* on_error */ goto error,
5399 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005400 if (msg->msg_state == HTTP_MSG_ENDING)
5401 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005402 return 1;
5403
5404 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005405 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005406 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5407 /* default_ret */ msg->next,
5408 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005409 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005410 msg->next -= ret;
5411 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5412 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005413 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005414 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005415 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005416 return 0;
5417 error:
5418 return -1;
5419}
5420
Christopher Faulet10079f52018-10-03 15:17:28 +02005421int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005422{
5423 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005424 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005425 int ret;
5426
5427 /* Here we have the guarantee to be in one of the following state:
5428 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5429 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5430
Christopher Fauletca874b82018-09-20 11:31:01 +02005431 if (msg->msg_state == HTTP_MSG_ENDING)
5432 goto ending;
5433
5434 /* Don't parse chunks if there is no input data */
5435 if (!ci_data(chn))
5436 goto waiting;
5437
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005438 switch_states:
5439 switch (msg->msg_state) {
5440 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005441 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005442 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005443 /* on_error */ goto error);
5444 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005445 msg->chunk_len -= ret;
5446 if (msg->chunk_len) {
5447 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005448 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005449 chn->flags |= CF_WAKE_WRITE;
5450 goto missing_data_or_waiting;
5451 }
5452
5453 /* nothing left to forward for this chunk*/
5454 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5455 /* fall through for HTTP_MSG_CHUNK_CRLF */
5456
5457 case HTTP_MSG_CHUNK_CRLF:
5458 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005459 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005460 if (ret == 0)
5461 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005462 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005463 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005464 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005465 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005466 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005467 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005468 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005469 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5470 /* fall through for HTTP_MSG_CHUNK_SIZE */
5471
5472 case HTTP_MSG_CHUNK_SIZE:
5473 /* read the chunk size and assign it to ->chunk_len,
5474 * then set ->next to point to the body and switch to
5475 * DATA or TRAILERS state.
5476 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005477 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005478 if (ret == 0)
5479 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005480 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005481 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005482 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005483 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005484 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005485 }
5486
5487 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005488 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005489 msg->chunk_len = chunk;
5490 msg->body_len += chunk;
5491
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005492 if (msg->chunk_len) {
5493 msg->msg_state = HTTP_MSG_DATA;
5494 goto switch_states;
5495 }
5496 msg->msg_state = HTTP_MSG_TRAILERS;
5497 /* fall through for HTTP_MSG_TRAILERS */
5498
5499 case HTTP_MSG_TRAILERS:
5500 ret = http_forward_trailers(msg);
5501 if (ret < 0)
5502 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005503 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5504 /* default_ret */ 1,
5505 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005506 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005507 if (!ret)
5508 goto missing_data_or_waiting;
5509 break;
5510
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005511 default:
5512 /* This should no happen in this function */
5513 goto error;
5514 }
5515
5516 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet297d3e22019-03-22 14:16:14 +01005517 chn->flags |= CF_EOI;
5518
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005519 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005520 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005521 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005522 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005523 /* default_ret */ msg->next,
5524 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005525 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005526 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005527 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5528 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005529 if (msg->next)
5530 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005531
Christopher Fauletda02e172015-12-04 09:25:05 +01005532 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005533 /* default_ret */ 1,
5534 /* on_error */ goto error,
5535 /* on_wait */ goto waiting);
5536 msg->msg_state = HTTP_MSG_DONE;
5537 return 1;
5538
5539 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005540 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005541 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005542 /* default_ret */ msg->next,
5543 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005544 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005545 msg->next -= ret;
5546 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5547 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005548 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005549 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005550 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005551 return 0;
5552
5553 chunk_parsing_error:
5554 if (msg->err_pos >= 0) {
5555 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005556 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005557 msg->msg_state, strm_fe(s));
5558 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005559 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005560 msg, msg->msg_state, s->be);
5561 }
5562 error:
5563 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005564}
5565
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005567/* Iterate the same filter through all request headers.
5568 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005569 * Since it can manage the switch to another backend, it updates the per-proxy
5570 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005571 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005572int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005573{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005574 char *cur_ptr, *cur_end, *cur_next;
5575 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005576 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005577 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005578 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005579
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005580 last_hdr = 0;
5581
Willy Tarreauf37954d2018-06-15 18:31:02 +02005582 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005583 old_idx = 0;
5584
5585 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005586 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005587 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005588 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005589 (exp->action == ACT_ALLOW ||
5590 exp->action == ACT_DENY ||
5591 exp->action == ACT_TARPIT))
5592 return 0;
5593
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005594 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005595 if (!cur_idx)
5596 break;
5597
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005598 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005599 cur_ptr = cur_next;
5600 cur_end = cur_ptr + cur_hdr->len;
5601 cur_next = cur_end + cur_hdr->cr + 1;
5602
5603 /* Now we have one header between cur_ptr and cur_end,
5604 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005605 */
5606
Willy Tarreau15a53a42015-01-21 13:39:42 +01005607 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005608 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005609 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005610 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005611 last_hdr = 1;
5612 break;
5613
5614 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005615 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005616 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005617 break;
5618
5619 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005620 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005621 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005622 break;
5623
5624 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005625 len = exp_replace(trash.area,
5626 trash.size, cur_ptr,
5627 exp->replace, pmatch);
5628 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005629 return -1;
5630
Willy Tarreau6e27be12018-08-22 04:46:47 +02005631 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5632
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005633 /* FIXME: if the user adds a newline in the replacement, the
5634 * index will not be recalculated for now, and the new line
5635 * will not be counted as a new header.
5636 */
5637
5638 cur_end += delta;
5639 cur_next += delta;
5640 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005641 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005642 break;
5643
5644 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005645 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005646 cur_next += delta;
5647
Willy Tarreaufa355d42009-11-29 18:12:29 +01005648 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005649 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5650 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005651 cur_hdr->len = 0;
5652 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005653 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005654 break;
5655
5656 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005657 }
5658
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005659 /* keep the link from this header to next one in case of later
5660 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005661 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005662 old_idx = cur_idx;
5663 }
5664 return 0;
5665}
5666
5667
5668/* Apply the filter to the request line.
5669 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5670 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005671 * Since it can manage the switch to another backend, it updates the per-proxy
5672 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005673 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005674int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005675{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005676 char *cur_ptr, *cur_end;
5677 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005678 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005679 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005680
Willy Tarreau3d300592007-03-18 18:34:41 +01005681 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005682 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005683 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005684 (exp->action == ACT_ALLOW ||
5685 exp->action == ACT_DENY ||
5686 exp->action == ACT_TARPIT))
5687 return 0;
5688 else if (exp->action == ACT_REMOVE)
5689 return 0;
5690
5691 done = 0;
5692
Willy Tarreauf37954d2018-06-15 18:31:02 +02005693 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005694 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005695
5696 /* Now we have the request line between cur_ptr and cur_end */
5697
Willy Tarreau15a53a42015-01-21 13:39:42 +01005698 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005699 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005700 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005701 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005702 done = 1;
5703 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005704
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005705 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005706 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005707 done = 1;
5708 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005709
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005710 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005711 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005712 done = 1;
5713 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005714
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005715 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005716 len = exp_replace(trash.area, trash.size,
5717 cur_ptr, exp->replace, pmatch);
5718 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005719 return -1;
5720
Willy Tarreau6e27be12018-08-22 04:46:47 +02005721 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5722
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005723 /* FIXME: if the user adds a newline in the replacement, the
5724 * index will not be recalculated for now, and the new line
5725 * will not be counted as a new header.
5726 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005727
Willy Tarreaufa355d42009-11-29 18:12:29 +01005728 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005729 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005730 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005731 HTTP_MSG_RQMETH,
5732 cur_ptr, cur_end + 1,
5733 NULL, NULL);
5734 if (unlikely(!cur_end))
5735 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005736
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005737 /* we have a full request and we know that we have either a CR
5738 * or an LF at <ptr>.
5739 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005740 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5741 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005742 /* there is no point trying this regex on headers */
5743 return 1;
5744 }
5745 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005746 return done;
5747}
Willy Tarreau97de6242006-12-27 17:18:38 +01005748
Willy Tarreau58f10d72006-12-04 02:26:12 +01005749
Willy Tarreau58f10d72006-12-04 02:26:12 +01005750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005751/*
Willy Tarreau87b09662015-04-03 00:22:06 +02005752 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005753 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005754 * unparsable request. Since it can manage the switch to another backend, it
5755 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005756 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005757int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005758{
Willy Tarreau192252e2015-04-04 01:47:55 +02005759 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005760 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005761 struct hdr_exp *exp;
5762
5763 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005764 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005765
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005766 /*
5767 * The interleaving of transformations and verdicts
5768 * makes it difficult to decide to continue or stop
5769 * the evaluation.
5770 */
5771
Willy Tarreau6c123b12010-01-28 20:22:06 +01005772 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5773 break;
5774
Willy Tarreau3d300592007-03-18 18:34:41 +01005775 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005776 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005777 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005778 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005779
5780 /* if this filter had a condition, evaluate it now and skip to
5781 * next filter if the condition does not match.
5782 */
5783 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005784 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01005785 ret = acl_pass(ret);
5786 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5787 ret = !ret;
5788
5789 if (!ret)
5790 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005791 }
5792
5793 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005794 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005795 if (unlikely(ret < 0))
5796 return -1;
5797
5798 if (likely(ret == 0)) {
5799 /* The filter did not match the request, it can be
5800 * iterated through all headers.
5801 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01005802 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
5803 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005804 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005805 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005806 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005807}
5808
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005809
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005810/* Delete a value in a header between delimiters <from> and <next> in buffer
5811 * <buf>. The number of characters displaced is returned, and the pointer to
5812 * the first delimiter is updated if required. The function tries as much as
5813 * possible to respect the following principles :
5814 * - replace <from> delimiter by the <next> one unless <from> points to a
5815 * colon, in which case <next> is simply removed
5816 * - set exactly one space character after the new first delimiter, unless
5817 * there are not enough characters in the block being moved to do so.
5818 * - remove unneeded spaces before the previous delimiter and after the new
5819 * one.
5820 *
5821 * It is the caller's responsibility to ensure that :
5822 * - <from> points to a valid delimiter or the colon ;
5823 * - <next> points to a valid delimiter or the final CR/LF ;
5824 * - there are non-space chars before <from> ;
5825 * - there is a CR/LF at or after <next>.
5826 */
Willy Tarreauc5a4fd52018-12-11 11:42:27 +01005827static int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005828{
5829 char *prev = *from;
5830
5831 if (*prev == ':') {
5832 /* We're removing the first value, preserve the colon and add a
5833 * space if possible.
5834 */
Willy Tarreau2235b262016-11-05 15:50:20 +01005835 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005836 next++;
5837 prev++;
5838 if (prev < next)
5839 *prev++ = ' ';
5840
Willy Tarreau2235b262016-11-05 15:50:20 +01005841 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005842 next++;
5843 } else {
5844 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01005845 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005846 prev--;
5847 *from = prev;
5848
5849 /* copy the delimiter and if possible a space if we're
5850 * not at the end of the line.
5851 */
Willy Tarreau2235b262016-11-05 15:50:20 +01005852 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005853 *prev++ = *next++;
5854 if (prev + 1 < next)
5855 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01005856 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005857 next++;
5858 }
5859 }
Willy Tarreaue3128022018-07-12 15:55:34 +02005860 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005861}
5862
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005863/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005864 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005865 * desirable to call it only when needed. This code is quite complex because
5866 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5867 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005868 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005869void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005870{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005871 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005872 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005873 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005874 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005875 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5876 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005877
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005878 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005879 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005880 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005881
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005882 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005883 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005884 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005885
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005886 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005887 hdr_beg = hdr_next;
5888 hdr_end = hdr_beg + cur_hdr->len;
5889 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005890
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005891 /* We have one full header between hdr_beg and hdr_end, and the
5892 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005893 * "Cookie:" headers.
5894 */
5895
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005896 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005897 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005898 old_idx = cur_idx;
5899 continue;
5900 }
5901
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005902 del_from = NULL; /* nothing to be deleted */
5903 preserve_hdr = 0; /* assume we may kill the whole header */
5904
Willy Tarreau58f10d72006-12-04 02:26:12 +01005905 /* Now look for cookies. Conforming to RFC2109, we have to support
5906 * attributes whose name begin with a '$', and associate them with
5907 * the right cookie, if we want to delete this cookie.
5908 * So there are 3 cases for each cookie read :
5909 * 1) it's a special attribute, beginning with a '$' : ignore it.
5910 * 2) it's a server id cookie that we *MAY* want to delete : save
5911 * some pointers on it (last semi-colon, beginning of cookie...)
5912 * 3) it's an application cookie : we *MAY* have to delete a previous
5913 * "special" cookie.
5914 * At the end of loop, if a "special" cookie remains, we may have to
5915 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005916 * *MUST* delete it.
5917 *
5918 * Note: RFC2965 is unclear about the processing of spaces around
5919 * the equal sign in the ATTR=VALUE form. A careful inspection of
5920 * the RFC explicitly allows spaces before it, and not within the
5921 * tokens (attrs or values). An inspection of RFC2109 allows that
5922 * too but section 10.1.3 lets one think that spaces may be allowed
5923 * after the equal sign too, resulting in some (rare) buggy
5924 * implementations trying to do that. So let's do what servers do.
5925 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5926 * allowed quoted strings in values, with any possible character
5927 * after a backslash, including control chars and delimitors, which
5928 * causes parsing to become ambiguous. Browsers also allow spaces
5929 * within values even without quotes.
5930 *
5931 * We have to keep multiple pointers in order to support cookie
5932 * removal at the beginning, middle or end of header without
5933 * corrupting the header. All of these headers are valid :
5934 *
5935 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5936 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5937 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5938 * | | | | | | | | |
5939 * | | | | | | | | hdr_end <--+
5940 * | | | | | | | +--> next
5941 * | | | | | | +----> val_end
5942 * | | | | | +-----------> val_beg
5943 * | | | | +--------------> equal
5944 * | | | +----------------> att_end
5945 * | | +---------------------> att_beg
5946 * | +--------------------------> prev
5947 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005948 */
5949
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005950 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
5951 /* Iterate through all cookies on this line */
5952
5953 /* find att_beg */
5954 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01005955 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005956 att_beg++;
5957
5958 /* find att_end : this is the first character after the last non
5959 * space before the equal. It may be equal to hdr_end.
5960 */
5961 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005962
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005963 while (equal < hdr_end) {
5964 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01005965 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01005966 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005967 continue;
5968 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005969 }
5970
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005971 /* here, <equal> points to '=', a delimitor or the end. <att_end>
5972 * is between <att_beg> and <equal>, both may be identical.
5973 */
5974
5975 /* look for end of cookie if there is an equal sign */
5976 if (equal < hdr_end && *equal == '=') {
5977 /* look for the beginning of the value */
5978 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01005979 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005980 val_beg++;
5981
5982 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02005983 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005984
5985 /* make val_end point to the first white space or delimitor after the value */
5986 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01005987 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005988 val_end--;
5989 } else {
5990 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01005991 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005992
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005993 /* We have nothing to do with attributes beginning with '$'. However,
5994 * they will automatically be removed if a header before them is removed,
5995 * since they're supposed to be linked together.
5996 */
5997 if (*att_beg == '$')
5998 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005999
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006000 /* Ignore cookies with no equal sign */
6001 if (equal == next) {
6002 /* This is not our cookie, so we must preserve it. But if we already
6003 * scheduled another cookie for removal, we cannot remove the
6004 * complete header, but we can remove the previous block itself.
6005 */
6006 preserve_hdr = 1;
6007 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006008 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006009 val_end += delta;
6010 next += delta;
6011 hdr_end += delta;
6012 hdr_next += delta;
6013 cur_hdr->len += delta;
6014 http_msg_move_end(&txn->req, delta);
6015 prev = del_from;
6016 del_from = NULL;
6017 }
6018 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006019 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006020
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006021 /* if there are spaces around the equal sign, we need to
6022 * strip them otherwise we'll get trouble for cookie captures,
6023 * or even for rewrites. Since this happens extremely rarely,
6024 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006025 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006026 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6027 int stripped_before = 0;
6028 int stripped_after = 0;
6029
6030 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006031 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006032 equal += stripped_before;
6033 val_beg += stripped_before;
6034 }
6035
6036 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006037 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006038 val_beg += stripped_after;
6039 stripped_before += stripped_after;
6040 }
6041
6042 val_end += stripped_before;
6043 next += stripped_before;
6044 hdr_end += stripped_before;
6045 hdr_next += stripped_before;
6046 cur_hdr->len += stripped_before;
6047 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006048 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006049 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006050
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006051 /* First, let's see if we want to capture this cookie. We check
6052 * that we don't already have a client side cookie, because we
6053 * can only capture one. Also as an optimisation, we ignore
6054 * cookies shorter than the declared name.
6055 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006056 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6057 (val_end - att_beg >= sess->fe->capture_namelen) &&
6058 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006059 int log_len = val_end - att_beg;
6060
Willy Tarreaubafbe012017-11-24 17:34:44 +01006061 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006062 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006063 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006064 if (log_len > sess->fe->capture_len)
6065 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006066 memcpy(txn->cli_cookie, att_beg, log_len);
6067 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006068 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006069 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006070
Willy Tarreaubca99692010-10-06 19:25:55 +02006071 /* Persistence cookies in passive, rewrite or insert mode have the
6072 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006073 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006074 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006075 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006076 * For cookies in prefix mode, the form is :
6077 *
6078 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006079 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006080 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6081 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6082 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006083 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006084
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006085 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6086 * have the server ID between val_beg and delim, and the original cookie between
6087 * delim+1 and val_end. Otherwise, delim==val_end :
6088 *
6089 * Cookie: NAME=SRV; # in all but prefix modes
6090 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6091 * | || || | |+-> next
6092 * | || || | +--> val_end
6093 * | || || +---------> delim
6094 * | || |+------------> val_beg
6095 * | || +-------------> att_end = equal
6096 * | |+-----------------> att_beg
6097 * | +------------------> prev
6098 * +-------------------------> hdr_beg
6099 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006100
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006101 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006102 for (delim = val_beg; delim < val_end; delim++)
6103 if (*delim == COOKIE_DELIM)
6104 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006105 } else {
6106 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006107 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006108 /* Now check if the cookie contains a date field, which would
6109 * appear after a vertical bar ('|') just after the server name
6110 * and before the delimiter.
6111 */
6112 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6113 if (vbar1) {
6114 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006115 * right is the last seen date. It is a base64 encoded
6116 * 30-bit value representing the UNIX date since the
6117 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006118 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006119 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006120 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006121 if (val_end - vbar1 >= 5) {
6122 val = b64tos30(vbar1);
6123 if (val > 0)
6124 txn->cookie_last_date = val << 2;
6125 }
6126 /* look for a second vertical bar */
6127 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6128 if (vbar1 && (val_end - vbar1 > 5)) {
6129 val = b64tos30(vbar1 + 1);
6130 if (val > 0)
6131 txn->cookie_first_date = val << 2;
6132 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006133 }
6134 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006135
Willy Tarreauf64d1412010-10-07 20:06:11 +02006136 /* if the cookie has an expiration date and the proxy wants to check
6137 * it, then we do that now. We first check if the cookie is too old,
6138 * then only if it has expired. We detect strict overflow because the
6139 * time resolution here is not great (4 seconds). Cookies with dates
6140 * in the future are ignored if their offset is beyond one day. This
6141 * allows an admin to fix timezone issues without expiring everyone
6142 * and at the same time avoids keeping unwanted side effects for too
6143 * long.
6144 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006145 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6146 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006147 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006148 txn->flags &= ~TX_CK_MASK;
6149 txn->flags |= TX_CK_OLD;
6150 delim = val_beg; // let's pretend we have not found the cookie
6151 txn->cookie_first_date = 0;
6152 txn->cookie_last_date = 0;
6153 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006154 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6155 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006156 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006157 txn->flags &= ~TX_CK_MASK;
6158 txn->flags |= TX_CK_EXPIRED;
6159 delim = val_beg; // let's pretend we have not found the cookie
6160 txn->cookie_first_date = 0;
6161 txn->cookie_last_date = 0;
6162 }
6163
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006164 /* Here, we'll look for the first running server which supports the cookie.
6165 * This allows to share a same cookie between several servers, for example
6166 * to dedicate backup servers to specific servers only.
6167 * However, to prevent clients from sticking to cookie-less backup server
6168 * when they have incidentely learned an empty cookie, we simply ignore
6169 * empty cookies and mark them as invalid.
6170 * The same behaviour is applied when persistence must be ignored.
6171 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006172 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006173 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006174
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006175 while (srv) {
6176 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6177 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006178 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006179 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006180 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006181 /* we found the server and we can use it */
6182 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006183 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006184 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006185 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006186 break;
6187 } else {
6188 /* we found a server, but it's down,
6189 * mark it as such and go on in case
6190 * another one is available.
6191 */
6192 txn->flags &= ~TX_CK_MASK;
6193 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006194 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006195 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006196 srv = srv->next;
6197 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006198
Willy Tarreauf64d1412010-10-07 20:06:11 +02006199 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006200 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006201 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006202 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006203 txn->flags |= TX_CK_UNUSED;
6204 else
6205 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006206 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006207
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006208 /* depending on the cookie mode, we may have to either :
6209 * - delete the complete cookie if we're in insert+indirect mode, so that
6210 * the server never sees it ;
6211 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlant5ba80252018-11-15 09:25:36 -08006212 * application cookie so that it does not get accidently removed later,
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006213 * if we're in cookie prefix mode
6214 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006215 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006216 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006217
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006218 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006219 val_end += delta;
6220 next += delta;
6221 hdr_end += delta;
6222 hdr_next += delta;
6223 cur_hdr->len += delta;
6224 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006225
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006226 del_from = NULL;
6227 preserve_hdr = 1; /* we want to keep this cookie */
6228 }
6229 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006230 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006231 del_from = prev;
6232 }
6233 } else {
6234 /* This is not our cookie, so we must preserve it. But if we already
6235 * scheduled another cookie for removal, we cannot remove the
6236 * complete header, but we can remove the previous block itself.
6237 */
6238 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006239
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006240 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006241 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006242 if (att_beg >= del_from)
6243 att_beg += delta;
6244 if (att_end >= del_from)
6245 att_end += delta;
6246 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006247 val_end += delta;
6248 next += delta;
6249 hdr_end += delta;
6250 hdr_next += delta;
6251 cur_hdr->len += delta;
6252 http_msg_move_end(&txn->req, delta);
6253 prev = del_from;
6254 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006255 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006256 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006257
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006258 /* continue with next cookie on this header line */
6259 att_beg = next;
6260 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006261
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006262 /* There are no more cookies on this line.
6263 * We may still have one (or several) marked for deletion at the
6264 * end of the line. We must do this now in two ways :
6265 * - if some cookies must be preserved, we only delete from the
6266 * mark to the end of line ;
6267 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006268 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006269 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006270 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006271 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006272 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006273 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006274 cur_hdr->len += delta;
6275 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006276 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006277
6278 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006279 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6280 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006281 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006282 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006283 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006284 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006285 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006286 }
6287
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006288 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006289 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006290 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006291}
6292
6293
Willy Tarreaua15645d2007-03-18 16:22:39 +01006294/* Iterate the same filter through all response headers contained in <rtr>.
6295 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6296 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006297int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006298{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006299 char *cur_ptr, *cur_end, *cur_next;
6300 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006301 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006302 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006303 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006304
6305 last_hdr = 0;
6306
Willy Tarreauf37954d2018-06-15 18:31:02 +02006307 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006308 old_idx = 0;
6309
6310 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006311 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006312 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006313 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006314 (exp->action == ACT_ALLOW ||
6315 exp->action == ACT_DENY))
6316 return 0;
6317
6318 cur_idx = txn->hdr_idx.v[old_idx].next;
6319 if (!cur_idx)
6320 break;
6321
6322 cur_hdr = &txn->hdr_idx.v[cur_idx];
6323 cur_ptr = cur_next;
6324 cur_end = cur_ptr + cur_hdr->len;
6325 cur_next = cur_end + cur_hdr->cr + 1;
6326
6327 /* Now we have one header between cur_ptr and cur_end,
6328 * and the next header starts at cur_next.
6329 */
6330
Willy Tarreau15a53a42015-01-21 13:39:42 +01006331 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006332 switch (exp->action) {
6333 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006334 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006335 last_hdr = 1;
6336 break;
6337
6338 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006339 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006340 last_hdr = 1;
6341 break;
6342
6343 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006344 len = exp_replace(trash.area,
6345 trash.size, cur_ptr,
6346 exp->replace, pmatch);
6347 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006348 return -1;
6349
Willy Tarreau6e27be12018-08-22 04:46:47 +02006350 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6351
Willy Tarreaua15645d2007-03-18 16:22:39 +01006352 /* FIXME: if the user adds a newline in the replacement, the
6353 * index will not be recalculated for now, and the new line
6354 * will not be counted as a new header.
6355 */
6356
6357 cur_end += delta;
6358 cur_next += delta;
6359 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006360 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006361 break;
6362
6363 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006364 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006365 cur_next += delta;
6366
Willy Tarreaufa355d42009-11-29 18:12:29 +01006367 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006368 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6369 txn->hdr_idx.used--;
6370 cur_hdr->len = 0;
6371 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006372 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006373 break;
6374
6375 }
6376 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006377
6378 /* keep the link from this header to next one in case of later
6379 * removal of next header.
6380 */
6381 old_idx = cur_idx;
6382 }
6383 return 0;
6384}
6385
6386
6387/* Apply the filter to the status line in the response buffer <rtr>.
6388 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6389 * or -1 if a replacement resulted in an invalid status line.
6390 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006391int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006392{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006393 char *cur_ptr, *cur_end;
6394 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006395 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006396 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006397
Willy Tarreau3d300592007-03-18 18:34:41 +01006398 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006399 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006400 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006401 (exp->action == ACT_ALLOW ||
6402 exp->action == ACT_DENY))
6403 return 0;
6404 else if (exp->action == ACT_REMOVE)
6405 return 0;
6406
6407 done = 0;
6408
Willy Tarreauf37954d2018-06-15 18:31:02 +02006409 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006410 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006411
6412 /* Now we have the status line between cur_ptr and cur_end */
6413
Willy Tarreau15a53a42015-01-21 13:39:42 +01006414 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006415 switch (exp->action) {
6416 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006417 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006418 done = 1;
6419 break;
6420
6421 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006422 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006423 done = 1;
6424 break;
6425
6426 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006427 len = exp_replace(trash.area, trash.size,
6428 cur_ptr, exp->replace, pmatch);
6429 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006430 return -1;
6431
Willy Tarreau6e27be12018-08-22 04:46:47 +02006432 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6433
Willy Tarreaua15645d2007-03-18 16:22:39 +01006434 /* FIXME: if the user adds a newline in the replacement, the
6435 * index will not be recalculated for now, and the new line
6436 * will not be counted as a new header.
6437 */
6438
Willy Tarreaufa355d42009-11-29 18:12:29 +01006439 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006440 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006441 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006442 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006443 cur_ptr, cur_end + 1,
6444 NULL, NULL);
6445 if (unlikely(!cur_end))
6446 return -1;
6447
6448 /* we have a full respnse and we know that we have either a CR
6449 * or an LF at <ptr>.
6450 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006451 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006452 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006453 /* there is no point trying this regex on headers */
6454 return 1;
6455 }
6456 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006457 return done;
6458}
6459
6460
6461
6462/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006463 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006464 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6465 * unparsable response.
6466 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006467int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006468{
Willy Tarreau192252e2015-04-04 01:47:55 +02006469 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006470 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006471 struct hdr_exp *exp;
6472
6473 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006474 int ret;
6475
6476 /*
6477 * The interleaving of transformations and verdicts
6478 * makes it difficult to decide to continue or stop
6479 * the evaluation.
6480 */
6481
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006482 if (txn->flags & TX_SVDENY)
6483 break;
6484
Willy Tarreau3d300592007-03-18 18:34:41 +01006485 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006486 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6487 exp->action == ACT_PASS)) {
6488 exp = exp->next;
6489 continue;
6490 }
6491
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006492 /* if this filter had a condition, evaluate it now and skip to
6493 * next filter if the condition does not match.
6494 */
6495 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006496 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006497 ret = acl_pass(ret);
6498 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6499 ret = !ret;
6500 if (!ret)
6501 continue;
6502 }
6503
Willy Tarreaua15645d2007-03-18 16:22:39 +01006504 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006505 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006506 if (unlikely(ret < 0))
6507 return -1;
6508
6509 if (likely(ret == 0)) {
6510 /* The filter did not match the response, it can be
6511 * iterated through all headers.
6512 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006513 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6514 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006515 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006516 }
6517 return 0;
6518}
6519
6520
Willy Tarreaua15645d2007-03-18 16:22:39 +01006521/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006522 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006523 * desirable to call it only when needed. This function is also used when we
6524 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006525 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006526void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006527{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006528 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006529 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006530 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006531 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006532 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006533 char *hdr_beg, *hdr_end, *hdr_next;
6534 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006535
Willy Tarreaua15645d2007-03-18 16:22:39 +01006536 /* Iterate through the headers.
6537 * we start with the start line.
6538 */
6539 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006540 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006541
6542 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6543 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006544 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006545
6546 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006547 hdr_beg = hdr_next;
6548 hdr_end = hdr_beg + cur_hdr->len;
6549 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006550
Willy Tarreau24581ba2010-08-31 22:39:35 +02006551 /* We have one full header between hdr_beg and hdr_end, and the
6552 * next header starts at hdr_next. We're only interested in
6553 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006554 */
6555
Willy Tarreau24581ba2010-08-31 22:39:35 +02006556 is_cookie2 = 0;
6557 prev = hdr_beg + 10;
6558 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006559 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006560 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6561 if (!val) {
6562 old_idx = cur_idx;
6563 continue;
6564 }
6565 is_cookie2 = 1;
6566 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006567 }
6568
Willy Tarreau24581ba2010-08-31 22:39:35 +02006569 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6570 * <prev> points to the colon.
6571 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006572 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006573
Willy Tarreau24581ba2010-08-31 22:39:35 +02006574 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6575 * check-cache is enabled) and we are not interested in checking
6576 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006577 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006578 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006579 return;
6580
Willy Tarreau24581ba2010-08-31 22:39:35 +02006581 /* OK so now we know we have to process this response cookie.
6582 * The format of the Set-Cookie header is slightly different
6583 * from the format of the Cookie header in that it does not
6584 * support the comma as a cookie delimiter (thus the header
6585 * cannot be folded) because the Expires attribute described in
6586 * the original Netscape's spec may contain an unquoted date
6587 * with a comma inside. We have to live with this because
6588 * many browsers don't support Max-Age and some browsers don't
6589 * support quoted strings. However the Set-Cookie2 header is
6590 * clean.
6591 *
6592 * We have to keep multiple pointers in order to support cookie
6593 * removal at the beginning, middle or end of header without
6594 * corrupting the header (in case of set-cookie2). A special
6595 * pointer, <scav> points to the beginning of the set-cookie-av
6596 * fields after the first semi-colon. The <next> pointer points
6597 * either to the end of line (set-cookie) or next unquoted comma
6598 * (set-cookie2). All of these headers are valid :
6599 *
6600 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6601 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6602 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6603 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6604 * | | | | | | | | | |
6605 * | | | | | | | | +-> next hdr_end <--+
6606 * | | | | | | | +------------> scav
6607 * | | | | | | +--------------> val_end
6608 * | | | | | +--------------------> val_beg
6609 * | | | | +----------------------> equal
6610 * | | | +------------------------> att_end
6611 * | | +----------------------------> att_beg
6612 * | +------------------------------> prev
6613 * +-----------------------------------------> hdr_beg
6614 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006615
Willy Tarreau24581ba2010-08-31 22:39:35 +02006616 for (; prev < hdr_end; prev = next) {
6617 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006618
Willy Tarreau24581ba2010-08-31 22:39:35 +02006619 /* find att_beg */
6620 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006621 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006622 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006623
Willy Tarreau24581ba2010-08-31 22:39:35 +02006624 /* find att_end : this is the first character after the last non
6625 * space before the equal. It may be equal to hdr_end.
6626 */
6627 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006628
Willy Tarreau24581ba2010-08-31 22:39:35 +02006629 while (equal < hdr_end) {
6630 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6631 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006632 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006633 continue;
6634 att_end = equal;
6635 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006636
Willy Tarreau24581ba2010-08-31 22:39:35 +02006637 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6638 * is between <att_beg> and <equal>, both may be identical.
6639 */
6640
6641 /* look for end of cookie if there is an equal sign */
6642 if (equal < hdr_end && *equal == '=') {
6643 /* look for the beginning of the value */
6644 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006645 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006646 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006647
Willy Tarreau24581ba2010-08-31 22:39:35 +02006648 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006649 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006650
6651 /* make val_end point to the first white space or delimitor after the value */
6652 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006653 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006654 val_end--;
6655 } else {
6656 /* <equal> points to next comma, semi-colon or EOL */
6657 val_beg = val_end = next = equal;
6658 }
6659
6660 if (next < hdr_end) {
6661 /* Set-Cookie2 supports multiple cookies, and <next> points to
6662 * a colon or semi-colon before the end. So skip all attr-value
6663 * pairs and look for the next comma. For Set-Cookie, since
6664 * commas are permitted in values, skip to the end.
6665 */
6666 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02006667 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006668 else
6669 next = hdr_end;
6670 }
6671
6672 /* Now everything is as on the diagram above */
6673
6674 /* Ignore cookies with no equal sign */
6675 if (equal == val_end)
6676 continue;
6677
6678 /* If there are spaces around the equal sign, we need to
6679 * strip them otherwise we'll get trouble for cookie captures,
6680 * or even for rewrites. Since this happens extremely rarely,
6681 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006682 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006683 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6684 int stripped_before = 0;
6685 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006686
Willy Tarreau24581ba2010-08-31 22:39:35 +02006687 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006688 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006689 equal += stripped_before;
6690 val_beg += stripped_before;
6691 }
6692
6693 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006694 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006695 val_beg += stripped_after;
6696 stripped_before += stripped_after;
6697 }
6698
6699 val_end += stripped_before;
6700 next += stripped_before;
6701 hdr_end += stripped_before;
6702 hdr_next += stripped_before;
6703 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006704 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006705 }
6706
6707 /* First, let's see if we want to capture this cookie. We check
6708 * that we don't already have a server side cookie, because we
6709 * can only capture one. Also as an optimisation, we ignore
6710 * cookies shorter than the declared name.
6711 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006712 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006713 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006714 (val_end - att_beg >= sess->fe->capture_namelen) &&
6715 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006716 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006717 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006718 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01006719 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006720 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006721 if (log_len > sess->fe->capture_len)
6722 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01006723 memcpy(txn->srv_cookie, att_beg, log_len);
6724 txn->srv_cookie[log_len] = 0;
6725 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006726 }
6727
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006728 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006729 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006730 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006731 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6732 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006733 /* assume passive cookie by default */
6734 txn->flags &= ~TX_SCK_MASK;
6735 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006736
6737 /* If the cookie is in insert mode on a known server, we'll delete
6738 * this occurrence because we'll insert another one later.
6739 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006740 * a direct access.
6741 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006742 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006743 /* The "preserve" flag was set, we don't want to touch the
6744 * server's cookie.
6745 */
6746 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006747 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006748 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006749 /* this cookie must be deleted */
6750 if (*prev == ':' && next == hdr_end) {
6751 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006752 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006753 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6754 txn->hdr_idx.used--;
6755 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006756 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006757 hdr_next += delta;
6758 http_msg_move_end(&txn->rsp, delta);
6759 /* note: while both invalid now, <next> and <hdr_end>
6760 * are still equal, so the for() will stop as expected.
6761 */
6762 } else {
6763 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006764 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006765 next = prev;
6766 hdr_end += delta;
6767 hdr_next += delta;
6768 cur_hdr->len += delta;
6769 http_msg_move_end(&txn->rsp, delta);
6770 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006771 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006772 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006773 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006774 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006775 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006776 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006777 * with this server since we know it.
6778 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006779 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006780 next += delta;
6781 hdr_end += delta;
6782 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006783 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006784 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006785
Willy Tarreauf1348312010-10-07 15:54:11 +02006786 txn->flags &= ~TX_SCK_MASK;
6787 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006788 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006789 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006790 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006791 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006792 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006793 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006794 next += delta;
6795 hdr_end += delta;
6796 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006797 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006798 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006799
Willy Tarreau827aee92011-03-10 16:55:02 +01006800 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006801 txn->flags &= ~TX_SCK_MASK;
6802 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006803 }
6804 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006805 /* that's done for this cookie, check the next one on the same
6806 * line when next != hdr_end (only if is_cookie2).
6807 */
6808 }
6809 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006810 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006811 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006812}
6813
6814
Willy Tarreaua15645d2007-03-18 16:22:39 +01006815/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006816 * Parses the Cache-Control and Pragma request header fields to determine if
6817 * the request may be served from the cache and/or if it is cacheable. Updates
6818 * s->txn->flags.
6819 */
6820void check_request_for_cacheability(struct stream *s, struct channel *chn)
6821{
6822 struct http_txn *txn = s->txn;
6823 char *p1, *p2;
6824 char *cur_ptr, *cur_end, *cur_next;
6825 int pragma_found;
6826 int cc_found;
6827 int cur_idx;
6828
Christopher Faulet25a02f62018-10-24 12:00:25 +02006829 if (IS_HTX_STRM(s))
6830 return htx_check_request_for_cacheability(s, chn);
6831
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006832 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
6833 return; /* nothing more to do here */
6834
6835 cur_idx = 0;
6836 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006837 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006838
6839 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6840 struct hdr_idx_elem *cur_hdr;
6841 int val;
6842
6843 cur_hdr = &txn->hdr_idx.v[cur_idx];
6844 cur_ptr = cur_next;
6845 cur_end = cur_ptr + cur_hdr->len;
6846 cur_next = cur_end + cur_hdr->cr + 1;
6847
6848 /* We have one full header between cur_ptr and cur_end, and the
6849 * next header starts at cur_next.
6850 */
6851
6852 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6853 if (val) {
6854 if ((cur_end - (cur_ptr + val) >= 8) &&
6855 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6856 pragma_found = 1;
6857 continue;
6858 }
6859 }
6860
William Lallemand8a16fe02018-05-22 11:04:33 +02006861 /* Don't use the cache and don't try to store if we found the
6862 * Authorization header */
6863 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
6864 if (val) {
6865 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6866 txn->flags |= TX_CACHE_IGNORE;
6867 continue;
6868 }
6869
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006870 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6871 if (!val)
6872 continue;
6873
6874 /* OK, right now we know we have a cache-control header at cur_ptr */
6875 cc_found = 1;
6876 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
6877
6878 if (p1 >= cur_end) /* no more info */
6879 continue;
6880
6881 /* p1 is at the beginning of the value */
6882 p2 = p1;
6883 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
6884 p2++;
6885
6886 /* we have a complete value between p1 and p2. We don't check the
6887 * values after max-age, max-stale nor min-fresh, we simply don't
6888 * use the cache when they're specified.
6889 */
6890 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
6891 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
6892 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
6893 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
6894 txn->flags |= TX_CACHE_IGNORE;
6895 continue;
6896 }
6897
6898 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
6899 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6900 continue;
6901 }
6902 }
6903
6904 /* RFC7234#5.4:
6905 * When the Cache-Control header field is also present and
6906 * understood in a request, Pragma is ignored.
6907 * When the Cache-Control header field is not present in a
6908 * request, caches MUST consider the no-cache request
6909 * pragma-directive as having the same effect as if
6910 * "Cache-Control: no-cache" were present.
6911 */
6912 if (!cc_found && pragma_found)
6913 txn->flags |= TX_CACHE_IGNORE;
6914}
6915
6916/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01006917 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006918 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006919void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006920{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006921 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006922 char *p1, *p2;
6923
6924 char *cur_ptr, *cur_end, *cur_next;
6925 int cur_idx;
6926
Christopher Faulet25a02f62018-10-24 12:00:25 +02006927
6928 if (IS_HTX_STRM(s))
6929 return htx_check_response_for_cacheability(s, rtr);
6930
Willy Tarreau12b32f22017-12-21 16:08:09 +01006931 if (txn->status < 200) {
6932 /* do not try to cache interim responses! */
6933 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006934 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01006935 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006936
6937 /* Iterate through the headers.
6938 * we start with the start line.
6939 */
6940 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006941 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006942
6943 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6944 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006945 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006946
6947 cur_hdr = &txn->hdr_idx.v[cur_idx];
6948 cur_ptr = cur_next;
6949 cur_end = cur_ptr + cur_hdr->len;
6950 cur_next = cur_end + cur_hdr->cr + 1;
6951
6952 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01006953 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006954 */
6955
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006956 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6957 if (val) {
6958 if ((cur_end - (cur_ptr + val) >= 8) &&
6959 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6960 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6961 return;
6962 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006963 }
6964
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006965 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6966 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006967 continue;
6968
6969 /* OK, right now we know we have a cache-control header at cur_ptr */
6970
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006971 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006972
6973 if (p1 >= cur_end) /* no more info */
6974 continue;
6975
6976 /* p1 is at the beginning of the value */
6977 p2 = p1;
6978
Willy Tarreau8f8e6452007-06-17 21:51:38 +02006979 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006980 p2++;
6981
6982 /* we have a complete value between p1 and p2 */
6983 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01006984 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
6985 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
6986 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6987 continue;
6988 }
6989
Willy Tarreaua15645d2007-03-18 16:22:39 +01006990 /* we have something of the form no-cache="set-cookie" */
6991 if ((cur_end - p1 >= 21) &&
6992 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
6993 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01006994 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006995 continue;
6996 }
6997
6998 /* OK, so we know that either p2 points to the end of string or to a comma */
6999 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007000 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007001 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007002 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007003 return;
7004 }
7005
7006 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007007 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007008 continue;
7009 }
7010 }
7011}
7012
Willy Tarreau58f10d72006-12-04 02:26:12 +01007013
Willy Tarreaub2513902006-12-17 14:52:38 +01007014/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007015 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007016 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007017 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007018 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007019 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007020 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007021 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007022 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007023int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007024{
7025 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007026 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007027 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007028
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007029 if (!uri_auth)
7030 return 0;
7031
Cyril Bonté70be45d2010-10-12 00:14:35 +02007032 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007033 return 0;
7034
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007035 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007036 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007037 return 0;
7038
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007039 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007040 return 0;
7041
Willy Tarreaub2513902006-12-17 14:52:38 +01007042 return 1;
7043}
7044
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007045/* Append the description of what is present in error snapshot <es> into <out>.
7046 * The description must be small enough to always fit in a trash. The output
7047 * buffer may be the trash so the trash must not be used inside this function.
7048 */
7049void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7050{
Christopher Fauleted26fb82018-11-29 22:53:30 +01007051 chunk_appendf(out,
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007052 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7053 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7054 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7055 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7056 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7057 es->ctx.http.m_flags, es->ctx.http.m_clen,
7058 es->ctx.http.m_blen, es->ctx.http.b_flags);
7059}
7060
Willy Tarreau4076a152009-04-02 15:18:36 +02007061/*
7062 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007063 * By default it tries to report the error position as msg->err_pos. However if
7064 * this one is not set, it will then report msg->next, which is the last known
7065 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007066 * displays buffers as a contiguous area starting at buf->p. The direction is
7067 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007068 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007069void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007070 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007071 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007072{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007073 union error_snapshot_ctx ctx;
7074 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007075
Willy Tarreauef3ca732018-09-07 15:47:35 +02007076 /* http-specific part now */
7077 ctx.http.sid = s->uniq_id;
7078 ctx.http.state = state;
7079 ctx.http.b_flags = msg->chn->flags;
7080 ctx.http.s_flags = s->flags;
7081 ctx.http.t_flags = s->txn->flags;
7082 ctx.http.m_flags = msg->flags;
7083 ctx.http.m_clen = msg->chunk_len;
7084 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007085
Willy Tarreauef3ca732018-09-07 15:47:35 +02007086 ofs = msg->chn->total - ci_data(msg->chn);
7087 if (ofs < 0)
7088 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007089
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007090 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007091 other_end, s->target,
7092 strm_sess(s), &msg->chn->buf,
7093 ofs, co_data(msg->chn),
7094 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7095 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007096}
Willy Tarreaub2513902006-12-17 14:52:38 +01007097
Willy Tarreaubaaee002006-06-26 02:48:02 +02007098/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007099 * Print a debug line with a header. Always stop at the first CR or LF char,
7100 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7101 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007102 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007103void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007104{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007105 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007106 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007107
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007108 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007109 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007110 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007111 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 +02007112
7113 for (max = 0; start + max < end; max++)
7114 if (start[max] == '\r' || start[max] == '\n')
7115 break;
7116
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007117 UBOUND(max, trash.size - trash.data - 3);
7118 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7119 trash.area[trash.data++] = '\n';
7120 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007121}
7122
Willy Tarreaueee5b512015-04-03 23:46:31 +02007123
7124/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7125 * The hdr_idx is allocated as well. In case of allocation failure, everything
7126 * allocated is freed and NULL is returned. Otherwise the new transaction is
7127 * assigned to the stream and returned.
7128 */
7129struct http_txn *http_alloc_txn(struct stream *s)
7130{
7131 struct http_txn *txn = s->txn;
7132
7133 if (txn)
7134 return txn;
7135
Willy Tarreaubafbe012017-11-24 17:34:44 +01007136 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007137 if (!txn)
7138 return txn;
7139
7140 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007141 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007142 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007143 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007144 return NULL;
7145 }
7146
7147 s->txn = txn;
7148 return txn;
7149}
7150
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007151void http_txn_reset_req(struct http_txn *txn)
7152{
7153 txn->req.flags = 0;
7154 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7155 txn->req.next = 0;
7156 txn->req.chunk_len = 0LL;
7157 txn->req.body_len = 0LL;
7158 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7159}
7160
7161void http_txn_reset_res(struct http_txn *txn)
7162{
7163 txn->rsp.flags = 0;
7164 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7165 txn->rsp.next = 0;
7166 txn->rsp.chunk_len = 0LL;
7167 txn->rsp.body_len = 0LL;
7168 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7169}
7170
Willy Tarreau0937bc42009-12-22 15:03:09 +01007171/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007172 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007173 * the required fields are properly allocated and that we only need to (re)init
7174 * them. This should be used before processing any new request.
7175 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007176void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007177{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007178 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007179 struct proxy *fe = strm_fe(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02007180 struct conn_stream *cs = objt_cs(s->si[0].end);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007181
Christopher Fauletf2824e62018-10-01 12:12:37 +02007182 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
7183 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
7184 : 0);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007185 txn->status = -1;
Willy Tarreauc9036c02019-01-11 19:38:25 +01007186 *(unsigned int *)txn->cache_hash = 0;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007187
Willy Tarreauf64d1412010-10-07 20:06:11 +02007188 txn->cookie_first_date = 0;
7189 txn->cookie_last_date = 0;
7190
Willy Tarreaueee5b512015-04-03 23:46:31 +02007191 txn->srv_cookie = NULL;
7192 txn->cli_cookie = NULL;
7193 txn->uri = NULL;
7194
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007195 http_txn_reset_req(txn);
7196 http_txn_reset_res(txn);
7197
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007198 txn->req.chn = &s->req;
7199 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007200
7201 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007202
7203 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7204 if (fe->options2 & PR_O2_REQBUG_OK)
7205 txn->req.err_pos = -1; /* let buggy requests pass */
7206
Willy Tarreau0937bc42009-12-22 15:03:09 +01007207 if (txn->hdr_idx.v)
7208 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007209
7210 vars_init(&s->vars_txn, SCOPE_TXN);
7211 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007212}
7213
7214/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007215void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007216{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007217 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007218 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007219
7220 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007221 pool_free(pool_head_requri, txn->uri);
7222 pool_free(pool_head_capture, txn->cli_cookie);
7223 pool_free(pool_head_capture, txn->srv_cookie);
7224 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007225
William Lallemanda73203e2012-03-12 12:48:57 +01007226 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007227 txn->uri = NULL;
7228 txn->srv_cookie = NULL;
7229 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007230
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007231 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007232 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007233 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007234 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007235 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007236 }
7237
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007238 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007239 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007240 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007241 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007242 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007243 }
7244
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007245 if (!LIST_ISEMPTY(&s->vars_txn.head))
7246 vars_prune(&s->vars_txn, s->sess, s);
7247 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7248 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007249}
7250
7251/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007252void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007253{
7254 http_end_txn(s);
7255 http_init_txn(s);
7256
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007257 /* reinitialise the current rule list pointer to NULL. We are sure that
7258 * any rulelist match the NULL pointer.
7259 */
7260 s->current_rule_list = NULL;
7261
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007262 s->be = strm_fe(s);
7263 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007264 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007265 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007266 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007267 /* re-init store persistence */
7268 s->store_count = 0;
Olivier Houcharda798bf52019-03-08 18:52:00 +01007269 s->uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007270
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007271 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007272
Willy Tarreau739cfba2010-01-25 23:11:14 +01007273 /* We must trim any excess data from the response buffer, because we
7274 * may have blocked an invalid response from a server that we don't
Joseph Herlant5ba80252018-11-15 09:25:36 -08007275 * want to accidently forward once we disable the analysers, nor do
Willy Tarreau739cfba2010-01-25 23:11:14 +01007276 * we want those data to come along with next response. A typical
7277 * example of such data would be from a buggy server responding to
7278 * a HEAD with some data, or sending more than the advertised
7279 * content-length.
7280 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007281 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007282 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007283
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007284 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007285 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007286
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007287 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007288 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007289
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007290 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007291 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007292
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007293 s->req.rex = TICK_ETERNITY;
7294 s->req.wex = TICK_ETERNITY;
7295 s->req.analyse_exp = TICK_ETERNITY;
7296 s->res.rex = TICK_ETERNITY;
7297 s->res.wex = TICK_ETERNITY;
7298 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007299 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007300}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007301
Willy Tarreau79e57332018-10-02 16:01:16 +02007302/* This function executes one of the set-{method,path,query,uri} actions. It
7303 * takes the string from the variable 'replace' with length 'len', then modifies
7304 * the relevant part of the request line accordingly. Then it updates various
7305 * pointers to the next elements which were moved, and the total buffer length.
7306 * It finds the action to be performed in p[2], previously filled by function
7307 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7308 * error, though this can be revisited when this code is finally exploited.
7309 *
7310 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7311 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007312 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007313 * In query string case, the mark question '?' must be set at the start of the
7314 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007315 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007316int http_replace_req_line(int action, const char *replace, int len,
7317 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007318{
Willy Tarreau79e57332018-10-02 16:01:16 +02007319 struct http_txn *txn = s->txn;
7320 char *cur_ptr, *cur_end;
7321 int offset = 0;
7322 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007323
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007324 if (IS_HTX_STRM(s))
7325 return htx_req_replace_stline(action, replace, len, px, s);
7326
Willy Tarreau79e57332018-10-02 16:01:16 +02007327 switch (action) {
7328 case 0: // method
7329 cur_ptr = ci_head(&s->req);
7330 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007331
Willy Tarreau79e57332018-10-02 16:01:16 +02007332 /* adjust req line offsets and lengths */
7333 delta = len - offset - (cur_end - cur_ptr);
7334 txn->req.sl.rq.m_l += delta;
7335 txn->req.sl.rq.u += delta;
7336 txn->req.sl.rq.v += delta;
7337 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007338
Willy Tarreau79e57332018-10-02 16:01:16 +02007339 case 1: // path
7340 cur_ptr = http_txn_get_path(txn);
7341 if (!cur_ptr)
7342 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007343
Willy Tarreau79e57332018-10-02 16:01:16 +02007344 cur_end = cur_ptr;
7345 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7346 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007347
Willy Tarreau79e57332018-10-02 16:01:16 +02007348 /* adjust req line offsets and lengths */
7349 delta = len - offset - (cur_end - cur_ptr);
7350 txn->req.sl.rq.u_l += delta;
7351 txn->req.sl.rq.v += delta;
7352 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007353
Willy Tarreau79e57332018-10-02 16:01:16 +02007354 case 2: // query
7355 offset = 1;
7356 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7357 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7358 while (cur_ptr < cur_end && *cur_ptr != '?')
7359 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007360
Willy Tarreau79e57332018-10-02 16:01:16 +02007361 /* skip the question mark or indicate that we must insert it
7362 * (but only if the format string is not empty then).
7363 */
7364 if (cur_ptr < cur_end)
7365 cur_ptr++;
7366 else if (len > 1)
7367 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007368
Willy Tarreau79e57332018-10-02 16:01:16 +02007369 /* adjust req line offsets and lengths */
7370 delta = len - offset - (cur_end - cur_ptr);
7371 txn->req.sl.rq.u_l += delta;
7372 txn->req.sl.rq.v += delta;
7373 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007374
Willy Tarreau79e57332018-10-02 16:01:16 +02007375 case 3: // uri
7376 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7377 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007378
Willy Tarreau79e57332018-10-02 16:01:16 +02007379 /* adjust req line offsets and lengths */
7380 delta = len - offset - (cur_end - cur_ptr);
7381 txn->req.sl.rq.u_l += delta;
7382 txn->req.sl.rq.v += delta;
7383 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007384
Willy Tarreau79e57332018-10-02 16:01:16 +02007385 default:
7386 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007387 }
7388
Willy Tarreau79e57332018-10-02 16:01:16 +02007389 /* commit changes and adjust end of message */
7390 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7391 txn->req.sl.rq.l += delta;
7392 txn->hdr_idx.v[0].len += delta;
7393 http_msg_move_end(&txn->req, delta);
7394 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007395}
7396
Willy Tarreau79e57332018-10-02 16:01:16 +02007397/* This function replace the HTTP status code and the associated message. The
7398 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007399 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007400void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007401{
Willy Tarreau79e57332018-10-02 16:01:16 +02007402 struct http_txn *txn = s->txn;
7403 char *cur_ptr, *cur_end;
7404 int delta;
7405 char *res;
7406 int c_l;
7407 const char *msg = reason;
7408 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007409
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007410 if (IS_HTX_STRM(s))
7411 return htx_res_set_status(status, reason, s);
7412
Willy Tarreau79e57332018-10-02 16:01:16 +02007413 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007414
Willy Tarreau79e57332018-10-02 16:01:16 +02007415 res = ultoa_o(status, trash.area, trash.size);
7416 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007417
Willy Tarreau79e57332018-10-02 16:01:16 +02007418 trash.area[c_l] = ' ';
7419 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007420
Willy Tarreau79e57332018-10-02 16:01:16 +02007421 /* Do we have a custom reason format string? */
7422 if (msg == NULL)
7423 msg = http_get_reason(status);
7424 msg_len = strlen(msg);
7425 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7426 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007427
Willy Tarreau79e57332018-10-02 16:01:16 +02007428 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7429 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007430
Willy Tarreau79e57332018-10-02 16:01:16 +02007431 /* commit changes and adjust message */
7432 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7433 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007434
Willy Tarreau79e57332018-10-02 16:01:16 +02007435 /* adjust res line offsets and lengths */
7436 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7437 txn->rsp.sl.st.c_l = c_l;
7438 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007439
Willy Tarreau79e57332018-10-02 16:01:16 +02007440 delta = trash.data - (cur_end - cur_ptr);
7441 txn->rsp.sl.st.l += delta;
7442 txn->hdr_idx.v[0].len += delta;
7443 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007444}
7445
Willy Tarreau58f10d72006-12-04 02:26:12 +01007446/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007447 * Local variables:
7448 * c-indent-level: 8
7449 * c-basic-offset: 8
7450 * End:
7451 */