blob: 0127e23b59b8c763e91a785046fe074dc650abdb [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010026#include <common/base64.h>
Frédéric Lécaillea41d5312018-01-29 12:05:07 +010027#include <common/cfgparse.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020028#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020029#include <common/compat.h>
30#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010031#include <common/debug.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010032#include <common/h1.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010042#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020043#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <types/global.h>
William Lallemand71bd11a2017-11-20 19:13:14 +010045#include <types/cache.h>
William Lallemand9ed62032016-11-21 17:49:11 +010046#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Willy Tarreau8797c062007-05-07 00:55:35 +020048#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020049#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020050#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010051#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020053#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010054#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010055#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020056#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010057#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020059#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020060#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010062#include <proto/hdr_idx.h>
Patrick Hemmere3faf022018-08-22 10:02:00 -040063#include <proto/hlua.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010064#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020065#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010067#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020069#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010070#include <proto/server.h>
Olivier Houchard985f1392018-11-30 17:31:52 +010071#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020072#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010073#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020074#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020075#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020076#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020077
Cyril Bonté19979e12012-04-04 12:57:21 +020078/* status codes available for the stats admin page (strictly 4 chars length) */
79const char *stat_status_codes[STAT_STATUS_SIZE] = {
80 [STAT_STATUS_DENY] = "DENY",
81 [STAT_STATUS_DONE] = "DONE",
82 [STAT_STATUS_ERRP] = "ERRP",
83 [STAT_STATUS_EXCD] = "EXCD",
84 [STAT_STATUS_NONE] = "NONE",
85 [STAT_STATUS_PART] = "PART",
86 [STAT_STATUS_UNKN] = "UNKN",
Christopher Faulet3c2ecf72019-02-27 16:41:27 +010087 [STAT_STATUS_IVAL] = "IVAL",
Cyril Bonté19979e12012-04-04 12:57:21 +020088};
89
Willy Tarreau2d3d94c2008-11-30 20:20:08 +010090/* This function handles a server error at the stream interface level. The
91 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +010092 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +010093 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +010094 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +020095 */
Willy Tarreau87b09662015-04-03 00:22:06 +020096static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau83061a82018-07-13 11:56:34 +020097 int err, int finst, const struct buffer *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +020098{
Christopher Faulet9768c262018-10-22 09:34:31 +020099 if (IS_HTX_STRM(s))
100 return htx_server_error(s, si, err, finst, msg);
101
Willy Tarreau2019f952017-03-14 11:07:31 +0100102 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100103 channel_auto_read(si_oc(si));
104 channel_abort(si_oc(si));
105 channel_auto_close(si_oc(si));
106 channel_erase(si_oc(si));
107 channel_auto_close(si_ic(si));
108 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200109 if (msg)
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200110 co_inject(si_ic(si), msg->area, msg->data);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200111 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200112 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200113 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200114 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115}
116
Willy Tarreau87b09662015-04-03 00:22:06 +0200117/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100118 * and message.
119 */
120
Willy Tarreau83061a82018-07-13 11:56:34 +0200121struct buffer *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100122{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200123 const int msgnum = http_get_status_idx(s->txn->status);
124
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100125 if (IS_HTX_STRM(s))
126 return htx_error_message(s);
127
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200128 if (s->be->errmsg[msgnum].area)
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200129 return &s->be->errmsg[msgnum];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200130 else if (strm_fe(s)->errmsg[msgnum].area)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200131 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100132 else
133 return &http_err_chunks[msgnum];
134}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200135
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100136void
Willy Tarreau83061a82018-07-13 11:56:34 +0200137http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100138{
Christopher Faulet9768c262018-10-22 09:34:31 +0200139 if (IS_HTX_STRM(s))
140 return htx_reply_and_close(s, status, msg);
141
Christopher Fauletd7c91962015-04-30 11:48:27 +0200142 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100143 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100144 si_retnclose(&s->si[0], msg);
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100145}
146
Willy Tarreau21d2af32008-02-14 20:25:24 +0100147/* Parse the URI from the given transaction (which is assumed to be in request
148 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
149 * It is returned otherwise.
150 */
Willy Tarreau6b952c82018-09-10 17:45:34 +0200151char *http_txn_get_path(const struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100152{
Willy Tarreau6b952c82018-09-10 17:45:34 +0200153 struct ist ret;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100154
Willy Tarreau9d9ccdb2018-10-28 20:13:12 +0100155 if (!txn->req.chn->buf.size)
156 return NULL;
157
Willy Tarreau6b952c82018-09-10 17:45:34 +0200158 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 +0100159
Willy Tarreau6b952c82018-09-10 17:45:34 +0200160 return ret.ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +0100161}
162
Willy Tarreau71241ab2012-12-27 11:30:54 +0100163/* Returns a 302 for a redirectable request that reaches a server working in
164 * in redirect mode. This may only be called just after the stream interface
165 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
166 * follow normal proxy processing. NOTE: this function is designed to support
167 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100168 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200169void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100170{
171 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100172 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100173 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200174 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100175
Christopher Fauletfefc73d2018-10-24 21:18:04 +0200176 if (IS_HTX_STRM(s))
177 return htx_perform_server_redirect(s, si);
178
Willy Tarreauefb453c2008-10-26 20:49:47 +0100179 /* 1: create the response header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200180 trash.data = strlen(HTTP_302);
181 memcpy(trash.area, HTTP_302, trash.data);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100182
Willy Tarreaub05e48a2018-09-20 11:12:58 +0200183 srv = __objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100184
Willy Tarreauefb453c2008-10-26 20:49:47 +0100185 /* 2: add the server's prefix */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200186 if (trash.data + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100187 return;
188
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100189 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100190 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200191 memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len);
192 trash.data += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100193 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100194
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200195 /* 3: add the request URI. Since it was already forwarded, we need
196 * to temporarily rewind the buffer.
197 */
Willy Tarreaueee5b512015-04-03 23:46:31 +0200198 txn = s->txn;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200199 c_rew(&s->req, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200200
Willy Tarreau6b952c82018-09-10 17:45:34 +0200201 path = http_txn_get_path(txn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200202 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 +0200203
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200204 c_adv(&s->req, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200205
Willy Tarreauefb453c2008-10-26 20:49:47 +0100206 if (!path)
207 return;
208
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200209 if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100210 return;
211
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200212 memcpy(trash.area + trash.data, path, len);
213 trash.data += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100214
215 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200216 memcpy(trash.area + trash.data,
217 "\r\nProxy-Connection: close\r\n\r\n", 29);
218 trash.data += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100219 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200220 memcpy(trash.area + trash.data,
221 "\r\nConnection: close\r\n\r\n", 23);
222 trash.data += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100223 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100224
225 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200226 si_shutr(si);
227 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100228 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100229 si->state = SI_ST_CLO;
230
231 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +0100232 txn->status = 302;
233 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100234
235 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100236 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500237 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100238}
239
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100240/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100241 * that the server side is closed. Note that err_type is actually a
242 * bitmask, where almost only aborts may be cumulated with other
243 * values. We consider that aborted operations are more important
244 * than timeouts or errors due to the fact that nobody else in the
245 * logs might explain incomplete retries. All others should avoid
246 * being cumulated. It should normally not be possible to have multiple
247 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100248 * Note that connection errors appearing on the second request of a keep-alive
249 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100250 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200251void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100252{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100253 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100254
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200255 /* set s->txn->status for http_error_message(s) */
256 s->txn->status = 503;
257
Willy Tarreauefb453c2008-10-26 20:49:47 +0100258 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200259 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100260 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100261 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200262 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100263 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200264 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100265 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200266 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100267 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100268 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200269 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +0100270 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100271 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200272 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100273 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200274 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100275 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200276 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100277 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200278 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +0200279 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200280 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100281 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200282 http_error_message(s));
283 else { /* SI_ET_CONN_OTHER and others */
284 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200285 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +0100286 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200287 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100288}
289
Willy Tarreau42250582007-04-01 01:30:43 +0200290extern const char sess_term_cond[8];
291extern const char sess_fin_state[8];
292extern const char *monthname[12];
Willy Tarreau8ceae722018-11-26 11:58:30 +0100293
294DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
295DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
296
297struct pool_head *pool_head_requri = NULL;
Willy Tarreaubafbe012017-11-24 17:34:44 +0100298struct pool_head *pool_head_capture = NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100299
Willy Tarreau117f59e2007-03-04 18:17:17 +0100300/*
301 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +0200302 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +0100303 */
Christopher Faulet10079f52018-10-03 15:17:28 +0200304void http_capture_headers(char *som, struct hdr_idx *idx,
305 char **cap, struct cap_hdr *cap_hdr)
Willy Tarreau117f59e2007-03-04 18:17:17 +0100306{
307 char *eol, *sol, *col, *sov;
308 int cur_idx;
309 struct cap_hdr *h;
310 int len;
311
312 sol = som + hdr_idx_first_pos(idx);
313 cur_idx = hdr_idx_first_idx(idx);
314
315 while (cur_idx) {
316 eol = sol + idx->v[cur_idx].len;
317
318 col = sol;
319 while (col < eol && *col != ':')
320 col++;
321
322 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100323 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +0100324 sov++;
325
326 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +0200327 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +0100328 (strncasecmp(sol, h->name, h->namelen) == 0)) {
329 if (cap[h->index] == NULL)
330 cap[h->index] =
Willy Tarreaubafbe012017-11-24 17:34:44 +0100331 pool_alloc(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100332
333 if (cap[h->index] == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100334 ha_alert("HTTP capture : out of memory.\n");
Willy Tarreau117f59e2007-03-04 18:17:17 +0100335 continue;
336 }
337
338 len = eol - sov;
339 if (len > h->len)
340 len = h->len;
341
342 memcpy(cap[h->index], sov, len);
343 cap[h->index][len]=0;
344 }
345 }
346 sol = eol + idx->v[cur_idx].cr + 1;
347 cur_idx = idx->v[cur_idx].next;
348 }
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +0100349}
350
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200351/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
352 * conversion succeeded, 0 in case of error. If the request was already 1.X,
353 * nothing is done and 1 is returned.
354 */
Willy Tarreau79e57332018-10-02 16:01:16 +0200355int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200356{
357 int delta;
358 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +0100359 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200360
361 if (msg->sl.rq.v_l != 0)
362 return 1;
363
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300364 /* RFC 1945 allows only GET for HTTP/0.9 requests */
365 if (txn->meth != HTTP_METH_GET)
366 return 0;
367
Willy Tarreauf37954d2018-06-15 18:31:02 +0200368 cur_end = ci_head(msg->chn) + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200369
370 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +0300371 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
372 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200373 }
374 /* add HTTP version */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200375 delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +0100376 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200377 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200378 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200379 HTTP_MSG_RQMETH,
Willy Tarreauf37954d2018-06-15 18:31:02 +0200380 ci_head(msg->chn), cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200381 NULL, NULL);
382 if (unlikely(!cur_end))
383 return 0;
384
385 /* we have a full HTTP/1.0 request now and we know that
386 * we have either a CR or an LF at <ptr>.
387 */
388 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
389 return 1;
390}
391
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100392/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100393 * and "keep-alive" values. If we already know that some headers may safely
394 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100395 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
396 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +0100397 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100398 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
399 * found, and TX_CON_*_SET is adjusted depending on what is left so only
400 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100401 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +0100402 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100403void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +0100404{
Willy Tarreau5b154472009-12-21 20:11:07 +0100405 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100406 const char *hdr_val = "Connection";
407 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +0100408
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100409 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +0100410 return;
411
Willy Tarreau88d349d2010-01-25 12:15:43 +0100412 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
413 hdr_val = "Proxy-Connection";
414 hdr_len = 16;
415 }
416
Willy Tarreau5b154472009-12-21 20:11:07 +0100417 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100418 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200419 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100420 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
421 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100422 if (to_del & 2)
423 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100424 else
425 txn->flags |= TX_CON_KAL_SET;
426 }
427 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
428 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100429 if (to_del & 1)
430 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100431 else
432 txn->flags |= TX_CON_CLO_SET;
433 }
Willy Tarreau50fc7772012-11-11 22:19:57 +0100434 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
435 txn->flags |= TX_HDR_CONN_UPG;
436 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100437 }
438
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100439 txn->flags |= TX_HDR_CONN_PRS;
440 return;
441}
Willy Tarreau5b154472009-12-21 20:11:07 +0100442
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100443/* Apply desired changes on the Connection: header. Values may be removed and/or
444 * added depending on the <wanted> flags, which are exclusively composed of
445 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
446 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
447 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100448void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100449{
450 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100451 const char *hdr_val = "Connection";
452 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100453
454 ctx.idx = 0;
455
Willy Tarreau88d349d2010-01-25 12:15:43 +0100456
457 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
458 hdr_val = "Proxy-Connection";
459 hdr_len = 16;
460 }
461
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100462 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreauf37954d2018-06-15 18:31:02 +0200463 while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100464 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
465 if (wanted & TX_CON_KAL_SET)
466 txn->flags |= TX_CON_KAL_SET;
467 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100468 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +0100469 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100470 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
471 if (wanted & TX_CON_CLO_SET)
472 txn->flags |= TX_CON_CLO_SET;
473 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100474 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +0100475 }
Willy Tarreau5b154472009-12-21 20:11:07 +0100476 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100477
478 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
479 return;
480
481 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
482 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100483 hdr_val = "Connection: close";
484 hdr_len = 17;
485 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
486 hdr_val = "Proxy-Connection: close";
487 hdr_len = 23;
488 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100489 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100490 }
491
492 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
493 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100494 hdr_val = "Connection: keep-alive";
495 hdr_len = 22;
496 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
497 hdr_val = "Proxy-Connection: keep-alive";
498 hdr_len = 28;
499 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100500 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +0100501 }
502 return;
Willy Tarreau5b154472009-12-21 20:11:07 +0100503}
504
Willy Tarreau87b09662015-04-03 00:22:06 +0200505void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200506{
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200507 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200508 int tmp = TX_CON_WANT_KAL;
509
Christopher Fauletf2824e62018-10-01 12:12:37 +0200510 if (IS_HTX_STRM(s))
Christopher Faulet0f226952018-10-22 09:29:56 +0200511 return htx_adjust_conn_mode(s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +0200512
Christopher Faulet315b39c2018-09-21 16:26:19 +0200513 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
514 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
515 tmp = TX_CON_WANT_TUN;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200516
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200517 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Christopher Faulet315b39c2018-09-21 16:26:19 +0200518 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200519 tmp = TX_CON_WANT_SCL;
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200520
Christopher Faulet315b39c2018-09-21 16:26:19 +0200521 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO ||
522 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200523 tmp = TX_CON_WANT_CLO;
524
525 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
526 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
527
528 if (!(txn->flags & TX_HDR_CONN_PRS) &&
529 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
530 /* parse the Connection header and possibly clean it */
531 int to_del = 0;
532 if ((msg->flags & HTTP_MSGF_VER_11) ||
533 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200534 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200535 to_del |= 2; /* remove "keep-alive" */
536 if (!(msg->flags & HTTP_MSGF_VER_11))
537 to_del |= 1; /* remove "close" */
538 http_parse_connection_header(txn, msg, to_del);
539 }
540
541 /* check if client or config asks for explicit close in KAL/SCL */
542 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
543 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
544 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
545 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
546 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200547 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +0200548 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
549}
William Lallemand82fe75c2012-10-23 10:25:10 +0200550
Willy Tarreaud787e662009-07-07 10:14:51 +0200551/* This stream analyser waits for a complete HTTP request. It returns 1 if the
552 * processing can continue on next analysers, or zero if it either needs more
553 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100554 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +0200555 * when it has nothing left to do, and may remove any analyser when it wants to
556 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100557 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200558int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100559{
Willy Tarreau59234e92008-11-30 23:51:27 +0100560 /*
561 * We will parse the partial (or complete) lines.
562 * We will check the request syntax, and also join multi-line
563 * headers. An index of all the lines will be elaborated while
564 * parsing.
565 *
566 * For the parsing, we use a 28 states FSM.
567 *
568 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +0200569 * ci_head(req) = beginning of request
570 * ci_head(req) + msg->eoh = end of processed headers / start of current one
571 * ci_tail(req) = end of input data
572 * msg->eol = end of current header or line (LF or CRLF)
573 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +0200574 *
575 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +0200576 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +0200577 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
578 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +0100579 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100580
Willy Tarreau59234e92008-11-30 23:51:27 +0100581 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200582 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200583 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +0100584 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +0200585 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +0100586
Christopher Faulete0768eb2018-10-03 16:38:02 +0200587 if (IS_HTX_STRM(s))
588 return htx_wait_for_request(s, req, an_bit);
589
Christopher Faulet45073512018-07-20 10:16:29 +0200590 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 +0100591 now_ms, __FUNCTION__,
592 s,
593 req,
594 req->rex, req->wex,
595 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200596 ci_data(req),
Willy Tarreau6bf17362009-02-24 10:48:35 +0100597 req->analysers);
598
Willy Tarreau52a0c602009-08-16 22:45:38 +0200599 /* we're speaking HTTP here, so let's speak HTTP to the client */
600 s->srv_error = http_return_srv_error;
601
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300602 /* If there is data available for analysis, log the end of the idle time. */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200603 if (c_data(req) && s->logs.t_idle == -1)
Rian McGuire89fcb7d2018-04-24 11:19:21 -0300604 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
605
Willy Tarreau83e3af02009-12-28 17:39:57 +0100606 /* There's a protected area at the end of the buffer for rewriting
607 * purposes. We don't want to start to parse the request if the
608 * protected area is affected, because we may have to move processed
609 * data later, which is much more complicated.
610 */
Willy Tarreaud760eec2018-07-10 09:50:25 +0200611 if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +0200612 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +0100613 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200614 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +0100615 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100616 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200617 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200618 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +0100619 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +0100620 return 0;
621 }
Willy Tarreau188e2302018-06-15 11:11:53 +0200622 if (unlikely(ci_tail(req) < c_ptr(req, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200623 ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200624 channel_slow_realign(req, trash.area);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100625 }
626
Willy Tarreauf37954d2018-06-15 18:31:02 +0200627 if (likely(msg->next < ci_data(req))) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +0100628 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +0100629 }
630
Willy Tarreau59234e92008-11-30 23:51:27 +0100631 /* 1: we might have to print this header in debug mode */
632 if (unlikely((global.mode & MODE_DEBUG) &&
633 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +0200634 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100635 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100636
Willy Tarreauf37954d2018-06-15 18:31:02 +0200637 sol = ci_head(req);
Willy Tarreaue92693a2012-09-24 21:13:39 +0200638 /* this is a bit complex : in case of error on the request line,
639 * we know that rq.l is still zero, so we display only the part
640 * up to the end of the line (truncated by debug_hdr).
641 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200642 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req));
Willy Tarreau59234e92008-11-30 23:51:27 +0100643 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +0100644
Willy Tarreau59234e92008-11-30 23:51:27 +0100645 sol += hdr_idx_first_pos(&txn->hdr_idx);
646 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +0100647
Willy Tarreau59234e92008-11-30 23:51:27 +0100648 while (cur_idx) {
649 eol = sol + txn->hdr_idx.v[cur_idx].len;
650 debug_hdr("clihdr", s, sol, eol);
651 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
652 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100653 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100654 }
655
Willy Tarreau58f10d72006-12-04 02:26:12 +0100656
Willy Tarreau59234e92008-11-30 23:51:27 +0100657 /*
658 * Now we quickly check if we have found a full valid request.
659 * If not so, we check the FD and buffer states before leaving.
660 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +0100661 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100662 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +0200663 * on a keep-alive stream, if we encounter and error, close, t/o,
664 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100665 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +0200666 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +0200667 * Last, we may increase some tracked counters' http request errors on
668 * the cases that are deliberately the client's fault. For instance,
669 * a timeout or connection reset is not counted as an error. However
670 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +0100671 */
Willy Tarreau58f10d72006-12-04 02:26:12 +0100672
Willy Tarreau655dce92009-11-08 13:10:58 +0100673 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100674 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100675 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100676 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100677 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200678 stream_inc_http_req_ctr(s);
679 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200680 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100681 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +0100682 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100683
Willy Tarreau59234e92008-11-30 23:51:27 +0100684 /* 1: Since we are in header mode, if there's no space
685 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +0200686 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +0100687 * must terminate it now.
688 */
Willy Tarreau23752332018-06-15 14:54:53 +0200689 if (unlikely(channel_full(req, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100690 /* FIXME: check if URI is set and return Status
691 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +0100692 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200693 stream_inc_http_req_ctr(s);
694 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200695 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +0200696 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +0200697 msg->err_pos = ci_data(req);
Willy Tarreau59234e92008-11-30 23:51:27 +0100698 goto return_bad_req;
699 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100700
Willy Tarreau59234e92008-11-30 23:51:27 +0100701 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200702 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200703 if (!(s->flags & SF_ERR_MASK))
704 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100705
Willy Tarreaufcffa692010-01-10 14:21:19 +0100706 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100707 goto failed_keep_alive;
708
Willy Tarreau0f228a02015-05-01 15:37:53 +0200709 if (sess->fe->options & PR_O_IGNORE_PRB)
710 goto failed_keep_alive;
711
Willy Tarreau59234e92008-11-30 23:51:27 +0100712 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +0200713 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200714 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +0200715 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200716 }
717
Willy Tarreaudc979f22012-12-04 10:39:01 +0100718 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +0100719 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +0100720 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100721 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +0100722 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +0200723 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200724 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100725 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200726 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100727 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200728
Willy Tarreaue7dff022015-04-03 01:14:29 +0200729 if (!(s->flags & SF_FINST_MASK))
730 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +0100731 return 0;
732 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +0200733
Willy Tarreau59234e92008-11-30 23:51:27 +0100734 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200735 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200736 if (!(s->flags & SF_ERR_MASK))
737 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100738
Willy Tarreaufcffa692010-01-10 14:21:19 +0100739 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100740 goto failed_keep_alive;
741
Willy Tarreau0f228a02015-05-01 15:37:53 +0200742 if (sess->fe->options & PR_O_IGNORE_PRB)
743 goto failed_keep_alive;
744
Willy Tarreau59234e92008-11-30 23:51:27 +0100745 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +0200746 if (msg->err_pos >= 0) {
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200747 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +0200748 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200749 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100750 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +0100751 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +0100752 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200753 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +0100754 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200755
Willy Tarreau87b09662015-04-03 00:22:06 +0200756 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200757 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100758 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200759 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100760 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200761
Willy Tarreaue7dff022015-04-03 01:14:29 +0200762 if (!(s->flags & SF_FINST_MASK))
763 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +0100764 return 0;
765 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200766
Willy Tarreau59234e92008-11-30 23:51:27 +0100767 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200768 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +0200769 if (!(s->flags & SF_ERR_MASK))
770 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +0100771
Willy Tarreaufcffa692010-01-10 14:21:19 +0100772 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +0100773 goto failed_keep_alive;
774
Willy Tarreau0f228a02015-05-01 15:37:53 +0200775 if (sess->fe->options & PR_O_IGNORE_PRB)
776 goto failed_keep_alive;
777
Willy Tarreau4076a152009-04-02 15:18:36 +0200778 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200779 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +0100780 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +0100781 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +0100782 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200783 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +0100784 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +0200785 stream_inc_http_err_ctr(s);
786 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200787 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100788 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200789 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100790 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200791
Willy Tarreaue7dff022015-04-03 01:14:29 +0200792 if (!(s->flags & SF_FINST_MASK))
793 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +0200794 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100795 }
796
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200797 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200798 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100799 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100800
801 if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req)) {
Willy Tarreau5e205522011-12-17 16:34:27 +0100802 /* We need more data, we have to re-enable quick-ack in case we
803 * previously disabled it, otherwise we might cause the client
804 * to delay next data.
805 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100806 conn_set_quickack(objt_conn(sess->origin), 1);
Willy Tarreau5e205522011-12-17 16:34:27 +0100807 }
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100808
Willy Tarreaufcffa692010-01-10 14:21:19 +0100809 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
810 /* If the client starts to talk, let's fall back to
811 * request timeout processing.
812 */
813 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +0100814 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +0100815 }
816
Willy Tarreau59234e92008-11-30 23:51:27 +0100817 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +0100818 if (!tick_isset(req->analyse_exp)) {
819 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
820 (txn->flags & TX_WAIT_NEXT_RQ) &&
821 tick_isset(s->be->timeout.httpka))
822 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
823 else
824 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
825 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100826
Willy Tarreau59234e92008-11-30 23:51:27 +0100827 /* we're not ready yet */
828 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +0100829
830 failed_keep_alive:
831 /* Here we process low-level errors for keep-alive requests. In
832 * short, if the request is not the first one and it experiences
833 * a timeout, read error or shutdown, we just silently close so
834 * that the client can try again.
835 */
836 txn->status = 0;
837 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +0100838 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +0100839 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +0200840 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100841 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100842 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +0100843 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +0100844 }
Willy Tarreau58f10d72006-12-04 02:26:12 +0100845
Willy Tarreaud787e662009-07-07 10:14:51 +0200846 /* OK now we have a complete HTTP request with indexed headers. Let's
847 * complete the request parsing by setting a few fields we will need
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200848 * later. At this point, we have the last CRLF at req->buf.data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +0100849 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +0100850 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +0100851 * byte after the last LF. msg->sov points to the first byte of data.
852 * msg->eol cannot be trusted because it may have been left uninitialized
853 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +0200854 */
Willy Tarreau9cdde232007-05-02 20:58:19 +0200855
Willy Tarreau87b09662015-04-03 00:22:06 +0200856 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200857 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +0100858
Willy Tarreaub16a5742010-01-10 14:46:16 +0100859 if (txn->flags & TX_WAIT_NEXT_RQ) {
860 /* kill the pending keep-alive timeout */
861 txn->flags &= ~TX_WAIT_NEXT_RQ;
862 req->analyse_exp = TICK_ETERNITY;
863 }
864
865
Willy Tarreaud787e662009-07-07 10:14:51 +0200866 /* Maybe we found in invalid header name while we were configured not
867 * to block on that, so we have to capture it now.
868 */
869 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +0200870 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +0200871
Willy Tarreau59234e92008-11-30 23:51:27 +0100872 /*
873 * 1: identify the method
874 */
Willy Tarreauf37954d2018-06-15 18:31:02 +0200875 txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +0100876
877 /* we can make use of server redirect on GET and HEAD */
878 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200879 s->flags |= SF_REDIRECTABLE;
Willy Tarreau91659792017-11-10 19:38:10 +0100880 else if (txn->meth == HTTP_METH_OTHER &&
Willy Tarreauf37954d2018-06-15 18:31:02 +0200881 msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) {
Willy Tarreau91659792017-11-10 19:38:10 +0100882 /* PRI is reserved for the HTTP/2 preface */
883 msg->err_pos = 0;
884 goto return_bad_req;
885 }
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200886
Willy Tarreau59234e92008-11-30 23:51:27 +0100887 /*
888 * 2: check if the URI matches the monitor_uri.
889 * We have to do this for every request which gets in, because
890 * the monitor-uri is defined by the frontend.
891 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200892 if (unlikely((sess->fe->monitor_uri_len != 0) &&
893 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +0200894 !memcmp(ci_head(req) + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200895 sess->fe->monitor_uri,
896 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +0100897 /*
Willy Tarreau59234e92008-11-30 23:51:27 +0100898 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +0100899 */
Willy Tarreau59234e92008-11-30 23:51:27 +0100900 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +0100901
Willy Tarreaue7dff022015-04-03 01:14:29 +0200902 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100903 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +0100904
Willy Tarreau59234e92008-11-30 23:51:27 +0100905 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200906 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +0200907 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +0200908
Willy Tarreau59234e92008-11-30 23:51:27 +0100909 ret = acl_pass(ret);
910 if (cond->pol == ACL_COND_UNLESS)
911 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +0100912
Willy Tarreau59234e92008-11-30 23:51:27 +0100913 if (ret) {
914 /* we fail this request, let's return 503 service unavail */
915 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200916 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +0200917 if (!(s->flags & SF_ERR_MASK))
918 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +0100919 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +0100920 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100921 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100922
Joseph Herlant5ba80252018-11-15 09:25:36 -0800923 /* nothing to fail, let's reply normally */
Willy Tarreau59234e92008-11-30 23:51:27 +0100924 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200925 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +0200926 if (!(s->flags & SF_ERR_MASK))
927 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +0100928 goto return_prx_cond;
929 }
930
931 /*
932 * 3: Maybe we have to copy the original REQURI for the logs ?
933 * Note: we cannot log anymore if the request has been
934 * classified as invalid.
935 */
936 if (unlikely(s->logs.logwait & LW_REQ)) {
937 /* we have a complete HTTP request that we must log */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100938 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Willy Tarreau59234e92008-11-30 23:51:27 +0100939 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100940
Stéphane Cottin23e9e932017-05-18 08:58:41 +0200941 if (urilen >= global.tune.requri_len )
942 urilen = global.tune.requri_len - 1;
Willy Tarreauf37954d2018-06-15 18:31:02 +0200943 memcpy(txn->uri, ci_head(req), urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +0100944 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100945
Willy Tarreaud79a3b22012-12-28 09:40:16 +0100946 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +0100947 s->do_log(s);
948 } else {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100949 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100950 }
Willy Tarreau59234e92008-11-30 23:51:27 +0100951 }
Willy Tarreau06619262006-12-17 08:37:22 +0100952
Willy Tarreau91852eb2015-05-01 13:26:00 +0200953 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
954 * exactly one digit "." one digit. This check may be disabled using
955 * option accept-invalid-http-request.
956 */
957 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
958 if (msg->sl.rq.v_l != 8) {
959 msg->err_pos = msg->sl.rq.v;
960 goto return_bad_req;
961 }
962
Willy Tarreauf37954d2018-06-15 18:31:02 +0200963 if (ci_head(req)[msg->sl.rq.v + 4] != '/' ||
964 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) ||
965 ci_head(req)[msg->sl.rq.v + 6] != '.' ||
966 !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +0200967 msg->err_pos = msg->sl.rq.v + 4;
968 goto return_bad_req;
969 }
970 }
Willy Tarreau13317662015-05-01 13:47:08 +0200971 else {
972 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
973 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
974 goto return_bad_req;
975 }
Willy Tarreau91852eb2015-05-01 13:26:00 +0200976
Willy Tarreau5b154472009-12-21 20:11:07 +0100977 /* ... and check if the request is HTTP/1.1 or above */
978 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +0200979 ((ci_head(req)[msg->sl.rq.v + 5] > '1') ||
980 ((ci_head(req)[msg->sl.rq.v + 5] == '1') &&
981 (ci_head(req)[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +0100982 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +0100983
984 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +0100985 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 +0100986
Willy Tarreau88d349d2010-01-25 12:15:43 +0100987 /* if the frontend has "option http-use-proxy-header", we'll check if
988 * we have what looks like a proxied connection instead of a connection,
989 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
990 * Note that this is *not* RFC-compliant, however browsers and proxies
991 * happen to do that despite being non-standard :-(
992 * We consider that a request not beginning with either '/' or '*' is
993 * a proxied connection, which covers both "scheme://location" and
994 * CONNECT ip:port.
995 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200996 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +0200997 ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +0100998 txn->flags |= TX_USE_PX_CONN;
999
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001000 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001001 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001002
Willy Tarreau59234e92008-11-30 23:51:27 +01001003 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02001004 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02001005 http_capture_headers(ci_head(req), &txn->hdr_idx,
1006 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02001007
Willy Tarreau557f1992015-05-01 10:05:17 +02001008 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
1009 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001010 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001011 * The length of a message body is determined by one of the following
1012 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02001013 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001014 * 1. Any response to a HEAD request and any response with a 1xx
1015 * (Informational), 204 (No Content), or 304 (Not Modified) status
1016 * code is always terminated by the first empty line after the
1017 * header fields, regardless of the header fields present in the
1018 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001019 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001020 * 2. Any 2xx (Successful) response to a CONNECT request implies that
1021 * the connection will become a tunnel immediately after the empty
1022 * line that concludes the header fields. A client MUST ignore any
1023 * Content-Length or Transfer-Encoding header fields received in
1024 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001025 *
Willy Tarreau557f1992015-05-01 10:05:17 +02001026 * 3. If a Transfer-Encoding header field is present and the chunked
1027 * transfer coding (Section 4.1) is the final encoding, the message
1028 * body length is determined by reading and decoding the chunked
1029 * data until the transfer coding indicates the data is complete.
1030 *
1031 * If a Transfer-Encoding header field is present in a response and
1032 * the chunked transfer coding is not the final encoding, the
1033 * message body length is determined by reading the connection until
1034 * it is closed by the server. If a Transfer-Encoding header field
1035 * is present in a request and the chunked transfer coding is not
1036 * the final encoding, the message body length cannot be determined
1037 * reliably; the server MUST respond with the 400 (Bad Request)
1038 * status code and then close the connection.
1039 *
1040 * If a message is received with both a Transfer-Encoding and a
1041 * Content-Length header field, the Transfer-Encoding overrides the
1042 * Content-Length. Such a message might indicate an attempt to
1043 * perform request smuggling (Section 9.5) or response splitting
1044 * (Section 9.4) and ought to be handled as an error. A sender MUST
1045 * remove the received Content-Length field prior to forwarding such
1046 * a message downstream.
1047 *
1048 * 4. If a message is received without Transfer-Encoding and with
1049 * either multiple Content-Length header fields having differing
1050 * field-values or a single Content-Length header field having an
1051 * invalid value, then the message framing is invalid and the
1052 * recipient MUST treat it as an unrecoverable error. If this is a
1053 * request message, the server MUST respond with a 400 (Bad Request)
1054 * status code and then close the connection. If this is a response
1055 * message received by a proxy, the proxy MUST close the connection
1056 * to the server, discard the received response, and send a 502 (Bad
1057 * Gateway) response to the client. If this is a response message
1058 * received by a user agent, the user agent MUST close the
1059 * connection to the server and discard the received response.
1060 *
1061 * 5. If a valid Content-Length header field is present without
1062 * Transfer-Encoding, its decimal value defines the expected message
1063 * body length in octets. If the sender closes the connection or
1064 * the recipient times out before the indicated number of octets are
1065 * received, the recipient MUST consider the message to be
1066 * incomplete and close the connection.
1067 *
1068 * 6. If this is a request message and none of the above are true, then
1069 * the message body length is zero (no message body is present).
1070 *
1071 * 7. Otherwise, this is a response message without a declared message
1072 * body length, so the message body length is determined by the
1073 * number of octets received prior to the server closing the
1074 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02001075 */
1076
Willy Tarreau32b47f42009-10-18 20:55:02 +02001077 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001078 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreauf37954d2018-06-15 18:31:02 +02001079 while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001080 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02001081 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001082 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02001083 /* chunked not last, return badreq */
1084 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001085 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001086 }
1087
Willy Tarreau1c913912015-04-30 10:57:51 +02001088 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02001089 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02001090 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001091 while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02001092 http_remove_header2(msg, &txn->hdr_idx, &ctx);
1093 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02001094 else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02001095 signed long long cl;
1096
Willy Tarreauad14f752011-09-02 20:33:27 +02001097 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001098 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001099 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001100 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001101
Willy Tarreauad14f752011-09-02 20:33:27 +02001102 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001103 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001104 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02001105 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001106
Willy Tarreauad14f752011-09-02 20:33:27 +02001107 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001108 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001109 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02001110 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001111
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001112 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02001113 msg->err_pos = ctx.line + ctx.val - ci_head(req);
Willy Tarreau32b47f42009-10-18 20:55:02 +02001114 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02001115 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02001116
Christopher Fauletbe821b92017-03-30 11:21:53 +02001117 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01001118 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001119 }
1120
Willy Tarreau34dfc602015-05-01 10:09:49 +02001121 /* even bodyless requests have a known length */
1122 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01001123
Willy Tarreau179085c2014-04-28 16:48:56 +02001124 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
1125 * only change if both the request and the config reference something else.
1126 * Option httpclose by itself sets tunnel mode where headers are mangled.
1127 * However, if another mode is set, it will affect it (eg: server-close/
1128 * keep-alive + httpclose = close). Note that we avoid to redo the same work
1129 * if FE and BE have the same settings (common). The method consists in
1130 * checking if options changed between the two calls (implying that either
1131 * one is non-null, or one of them is non-null and we are there for the first
1132 * time.
1133 */
1134 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001135 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001136 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02001137
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001138 /* we may have to wait for the request's body */
1139 if ((s->be->options & PR_O_WREQ_BODY) &&
1140 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
1141 req->analysers |= AN_REQ_HTTP_BODY;
1142
Willy Tarreau83ece462017-12-21 15:13:09 +01001143 /*
1144 * RFC7234#4:
1145 * A cache MUST write through requests with methods
1146 * that are unsafe (Section 4.2.1 of [RFC7231]) to
1147 * the origin server; i.e., a cache is not allowed
1148 * to generate a reply to such a request before
1149 * having forwarded the request and having received
1150 * a corresponding response.
1151 *
1152 * RFC7231#4.2.1:
1153 * Of the request methods defined by this
1154 * specification, the GET, HEAD, OPTIONS, and TRACE
1155 * methods are defined to be safe.
1156 */
1157 if (likely(txn->meth == HTTP_METH_GET ||
1158 txn->meth == HTTP_METH_HEAD ||
1159 txn->meth == HTTP_METH_OPTIONS ||
1160 txn->meth == HTTP_METH_TRACE))
1161 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
1162
Willy Tarreaud787e662009-07-07 10:14:51 +02001163 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02001164 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02001165 req->analyse_exp = TICK_ETERNITY;
1166 return 1;
1167
1168 return_bad_req:
1169 /* We centralize bad requests processing here */
1170 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
1171 /* we detected a parsing error. We want to archive this request
1172 * in the dedicated proxy area for later troubleshooting.
1173 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02001174 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02001175 }
1176
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001177 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02001178 txn->req.msg_state = HTTP_MSG_ERROR;
1179 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001180 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001181
Olivier Houcharda798bf52019-03-08 18:52:00 +01001182 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001183 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001184 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02001185
1186 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02001187 if (!(s->flags & SF_ERR_MASK))
1188 s->flags |= SF_ERR_PRXCOND;
1189 if (!(s->flags & SF_FINST_MASK))
1190 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02001191
Christopher Faulet0184ea72017-01-05 14:06:34 +01001192 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02001193 req->analyse_exp = TICK_ETERNITY;
1194 return 0;
1195}
1196
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02001197
Willy Tarreau347a35d2013-11-22 17:51:09 +01001198/* This function prepares an applet to handle the stats. It can deal with the
1199 * "100-continue" expectation, check that admin rules are met for POST requests,
1200 * and program a response message if something was unexpected. It cannot fail
1201 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001202 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001203 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02001204 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001205 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001206int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001207{
1208 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01001209 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02001210 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001211 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001212 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001213 struct uri_auth *uri_auth = s->be->uri_auth;
1214 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001215 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001216
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001217 appctx = si_appctx(si);
1218 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1219 appctx->st1 = appctx->st2 = 0;
1220 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
1221 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001222 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02001223 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001224
Willy Tarreauf37954d2018-06-15 18:31:02 +02001225 uri = ci_head(msg->chn) + msg->sl.rq.u;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001226 lookup = uri + uri_auth->uri_len;
1227
1228 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
1229 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001230 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001231 break;
1232 }
1233 }
1234
1235 if (uri_auth->refresh) {
1236 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
1237 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001238 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001239 break;
1240 }
1241 }
1242 }
1243
1244 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
1245 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001246 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001247 break;
1248 }
1249 }
1250
Willy Tarreau1e62df92016-01-11 18:57:53 +01001251 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
1252 if (memcmp(h, ";typed", 6) == 0) {
1253 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
1254 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1255 break;
1256 }
1257 }
1258
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001259 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1260 if (memcmp(h, ";st=", 4) == 0) {
1261 int i;
1262 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001263 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001264 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
1265 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001266 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001267 break;
1268 }
1269 }
1270 break;
1271 }
1272 }
1273
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001274 appctx->ctx.stats.scope_str = 0;
1275 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001276 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
1277 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
1278 int itx = 0;
1279 const char *h2;
1280 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
1281 const char *err;
1282
1283 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
1284 h2 = h;
Willy Tarreauf37954d2018-06-15 18:31:02 +02001285 appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001286 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
1287 itx++;
1288 h++;
1289 }
1290
1291 if (itx > STAT_SCOPE_TXT_MAXLEN)
1292 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001293 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001294
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001295 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001296 memcpy(scope_txt, h2, itx);
1297 scope_txt[itx] = '\0';
1298 err = invalid_char(scope_txt);
1299 if (err) {
1300 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001301 appctx->ctx.stats.scope_str = 0;
1302 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001303 }
1304 break;
1305 }
1306 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001307
1308 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01001309 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001310 int ret = 1;
1311
1312 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001313 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 +01001314 ret = acl_pass(ret);
1315 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
1316 ret = !ret;
1317 }
1318
1319 if (ret) {
1320 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001321 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001322 break;
1323 }
1324 }
1325
1326 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001327 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001328 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02001329 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02001330 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
1331 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001332 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001333 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001334 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001335 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
1336 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02001337 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001338 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01001339 else {
1340 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001341 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001342 }
1343
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001344 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01001345 return 1;
1346}
1347
Willy Tarreau87b09662015-04-03 00:22:06 +02001348int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001349 const char* name, unsigned int name_len,
1350 const char *str, struct my_regex *re,
1351 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06001352{
Willy Tarreaueee5b512015-04-03 23:46:31 +02001353 struct hdr_idx *idx = &s->txn->hdr_idx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001354 struct buffer *output = get_trash_chunk();
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001355
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001356 /* Choose the header browsing function. */
1357 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001358 case ACT_HTTP_REPLACE_VAL:
Willy Tarreauc5a4fd52018-12-11 11:42:27 +01001359 return http_legacy_replace_header(idx, msg, name, name_len, str, re, output);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001360 case ACT_HTTP_REPLACE_HDR:
Willy Tarreauc5a4fd52018-12-11 11:42:27 +01001361 return http_legacy_replace_full_header(idx, msg, name, name_len, str, re, output);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01001362 default: /* impossible */
1363 return -1;
1364 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001365}
1366
Willy Tarreau87b09662015-04-03 00:22:06 +02001367static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001368 const char* name, unsigned int name_len,
1369 struct list *fmt, struct my_regex *re,
1370 int action)
1371{
Willy Tarreau83061a82018-07-13 11:56:34 +02001372 struct buffer *replace;
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001373 int ret = -1;
1374
1375 replace = alloc_trash_chunk();
1376 if (!replace)
1377 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001378
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001379 replace->data = build_logline(s, replace->area, replace->size, fmt);
1380 if (replace->data >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001381 goto leave;
1382
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001383 ret = http_transform_header_str(s, msg, name, name_len, replace->area,
1384 re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001385
Christopher Faulet07a0fec2017-02-08 12:17:07 +01001386 leave:
1387 free_trash_chunk(replace);
1388 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01001389}
1390
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001391/*
1392 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
1393 * built according to <fmt> log line format.
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001394 * If <early_hints> is NULL, it is allocated and the HTTP 103 response first
1395 * line is inserted before the header. If an error occurred <early_hints> is
1396 * released and NULL is returned. On success the updated buffer is returned.
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001397 */
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001398static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints,
1399 const char* name, unsigned int name_len,
1400 struct list *fmt)
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001401{
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001402 if (!early_hints) {
1403 early_hints = alloc_trash_chunk();
1404 if (!early_hints)
1405 goto fail;
1406 if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len))
1407 goto fail;
1408 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001409
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001410 if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2))
1411 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001412
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001413 early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt);
1414 if (!chunk_memcat(early_hints, "\r\n", 2))
1415 goto fail;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001416
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001417 return early_hints;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001418
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001419 fail:
1420 free_trash_chunk(early_hints);
1421 return NULL;
1422}
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001423
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001424/* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the
1425 * response is added. If an error occurred or if another response was already
1426 * sent, this function does nothing.
1427 */
1428static void http_send_early_hints(struct stream *s, struct buffer *early_hints)
1429{
1430 struct channel *chn = s->txn->rsp.chn;
1431 char *cur_ptr = ci_head(chn);
1432 int ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001433
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001434 /* If a response was already sent, skip early hints */
1435 if (s->txn->status > 0)
1436 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001437
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001438 if (!chunk_memcat(early_hints, "\r\n", 2))
1439 return;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001440
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001441 ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints));
1442 c_adv(chn, ret);
1443 chn->total += ret;
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001444}
1445
Willy Tarreau87b09662015-04-03 00:22:06 +02001446/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02001447 * transaction <txn>. Returns the verdict of the first rule that prevents
1448 * further processing of the request (auth, deny, ...), and defaults to
1449 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
1450 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02001451 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
1452 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
1453 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001454 */
Willy Tarreau0b748332014-04-29 00:13:29 +02001455enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02001456http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001457{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001458 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001459 struct http_txn *txn = s->txn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001460 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01001461 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02001462 const char *auth_realm;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001463 struct buffer *early_hints = NULL;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001464 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001465 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02001466 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001467
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001468 /* If "the current_rule_list" match the executed rule list, we are in
1469 * resume condition. If a resume is needed it is always in the action
1470 * and never in the ACL or converters. In this case, we initialise the
1471 * current rule, and go to the action execution point.
1472 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001473 if (s->current_rule) {
1474 rule = s->current_rule;
1475 s->current_rule = NULL;
1476 if (s->current_rule_list == rules)
1477 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001478 }
1479 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001480
Willy Tarreauff011f22011-01-06 17:51:27 +01001481 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001482
Willy Tarreau96257ec2012-12-27 10:46:37 +01001483 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01001484 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001485 int ret;
1486
Willy Tarreau192252e2015-04-04 01:47:55 +02001487 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001488 ret = acl_pass(ret);
1489
Willy Tarreauff011f22011-01-06 17:51:27 +01001490 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001491 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001492
1493 if (!ret) /* condition not matched */
1494 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001495 }
1496
Willy Tarreauacc98002015-09-27 23:34:39 +02001497 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001498resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001499 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001500 case ACT_ACTION_ALLOW:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001501 rule_ret = HTTP_RULE_RES_STOP;
1502 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001503
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001504 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02001505 if (deny_status)
1506 *deny_status = rule->deny_status;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001507 rule_ret = HTTP_RULE_RES_DENY;
1508 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001509
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001510 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01001511 txn->flags |= TX_CLTARPIT;
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 Tarreauccbcc372012-12-27 12:37:57 +01001516
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001517 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001518 /* Be sure to send any pending HTTP 103 response first */
1519 if (early_hints) {
1520 http_send_early_hints(s, early_hints);
1521 free_trash_chunk(early_hints);
1522 early_hints = NULL;
1523 }
Willy Tarreauae3c0102014-04-28 23:22:08 +02001524 /* Auth might be performed on regular http-req rules as well as on stats */
1525 auth_realm = rule->arg.auth.realm;
1526 if (!auth_realm) {
1527 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
1528 auth_realm = STATS_DEFAULT_REALM;
1529 else
1530 auth_realm = px->id;
1531 }
1532 /* send 401/407 depending on whether we use a proxy or not. We still
1533 * count one error, because normal browsing won't significantly
1534 * increase the counter but brute force attempts will.
1535 */
1536 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
1537 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001538 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02001539 stream_inc_http_err_ctr(s);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001540 rule_ret = HTTP_RULE_RES_ABRT;
1541 goto end;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001542
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001543 case ACT_HTTP_REDIR:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001544 /* Be sure to send any pending HTTP 103 response first */
1545 if (early_hints) {
1546 http_send_early_hints(s, early_hints);
1547 free_trash_chunk(early_hints);
1548 early_hints = NULL;
1549 }
Christopher Fauletea827bd2018-11-15 15:34:11 +01001550 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau0b748332014-04-29 00:13:29 +02001551 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Fauletea827bd2018-11-15 15:34:11 +01001552 rule_ret = HTTP_RULE_RES_BADREQ;
1553 goto end;
Willy Tarreau81499eb2012-12-27 12:19:02 +01001554
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001555 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001556 s->task->nice = rule->arg.nice;
1557 break;
1558
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001559 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001560 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001561 break;
1562
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001563 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001564 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Willy Tarreau51347ed2013-06-11 19:34:13 +02001565 break;
1566
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001567 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001568 s->logs.level = rule->arg.loglevel;
1569 break;
1570
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001571 case ACT_HTTP_REPLACE_HDR:
1572 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001573 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
1574 rule->arg.hdr_add.name_len,
1575 &rule->arg.hdr_add.fmt,
Christopher Fauletea827bd2018-11-15 15:34:11 +01001576 &rule->arg.hdr_add.re, rule->action)) {
1577 rule_ret = HTTP_RULE_RES_BADREQ;
1578 goto end;
1579 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001580 break;
1581
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001582 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01001583 ctx.idx = 0;
1584 /* remove all occurrences of the header */
1585 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001586 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01001587 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01001588 }
Willy Tarreau85603282015-01-21 20:39:27 +01001589 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01001590
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001591 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001592 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02001593 /* The scope of the trash buffer must be limited to this function. The
1594 * build_logline() function can execute a lot of other function which
1595 * can use the trash buffer. So for limiting the scope of this global
1596 * buffer, we build first the header value using build_logline, and
1597 * after we store the header name.
1598 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001599 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001600
1601 replace = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001602 if (!replace) {
1603 rule_ret = HTTP_RULE_RES_BADREQ;
1604 goto end;
1605 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001606
Thierry Fournier4b788f72016-06-01 13:35:36 +02001607 len = rule->arg.hdr_add.name_len + 2,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001608 len += build_logline(s, replace->area + len,
1609 replace->size - len,
1610 &rule->arg.hdr_add.fmt);
1611 memcpy(replace->area, rule->arg.hdr_add.name,
1612 rule->arg.hdr_add.name_len);
1613 replace->area[rule->arg.hdr_add.name_len] = ':';
1614 replace->area[rule->arg.hdr_add.name_len + 1] = ' ';
1615 replace->data = len;
Willy Tarreau85603282015-01-21 20:39:27 +01001616
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001617 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001618 /* remove all occurrences of the header */
1619 ctx.idx = 0;
1620 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001621 ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001622 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
1623 }
1624 }
1625
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001626 if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001627 static unsigned char rate_limit = 0;
1628
1629 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001630 replace->area[rule->arg.hdr_add.name_len] = 0;
1631 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,
1632 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001633 }
1634
Olivier Houcharda798bf52019-03-08 18:52:00 +01001635 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001636 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001637 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001638 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001639 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001640 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001641
1642 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01001643 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001644 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001645
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001646 case ACT_HTTP_DEL_ACL:
1647 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001648 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001649 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001650
1651 /* collect reference */
1652 ref = pat_ref_lookup(rule->arg.map.ref);
1653 if (!ref)
1654 continue;
1655
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001656 /* allocate key */
1657 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001658 if (!key) {
1659 rule_ret = HTTP_RULE_RES_BADREQ;
1660 goto end;
1661 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001662
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001663 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001664 key->data = build_logline(s, key->area, key->size,
1665 &rule->arg.map.key);
1666 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001667
1668 /* perform update */
1669 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001670 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001671 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001672 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001673
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001674 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001675 break;
1676 }
1677
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001678 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001679 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001680 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001681
1682 /* collect reference */
1683 ref = pat_ref_lookup(rule->arg.map.ref);
1684 if (!ref)
1685 continue;
1686
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001687 /* allocate key */
1688 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001689 if (!key) {
1690 rule_ret = HTTP_RULE_RES_BADREQ;
1691 goto end;
1692 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001693
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001694 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001695 key->data = build_logline(s, key->area, key->size,
1696 &rule->arg.map.key);
1697 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001698
1699 /* perform update */
1700 /* add entry only if it does not already exist */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001701 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001702 if (pat_ref_find_elt(ref, key->area) == NULL)
1703 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001704 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001705
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001706 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001707 break;
1708 }
1709
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001710 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001711 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02001712 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001713
1714 /* collect reference */
1715 ref = pat_ref_lookup(rule->arg.map.ref);
1716 if (!ref)
1717 continue;
1718
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001719 /* allocate key */
1720 key = alloc_trash_chunk();
Christopher Fauletea827bd2018-11-15 15:34:11 +01001721 if (!key) {
1722 rule_ret = HTTP_RULE_RES_BADREQ;
1723 goto end;
1724 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001725
1726 /* allocate value */
1727 value = alloc_trash_chunk();
1728 if (!value) {
1729 free_trash_chunk(key);
Christopher Fauletea827bd2018-11-15 15:34:11 +01001730 rule_ret = HTTP_RULE_RES_BADREQ;
1731 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001732 }
1733
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001734 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001735 key->data = build_logline(s, key->area, key->size,
1736 &rule->arg.map.key);
1737 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001738
1739 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001740 value->data = build_logline(s, value->area,
1741 value->size,
1742 &rule->arg.map.value);
1743 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001744
1745 /* perform update */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001746 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001747 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001748 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001749 else
1750 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001751 pat_ref_add(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001752
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001753 free_trash_chunk(key);
1754 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001755 break;
1756 }
William Lallemand73025dd2014-04-24 14:38:37 +02001757
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001758 case ACT_HTTP_EARLY_HINT:
1759 if (!(txn->req.flags & HTTP_MSGF_VER_11))
1760 break;
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001761 early_hints = http_apply_early_hint_rule(s, early_hints,
1762 rule->arg.early_hint.name,
1763 rule->arg.early_hint.name_len,
1764 &rule->arg.early_hint.fmt);
1765 if (!early_hints) {
Christopher Fauletea827bd2018-11-15 15:34:11 +01001766 rule_ret = HTTP_RULE_RES_DONE;
1767 goto end;
1768 }
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001769 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02001770 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01001771 if ((s->req.flags & CF_READ_ERROR) ||
1772 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
1773 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
1774 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02001775 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02001776
Willy Tarreauacc98002015-09-27 23:34:39 +02001777 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02001778 case ACT_RET_ERR:
1779 case ACT_RET_CONT:
1780 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02001781 case ACT_RET_STOP:
Christopher Fauletea827bd2018-11-15 15:34:11 +01001782 rule_ret = HTTP_RULE_RES_DONE;
1783 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02001784 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02001785 s->current_rule = rule;
Christopher Fauletea827bd2018-11-15 15:34:11 +01001786 rule_ret = HTTP_RULE_RES_YIELD;
1787 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001788 }
William Lallemand73025dd2014-04-24 14:38:37 +02001789 break;
1790
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001791 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02001792 /* Note: only the first valid tracking parameter of each
1793 * applies.
1794 */
1795
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001796 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02001797 struct stktable *t;
1798 struct stksess *ts;
1799 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001800 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02001801
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02001802 t = rule->arg.trk_ctr.table.t;
1803 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 +02001804
1805 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001806 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02001807
1808 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02001809 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1810 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1811 if (ptr1 || ptr2) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001812 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02001813
Emeric Brun819fc6f2017-06-13 19:37:32 +02001814 if (ptr1)
1815 stktable_data_cast(ptr1, http_req_cnt)++;
1816
1817 if (ptr2)
1818 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
1819 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
1820
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001821 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun0fed0b02017-11-29 16:15:07 +01001822
1823 /* If data was modified, we need to touch to re-schedule sync */
1824 stktable_touch_local(t, ts, 0);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001825 }
Willy Tarreau09448f72014-06-25 18:12:15 +02001826
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001827 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001828 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02001829 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02001830 }
1831 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02001832 break;
1833
Joseph Herlant5ba80252018-11-15 09:25:36 -08001834 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02001835 default:
1836 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001837 }
1838 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01001839
Christopher Fauletea827bd2018-11-15 15:34:11 +01001840 end:
Christopher Faulet3c0544e2018-11-15 15:41:55 +01001841 if (early_hints) {
1842 http_send_early_hints(s, early_hints);
1843 free_trash_chunk(early_hints);
Frédéric Lécaille9ca51aa2018-11-12 10:06:54 +01001844 }
1845
Willy Tarreau96257ec2012-12-27 10:46:37 +01001846 /* we reached the end of the rules, nothing to report */
Christopher Fauletea827bd2018-11-15 15:34:11 +01001847 return rule_ret;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01001848}
1849
Willy Tarreau71241ab2012-12-27 11:30:54 +01001850
Willy Tarreau51d861a2015-05-22 17:30:48 +02001851/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
1852 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
1853 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
1854 * is returned, the process can continue the evaluation of next rule list. If
1855 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
1856 * is returned, it means the operation could not be processed and a server error
1857 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
1858 * deny rule. If *YIELD is returned, the caller must call again the function
1859 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001860 */
Christopher Faulet10079f52018-10-03 15:17:28 +02001861enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001862http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001863{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001864 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02001865 struct http_txn *txn = s->txn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02001866 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001867 struct hdr_ctx ctx;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001868 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Willy Tarreauacc98002015-09-27 23:34:39 +02001869 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001870
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001871 /* If "the current_rule_list" match the executed rule list, we are in
1872 * resume condition. If a resume is needed it is always in the action
1873 * and never in the ACL or converters. In this case, we initialise the
1874 * current rule, and go to the action execution point.
1875 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02001876 if (s->current_rule) {
1877 rule = s->current_rule;
1878 s->current_rule = NULL;
1879 if (s->current_rule_list == rules)
1880 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001881 }
1882 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02001883
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001884 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001885
1886 /* check optional condition */
1887 if (rule->cond) {
1888 int ret;
1889
Willy Tarreau192252e2015-04-04 01:47:55 +02001890 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001891 ret = acl_pass(ret);
1892
1893 if (rule->cond->pol == ACL_COND_UNLESS)
1894 ret = !ret;
1895
1896 if (!ret) /* condition not matched */
1897 continue;
1898 }
1899
Willy Tarreauacc98002015-09-27 23:34:39 +02001900 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01001901resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001902 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001903 case ACT_ACTION_ALLOW:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001904 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
1905 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001906
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001907 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001908 txn->flags |= TX_SVDENY;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001909 rule_ret = HTTP_RULE_RES_STOP;
1910 goto end;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001911
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001912 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02001913 s->task->nice = rule->arg.nice;
1914 break;
1915
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001916 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001917 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02001918 break;
1919
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001920 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01001921 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Willy Tarreau51347ed2013-06-11 19:34:13 +02001922 break;
1923
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001924 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02001925 s->logs.level = rule->arg.loglevel;
1926 break;
1927
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001928 case ACT_HTTP_REPLACE_HDR:
1929 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01001930 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
1931 rule->arg.hdr_add.name_len,
1932 &rule->arg.hdr_add.fmt,
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001933 &rule->arg.hdr_add.re, rule->action)) {
1934 rule_ret = HTTP_RULE_RES_BADREQ;
1935 goto end;
1936 }
Sasha Pachev218f0642014-06-16 12:05:59 -06001937 break;
1938
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001939 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001940 ctx.idx = 0;
1941 /* remove all occurrences of the header */
1942 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001943 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001944 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
1945 }
Willy Tarreau85603282015-01-21 20:39:27 +01001946 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001947
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001948 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001949 case ACT_HTTP_ADD_HDR: {
Willy Tarreau83061a82018-07-13 11:56:34 +02001950 struct buffer *replace;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001951
1952 replace = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01001953 if (!replace) {
1954 rule_ret = HTTP_RULE_RES_BADREQ;
1955 goto end;
1956 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001957
1958 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001959 memcpy(replace->area, rule->arg.hdr_add.name,
1960 rule->arg.hdr_add.name_len);
1961 replace->data = rule->arg.hdr_add.name_len;
1962 replace->area[replace->data++] = ':';
1963 replace->area[replace->data++] = ' ';
1964 replace->data += build_logline(s,
1965 replace->area + replace->data,
1966 replace->size - replace->data,
1967 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01001968
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02001969 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01001970 /* remove all occurrences of the header */
1971 ctx.idx = 0;
1972 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02001973 ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau85603282015-01-21 20:39:27 +01001974 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
1975 }
1976 }
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001977
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001978 if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) {
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001979 static unsigned char rate_limit = 0;
1980
1981 if ((rate_limit++ & 255) == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001982 replace->area[rule->arg.hdr_add.name_len] = 0;
1983 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,
1984 replace->area, s->uniq_id);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001985 }
1986
Olivier Houcharda798bf52019-03-08 18:52:00 +01001987 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001988 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001989 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001990 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001991 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001992 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001993 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001994 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001995
1996 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001997 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001998 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02001999
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002000 case ACT_HTTP_DEL_ACL:
2001 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002002 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002003 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002004
2005 /* collect reference */
2006 ref = pat_ref_lookup(rule->arg.map.ref);
2007 if (!ref)
2008 continue;
2009
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002010 /* allocate key */
2011 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002012 if (!key) {
2013 rule_ret = HTTP_RULE_RES_BADREQ;
2014 goto end;
2015 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002016
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002017 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002018 key->data = build_logline(s, key->area, key->size,
2019 &rule->arg.map.key);
2020 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002021
2022 /* perform update */
2023 /* returned code: 1=ok, 0=ko */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002024 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002025 pat_ref_delete(ref, key->area);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002026 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002027
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002028 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002029 break;
2030 }
2031
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002032 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002033 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002034 struct buffer *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002035
2036 /* collect reference */
2037 ref = pat_ref_lookup(rule->arg.map.ref);
2038 if (!ref)
2039 continue;
2040
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002041 /* allocate key */
2042 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002043 if (!key) {
2044 rule_ret = HTTP_RULE_RES_BADREQ;
2045 goto end;
2046 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002047
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002048 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002049 key->data = build_logline(s, key->area, key->size,
2050 &rule->arg.map.key);
2051 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002052
2053 /* perform update */
2054 /* check if the entry already exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002055 if (pat_ref_find_elt(ref, key->area) == NULL)
2056 pat_ref_add(ref, key->area, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002057
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002058 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002059 break;
2060 }
2061
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002062 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002063 struct pat_ref *ref;
Willy Tarreau83061a82018-07-13 11:56:34 +02002064 struct buffer *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002065
2066 /* collect reference */
2067 ref = pat_ref_lookup(rule->arg.map.ref);
2068 if (!ref)
2069 continue;
2070
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002071 /* allocate key */
2072 key = alloc_trash_chunk();
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002073 if (!key) {
2074 rule_ret = HTTP_RULE_RES_BADREQ;
2075 goto end;
2076 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002077
2078 /* allocate value */
2079 value = alloc_trash_chunk();
2080 if (!value) {
2081 free_trash_chunk(key);
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002082 rule_ret = HTTP_RULE_RES_BADREQ;
2083 goto end;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002084 }
2085
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002086 /* collect key */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002087 key->data = build_logline(s, key->area, key->size,
2088 &rule->arg.map.key);
2089 key->area[key->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002090
2091 /* collect value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002092 value->data = build_logline(s, value->area,
2093 value->size,
2094 &rule->arg.map.value);
2095 value->area[value->data] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002096
2097 /* perform update */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002098 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002099 if (pat_ref_find_elt(ref, key->area) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002100 /* update entry if it exists */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002101 pat_ref_set(ref, key->area, value->area, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002102 else
2103 /* insert a new entry */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002104 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002105 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002106 free_trash_chunk(key);
2107 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002108 break;
2109 }
William Lallemand73025dd2014-04-24 14:38:37 +02002110
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002111 case ACT_HTTP_REDIR:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002112 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002113 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002114 rule_ret = HTTP_RULE_RES_BADREQ;
2115 goto end;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002116
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002117 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2118 /* Note: only the first valid tracking parameter of each
2119 * applies.
2120 */
2121
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002122 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002123 struct stktable *t;
2124 struct stksess *ts;
2125 struct stktable_key *key;
2126 void *ptr;
2127
2128 t = rule->arg.trk_ctr.table.t;
2129 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2130
2131 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002132 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002133
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002134 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002135
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002136 /* let's count a new HTTP request as it's the first time we do it */
2137 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2138 if (ptr)
2139 stktable_data_cast(ptr, http_req_cnt)++;
2140
2141 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2142 if (ptr)
2143 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2144 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2145
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002146 /* When the client triggers a 4xx from the server, it's most often due
2147 * to a missing object or permission. These events should be tracked
2148 * because if they happen often, it may indicate a brute force or a
2149 * vulnerability scan. Normally this is done when receiving the response
2150 * but here we're tracking after this ought to have been done so we have
2151 * to do it on purpose.
2152 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002153 if ((unsigned)(txn->status - 400) < 100) {
2154 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2155 if (ptr)
2156 stktable_data_cast(ptr, http_err_cnt)++;
2157
2158 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2159 if (ptr)
2160 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2161 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2162 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02002163
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002164 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02002165
Emeric Brun0fed0b02017-11-29 16:15:07 +01002166 /* If data was modified, we need to touch to re-schedule sync */
2167 stktable_touch_local(t, ts, 0);
2168
Emeric Brun819fc6f2017-06-13 19:37:32 +02002169 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2170 if (sess->fe != s->be)
2171 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2172
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002173 }
2174 }
2175 break;
2176
Thierry FOURNIER42148732015-09-02 17:17:33 +02002177 case ACT_CUSTOM:
Willy Tarreau7aa15b02017-12-20 16:56:50 +01002178 if ((s->req.flags & CF_READ_ERROR) ||
2179 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2180 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2181 (px->options & PR_O_ABRT_CLOSE)))
Willy Tarreauacc98002015-09-27 23:34:39 +02002182 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002183
Willy Tarreauacc98002015-09-27 23:34:39 +02002184 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002185 case ACT_RET_ERR:
2186 case ACT_RET_CONT:
2187 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002188 case ACT_RET_STOP:
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002189 rule_ret = HTTP_RULE_RES_STOP;
2190 goto end;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002191 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002192 s->current_rule = rule;
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002193 rule_ret = HTTP_RULE_RES_YIELD;
2194 goto end;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002195 }
William Lallemand73025dd2014-04-24 14:38:37 +02002196 break;
2197
Joseph Herlant5ba80252018-11-15 09:25:36 -08002198 /* other flags exists, but normally, they never be matched. */
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002199 default:
2200 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002201 }
2202 }
2203
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002204 end:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002205 /* we reached the end of the rules, nothing to report */
Christopher Faulet6c243eb2018-11-15 15:40:29 +01002206 return rule_ret;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002207}
2208
2209
Willy Tarreau71241ab2012-12-27 11:30:54 +01002210/* Perform an HTTP redirect based on the information in <rule>. The function
2211 * returns non-zero on success, or zero in case of a, irrecoverable error such
2212 * as too large a request to build a valid response.
2213 */
Christopher Faulet10079f52018-10-03 15:17:28 +02002214int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01002215{
Willy Tarreaub329a312015-05-22 16:27:37 +02002216 struct http_msg *req = &txn->req;
2217 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002218 const char *msg_fmt;
Willy Tarreau83061a82018-07-13 11:56:34 +02002219 struct buffer *chunk;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002220 int ret = 0;
2221
Christopher Fauletf2824e62018-10-01 12:12:37 +02002222 if (IS_HTX_STRM(s))
2223 return htx_apply_redirect_rule(rule, s, txn);
2224
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002225 chunk = alloc_trash_chunk();
2226 if (!chunk)
2227 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002228
2229 /* build redirect message */
2230 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04002231 case 308:
2232 msg_fmt = HTTP_308;
2233 break;
2234 case 307:
2235 msg_fmt = HTTP_307;
2236 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002237 case 303:
2238 msg_fmt = HTTP_303;
2239 break;
2240 case 301:
2241 msg_fmt = HTTP_301;
2242 break;
2243 case 302:
2244 default:
2245 msg_fmt = HTTP_302;
2246 break;
2247 }
2248
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002249 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2250 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002251
2252 switch(rule->type) {
2253 case REDIRECT_TYPE_SCHEME: {
2254 const char *path;
2255 const char *host;
2256 struct hdr_ctx ctx;
2257 int pathlen;
2258 int hostlen;
2259
2260 host = "";
2261 hostlen = 0;
2262 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02002263 if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002264 host = ctx.line + ctx.val;
2265 hostlen = ctx.vlen;
2266 }
2267
Willy Tarreau6b952c82018-09-10 17:45:34 +02002268 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002269 /* build message using path */
2270 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002271 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002272 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2273 int qs = 0;
2274 while (qs < pathlen) {
2275 if (path[qs] == '?') {
2276 pathlen = qs;
2277 break;
2278 }
2279 qs++;
2280 }
2281 }
2282 } else {
2283 path = "/";
2284 pathlen = 1;
2285 }
2286
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002287 if (rule->rdr_str) { /* this is an old "redirect" rule */
2288 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002289 if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002290 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002291
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002292 /* add scheme */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002293 memcpy(chunk->area + chunk->data, rule->rdr_str,
2294 rule->rdr_len);
2295 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002296 }
2297 else {
2298 /* add scheme with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002299 chunk->data += build_logline(s,
2300 chunk->area + chunk->data,
2301 chunk->size - chunk->data,
2302 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002303
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002304 /* check if we can add scheme + "://" + host + path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002305 if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002306 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002307 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002308 /* add "://" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002309 memcpy(chunk->area + chunk->data, "://", 3);
2310 chunk->data += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002311
2312 /* add host */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002313 memcpy(chunk->area + chunk->data, host, hostlen);
2314 chunk->data += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002315
2316 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002317 memcpy(chunk->area + chunk->data, path, pathlen);
2318 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002319
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002320 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002321 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002322 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002323 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002324 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002325 chunk->area[chunk->data] = '/';
2326 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002327 }
2328
2329 break;
2330 }
2331 case REDIRECT_TYPE_PREFIX: {
2332 const char *path;
2333 int pathlen;
2334
Willy Tarreau6b952c82018-09-10 17:45:34 +02002335 path = http_txn_get_path(txn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002336 /* build message using path */
2337 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002338 pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002339 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2340 int qs = 0;
2341 while (qs < pathlen) {
2342 if (path[qs] == '?') {
2343 pathlen = qs;
2344 break;
2345 }
2346 qs++;
2347 }
2348 }
2349 } else {
2350 path = "/";
2351 pathlen = 1;
2352 }
2353
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002354 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002355 if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002356 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002357
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002358 /* add prefix. Note that if prefix == "/", we don't want to
2359 * add anything, otherwise it makes it hard for the user to
2360 * configure a self-redirection.
2361 */
2362 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002363 memcpy(chunk->area + chunk->data,
2364 rule->rdr_str, rule->rdr_len);
2365 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002366 }
2367 }
2368 else {
2369 /* add prefix with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002370 chunk->data += build_logline(s,
2371 chunk->area + chunk->data,
2372 chunk->size - chunk->data,
2373 &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002374
2375 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002376 if (chunk->data + pathlen > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002377 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002378 }
2379
2380 /* add path */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002381 memcpy(chunk->area + chunk->data, path, pathlen);
2382 chunk->data += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002383
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002384 /* append a slash at the end of the location if needed and missing */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002385 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002386 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002387 if (chunk->data > chunk->size - 5)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002388 goto leave;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002389 chunk->area[chunk->data] = '/';
2390 chunk->data++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002391 }
2392
2393 break;
2394 }
2395 case REDIRECT_TYPE_LOCATION:
2396 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002397 if (rule->rdr_str) { /* this is an old "redirect" rule */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002398 if (chunk->data + rule->rdr_len > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002399 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002400
2401 /* add location */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002402 memcpy(chunk->area + chunk->data, rule->rdr_str,
2403 rule->rdr_len);
2404 chunk->data += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002405 }
2406 else {
2407 /* add location with executing log format */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002408 chunk->data += build_logline(s,
2409 chunk->area + chunk->data,
2410 chunk->size - chunk->data,
2411 &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002412
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002413 /* Check left length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002414 if (chunk->data > chunk->size - 4)
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002415 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01002416 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002417 break;
2418 }
2419
2420 if (rule->cookie_len) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002421 memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14);
2422 chunk->data += 14;
2423 memcpy(chunk->area + chunk->data, rule->cookie_str,
2424 rule->cookie_len);
2425 chunk->data += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002426 }
2427
Willy Tarreau19b14122017-02-28 09:48:11 +01002428 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01002429 txn->status = rule->code;
2430 /* let's log the request time */
2431 s->logs.tv_request = now;
2432
Christopher Fauletbe821b92017-03-30 11:21:53 +02002433 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01002434 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
2435 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
2436 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02002437 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002438 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002439 memcpy(chunk->area + chunk->data,
2440 "\r\nProxy-Connection: keep-alive", 30);
2441 chunk->data += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002442 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002443 memcpy(chunk->area + chunk->data,
2444 "\r\nConnection: keep-alive", 24);
2445 chunk->data += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002446 }
2447 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002448 memcpy(chunk->area + chunk->data, "\r\n\r\n", 4);
2449 chunk->data += 4;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002450 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002451 co_inject(res->chn, chunk->area, chunk->data);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002452 /* "eat" the request */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002453 b_del(&req->chn->buf, req->sov);
Willy Tarreaub329a312015-05-22 16:27:37 +02002454 req->next -= req->sov;
2455 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002456 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01002457 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02002458 req->msg_state = HTTP_MSG_CLOSED;
2459 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02002460 /* Trim any possible response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002461 b_set_data(&res->chn->buf, co_data(res->chn));
Willy Tarreau51d861a2015-05-22 17:30:48 +02002462 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02002463 /* let the server side turn to SI_ST_CLO */
2464 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01002465 } else {
2466 /* keep-alive not possible */
2467 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002468 memcpy(chunk->area + chunk->data,
2469 "\r\nProxy-Connection: close\r\n\r\n", 29);
2470 chunk->data += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002471 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002472 memcpy(chunk->area + chunk->data,
2473 "\r\nConnection: close\r\n\r\n", 23);
2474 chunk->data += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002475 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002476 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002477 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002478 }
2479
Willy Tarreaue7dff022015-04-03 01:14:29 +02002480 if (!(s->flags & SF_ERR_MASK))
2481 s->flags |= SF_ERR_LOCAL;
2482 if (!(s->flags & SF_FINST_MASK))
2483 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002484
Thierry FOURNIER0d945762017-01-28 07:39:53 +01002485 ret = 1;
2486 leave:
2487 free_trash_chunk(chunk);
2488 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002489}
2490
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002491/* This stream analyser runs all HTTP request processing which is common to
2492 * frontends and backends, which means blocking ACLs, filters, connection-close,
2493 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002494 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002495 * either needs more data or wants to immediately abort the request (eg: deny,
2496 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002497 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002498int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002499{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002500 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002501 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02002502 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002503 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002504 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02002505 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002506 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002507 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02002508
Christopher Faulete0768eb2018-10-03 16:38:02 +02002509 if (IS_HTX_STRM(s))
2510 return htx_process_req_common(s, req, an_bit, px);
2511
Willy Tarreau655dce92009-11-08 13:10:58 +01002512 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002513 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002514 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02002515 }
2516
Christopher Faulet45073512018-07-20 10:16:29 +02002517 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 +02002518 now_ms, __FUNCTION__,
2519 s,
2520 req,
2521 req->rex, req->wex,
2522 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002523 ci_data(req),
Willy Tarreaud787e662009-07-07 10:14:51 +02002524 req->analysers);
2525
Willy Tarreau65410832014-04-28 21:25:43 +02002526 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02002527 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02002528
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002529 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02002530 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02002531 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01002532
Willy Tarreau0b748332014-04-29 00:13:29 +02002533 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002534 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2535 goto return_prx_yield;
2536
Willy Tarreau0b748332014-04-29 00:13:29 +02002537 case HTTP_RULE_RES_CONT:
2538 case HTTP_RULE_RES_STOP: /* nothing to do */
2539 break;
Willy Tarreau52542592014-04-28 18:33:26 +02002540
Willy Tarreau0b748332014-04-29 00:13:29 +02002541 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2542 if (txn->flags & TX_CLTARPIT)
2543 goto tarpit;
2544 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002545
Willy Tarreau0b748332014-04-29 00:13:29 +02002546 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
2547 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02002548
Willy Tarreau0b748332014-04-29 00:13:29 +02002549 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02002550 goto done;
2551
Willy Tarreau0b748332014-04-29 00:13:29 +02002552 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2553 goto return_bad_req;
2554 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002555 }
2556
Olivier Houchard25ae45a2017-11-29 19:51:19 +01002557 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
2558 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002559 struct hdr_ctx ctx;
2560
2561 ctx.idx = 0;
2562 if (!http_find_header2("Early-Data", strlen("Early-Data"),
Willy Tarreauf37954d2018-06-15 18:31:02 +02002563 ci_head(&s->req), &txn->hdr_idx, &ctx)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002564 if (unlikely(http_header_add_tail2(&txn->req,
2565 &txn->hdr_idx, "Early-Data: 1",
Christopher Faulet005e79e2018-07-20 09:54:26 +02002566 strlen("Early-Data: 1")) < 0)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002567 goto return_bad_req;
2568 }
2569 }
2570
2571 }
2572
Willy Tarreau52542592014-04-28 18:33:26 +02002573 /* OK at this stage, we know that the request was accepted according to
2574 * the http-request rules, we can check for the stats. Note that the
2575 * URI is detected *before* the req* rules in order not to be affected
2576 * by a possible reqrep, while they are processed *after* so that a
2577 * reqdeny can still block them. This clearly needs to change in 1.6!
2578 */
Willy Tarreau350f4872014-11-28 14:42:25 +01002579 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02002580 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01002581 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02002582 txn->status = 500;
2583 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002584 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002585
Willy Tarreaue7dff022015-04-03 01:14:29 +02002586 if (!(s->flags & SF_ERR_MASK))
2587 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02002588 goto return_prx_cond;
2589 }
2590
2591 /* parse the whole stats request and extract the relevant information */
2592 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02002593 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02002594 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002595
Willy Tarreau0b748332014-04-29 00:13:29 +02002596 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
2597 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02002598
Willy Tarreau0b748332014-04-29 00:13:29 +02002599 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
2600 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002601 }
2602
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002603 /* evaluate the req* rules except reqadd */
2604 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002605 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002606 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002607
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002608 if (txn->flags & TX_CLDENY)
2609 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02002610
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002611 if (txn->flags & TX_CLTARPIT) {
2612 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002613 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002614 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002615 }
Willy Tarreau06619262006-12-17 08:37:22 +01002616
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002617 /* add request headers from the rule sets in the same order */
2618 list_for_each_entry(wl, &px->req_add, list) {
2619 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002620 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002621 ret = acl_pass(ret);
2622 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2623 ret = !ret;
2624 if (!ret)
2625 continue;
2626 }
2627
Christopher Faulet10079f52018-10-03 15:17:28 +02002628 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002629 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002630 }
2631
Willy Tarreau52542592014-04-28 18:33:26 +02002632
2633 /* Proceed with the stats now. */
William Lallemand71bd11a2017-11-20 19:13:14 +01002634 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
2635 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002636 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002637 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +01002638 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01002639
Willy Tarreaue7dff022015-04-03 01:14:29 +02002640 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
2641 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
2642 if (!(s->flags & SF_FINST_MASK))
2643 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01002644
Willy Tarreau70730dd2014-04-24 18:06:27 +02002645 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002646 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
2647 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002648 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002649 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002650 }
Willy Tarreaub2513902006-12-17 14:52:38 +01002651
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002652 /* check whether we have some ACLs set to redirect this request */
2653 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01002654 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01002655 int ret;
2656
Willy Tarreau192252e2015-04-04 01:47:55 +02002657 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01002658 ret = acl_pass(ret);
2659 if (rule->cond->pol == ACL_COND_UNLESS)
2660 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01002661 if (!ret)
2662 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01002663 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01002664 if (!http_apply_redirect_rule(rule, s, txn))
2665 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002666 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002667 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002668
Willy Tarreau2be39392010-01-03 17:24:51 +01002669 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
2670 * If this happens, then the data will not come immediately, so we must
2671 * send all what we have without waiting. Note that due to the small gain
2672 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002673 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01002674 * itself once used.
2675 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002676 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01002677
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002678 done: /* done with this analyser, continue with next ones that the calling
2679 * points will have set, if any.
2680 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002681 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002682 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
2683 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002684 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02002685
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002686 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02002687 /* Allow cookie logging
2688 */
2689 if (s->be->cookie_name || sess->fe->capture_name)
2690 manage_client_side_cookies(s, req);
2691
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002692 /* When a connection is tarpitted, we use the tarpit timeout,
2693 * which may be the same as the connect timeout if unspecified.
2694 * If unset, then set it to zero because we really want it to
2695 * eventually expire. We build the tarpit as an analyser.
2696 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002697 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002698
2699 /* wipe the request out so that we can drop the connection early
2700 * if the client closes first.
2701 */
2702 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002703
Jarno Huuskonen800d1762017-03-06 14:56:36 +02002704 txn->status = http_err_codes[deny_status];
2705
Christopher Faulet0184ea72017-01-05 14:06:34 +01002706 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002707 req->analysers |= AN_REQ_HTTP_TARPIT;
2708 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2709 if (!req->analyse_exp)
2710 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02002711 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +01002712 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002713 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002714 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002715 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002716 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02002717 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002718
2719 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002720
2721 /* Allow cookie logging
2722 */
2723 if (s->be->cookie_name || sess->fe->capture_name)
2724 manage_client_side_cookies(s, req);
2725
Willy Tarreau0b748332014-04-29 00:13:29 +02002726 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002727 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002728 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002729 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02002730 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +01002731 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002732 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002733 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002734 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002735 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02002736 goto return_prx_cond;
2737
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002738 return_bad_req:
2739 /* We centralize bad requests processing here */
2740 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2741 /* we detected a parsing error. We want to archive this request
2742 * in the dedicated proxy area for later troubleshooting.
2743 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02002744 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002745 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02002746
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002747 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002748 txn->req.msg_state = HTTP_MSG_ERROR;
2749 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002750 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002751
Olivier Houcharda798bf52019-03-08 18:52:00 +01002752 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002753 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002754 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02002755
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002756 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002757 if (!(s->flags & SF_ERR_MASK))
2758 s->flags |= SF_ERR_PRXCOND;
2759 if (!(s->flags & SF_FINST_MASK))
2760 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01002761
Christopher Faulet0184ea72017-01-05 14:06:34 +01002762 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002763 req->analyse_exp = TICK_ETERNITY;
2764 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002765
2766 return_prx_yield:
2767 channel_dont_connect(req);
2768 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002769}
Willy Tarreau58f10d72006-12-04 02:26:12 +01002770
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002771/* This function performs all the processing enabled for the current request.
2772 * It returns 1 if the processing can continue on next analysers, or zero if it
2773 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002774 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002775 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002776int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002777{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002778 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002779 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002780 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02002781 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002782
Christopher Faulete0768eb2018-10-03 16:38:02 +02002783 if (IS_HTX_STRM(s))
2784 return htx_process_request(s, req, an_bit);
2785
Willy Tarreau655dce92009-11-08 13:10:58 +01002786 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002787 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002788 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002789 return 0;
2790 }
2791
Christopher Faulet45073512018-07-20 10:16:29 +02002792 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 +02002793 now_ms, __FUNCTION__,
2794 s,
2795 req,
2796 req->rex, req->wex,
2797 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02002798 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002799 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01002800
Willy Tarreau59234e92008-11-30 23:51:27 +01002801 /*
2802 * Right now, we know that we have processed the entire headers
2803 * and that unwanted requests have been filtered out. We can do
2804 * whatever we want with the remaining request. Also, now we
2805 * may have separate values for ->fe, ->be.
2806 */
Willy Tarreau06619262006-12-17 08:37:22 +01002807
Willy Tarreau59234e92008-11-30 23:51:27 +01002808 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002809 * If HTTP PROXY is set we simply get remote server address parsing
2810 * incoming request. Note that this requires that a connection is
2811 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01002812 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02002813 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002814 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002815 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002816
Willy Tarreau9471b8c2013-12-15 13:31:35 +01002817 /* Note that for now we don't reuse existing proxy connections */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002818 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002819 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002820 txn->req.msg_state = HTTP_MSG_ERROR;
2821 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002822 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002823 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002824
Willy Tarreaue7dff022015-04-03 01:14:29 +02002825 if (!(s->flags & SF_ERR_MASK))
2826 s->flags |= SF_ERR_RESOURCE;
2827 if (!(s->flags & SF_FINST_MASK))
2828 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02002829
2830 return 0;
2831 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002832
Willy Tarreau6b952c82018-09-10 17:45:34 +02002833 path = http_txn_get_path(txn);
Willy Tarreauf37954d2018-06-15 18:31:02 +02002834 if (url2sa(ci_head(req) + msg->sl.rq.u,
2835 path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l,
Christopher Faulet11ebb202018-04-13 15:53:12 +02002836 &conn->addr.to, NULL) == -1)
2837 goto return_bad_req;
2838
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002839 /* if the path was found, we have to remove everything between
Willy Tarreauf37954d2018-06-15 18:31:02 +02002840 * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not
2841 * found, we need to replace from ci_head(req) + msg->sl.rq.u for
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002842 * u_l characters by a single "/".
2843 */
2844 if (path) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002845 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002846 char *cur_end = cur_ptr + txn->req.sl.rq.l;
2847 int delta;
2848
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002849 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002850 http_msg_move_end(&txn->req, delta);
2851 cur_end += delta;
2852 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
2853 goto return_bad_req;
2854 }
2855 else {
Willy Tarreauf37954d2018-06-15 18:31:02 +02002856 char *cur_ptr = ci_head(req);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002857 char *cur_end = cur_ptr + txn->req.sl.rq.l;
2858 int delta;
2859
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002860 delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002861 cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01002862 http_msg_move_end(&txn->req, delta);
2863 cur_end += delta;
2864 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
2865 goto return_bad_req;
2866 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002867 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002868
Willy Tarreau59234e92008-11-30 23:51:27 +01002869 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01002870 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01002871 * Note that doing so might move headers in the request, but
2872 * the fields will stay coherent and the URI will not move.
2873 * This should only be performed in the backend.
2874 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02002875 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01002876 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02002877
William Lallemanda73203e2012-03-12 12:48:57 +01002878 /* add unique-id if "header-unique-id" is specified */
2879
Thierry Fournierf4011dd2016-03-29 17:23:51 +02002880 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01002881 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002882 goto return_bad_req;
2883 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002884 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02002885 }
William Lallemanda73203e2012-03-12 12:48:57 +01002886
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002887 if (sess->fe->header_unique_id && s->unique_id) {
Willy Tarreau5f6333c2018-08-22 05:14:37 +02002888 if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01002889 goto return_bad_req;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002890 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01002891 goto return_bad_req;
2892 }
2893
Cyril Bontéb21570a2009-11-29 20:04:48 +01002894 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002895 * 9: add X-Forwarded-For if either the frontend or the backend
2896 * asks for it.
2897 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002898 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02002899 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002900 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
2901 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
2902 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreauf37954d2018-06-15 18:31:02 +02002903 ci_head(req), &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02002904 /* The header is set to be added only if none is present
2905 * and we found it, so don't do anything.
2906 */
2907 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002908 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002909 /* Add an X-Forwarded-For header unless the source IP is
2910 * in the 'except' network range.
2911 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002912 if ((!sess->fe->except_mask.s_addr ||
2913 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
2914 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01002915 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002916 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01002917 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01002918 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01002919 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002920 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02002921
2922 /* Note: we rely on the backend to get the header name to be used for
2923 * x-forwarded-for, because the header is really meant for the backends.
2924 * However, if the backend did not specify any option, we have to rely
2925 * on the frontend's header name.
2926 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002927 if (s->be->fwdfor_hdr_len) {
2928 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002929 memcpy(trash.area,
2930 s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02002931 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002932 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002933 memcpy(trash.area,
2934 sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01002935 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002936 len += snprintf(trash.area + len,
2937 trash.size - len,
2938 ": %d.%d.%d.%d", pn[0], pn[1],
2939 pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01002940
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002941 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01002942 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01002943 }
2944 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002945 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002946 /* FIXME: for the sake of completeness, we should also support
2947 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002948 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002949 int len;
2950 char pn[INET6_ADDRSTRLEN];
2951 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002952 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01002953 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002954
Willy Tarreau59234e92008-11-30 23:51:27 +01002955 /* Note: we rely on the backend to get the header name to be used for
2956 * x-forwarded-for, because the header is really meant for the backends.
2957 * However, if the backend did not specify any option, we have to rely
2958 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002959 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002960 if (s->be->fwdfor_hdr_len) {
2961 len = s->be->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002962 memcpy(trash.area, s->be->fwdfor_hdr_name,
2963 len);
Willy Tarreau59234e92008-11-30 23:51:27 +01002964 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002965 len = sess->fe->fwdfor_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002966 memcpy(trash.area, sess->fe->fwdfor_hdr_name,
2967 len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02002968 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002969 len += snprintf(trash.area + len, trash.size - len,
2970 ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02002971
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002972 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01002973 goto return_bad_req;
2974 }
2975 }
2976
2977 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02002978 * 10: add X-Original-To if either the frontend or the backend
2979 * asks for it.
2980 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002981 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02002982
2983 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002984 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02002985 /* Add an X-Original-To header unless the destination IP is
2986 * in the 'except' network range.
2987 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002988 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02002989
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002990 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002991 ((!sess->fe->except_mask_to.s_addr ||
2992 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
2993 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02002994 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002995 (((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 +02002996 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02002997 int len;
2998 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002999 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003000
3001 /* Note: we rely on the backend to get the header name to be used for
3002 * x-original-to, because the header is really meant for the backends.
3003 * However, if the backend did not specify any option, we have to rely
3004 * on the frontend's header name.
3005 */
3006 if (s->be->orgto_hdr_len) {
3007 len = s->be->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003008 memcpy(trash.area,
3009 s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003010 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003011 len = sess->fe->orgto_hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003012 memcpy(trash.area,
3013 sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003014 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003015 len += snprintf(trash.area + len,
3016 trash.size - len,
3017 ": %d.%d.%d.%d", pn[0], pn[1],
3018 pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003019
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003020 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003021 goto return_bad_req;
3022 }
3023 }
3024 }
3025
Willy Tarreau50fc7772012-11-11 22:19:57 +01003026 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3027 * If an "Upgrade" token is found, the header is left untouched in order not to have
3028 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3029 * "Upgrade" is present in the Connection header.
3030 */
Christopher Faulet315b39c2018-09-21 16:26:19 +02003031 if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003032 unsigned int want_flags = 0;
3033
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003034 if (msg->flags & HTTP_MSGF_VER_11) {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003035 if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003036 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003037 want_flags |= TX_CON_CLO_SET;
3038 } else {
Christopher Faulet315b39c2018-09-21 16:26:19 +02003039 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003040 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003041 want_flags |= TX_CON_KAL_SET;
3042 }
3043
3044 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003045 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003046 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003047
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003048
Willy Tarreau522d6c02009-12-06 18:49:18 +01003049 /* If we have no server assigned yet and we're balancing on url_param
3050 * with a POST request, we may be interested in checking the body for
3051 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003052 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003053 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +01003054 s->txn->meth == HTTP_METH_POST &&
3055 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003056 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003057 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003058 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003059 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003060
Christopher Fauletbe821b92017-03-30 11:21:53 +02003061 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3062 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +01003063
Christopher Fauletbe821b92017-03-30 11:21:53 +02003064 /* We expect some data from the client. Unless we know for sure
3065 * we already have a full request, we have to re-enable quick-ack
3066 * in case we previously disabled it, otherwise we might cause
3067 * the client to delay further data.
3068 */
3069 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletbe821b92017-03-30 11:21:53 +02003070 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003071 (msg->body_len > ci_data(req) - txn->req.eoh - 2)))
Willy Tarreau1a18b542018-12-11 16:37:42 +01003072 conn_set_quickack(cli_conn, 1);
Willy Tarreau03945942009-12-22 16:50:27 +01003073
Willy Tarreau59234e92008-11-30 23:51:27 +01003074 /*************************************************************
3075 * OK, that's finished for the headers. We have done what we *
3076 * could. Let's switch to the DATA state. *
3077 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003078 req->analyse_exp = TICK_ETERNITY;
3079 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003080
Willy Tarreau59234e92008-11-30 23:51:27 +01003081 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003082 /* OK let's go on with the BODY now */
3083 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003084
Willy Tarreau59234e92008-11-30 23:51:27 +01003085 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003086 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003087 /* we detected a parsing error. We want to archive this request
3088 * in the dedicated proxy area for later troubleshooting.
3089 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02003090 http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003091 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003092
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003093 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003094 txn->req.msg_state = HTTP_MSG_ERROR;
3095 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003096 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003097 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003098
Olivier Houcharda798bf52019-03-08 18:52:00 +01003099 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003100 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003101 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003102
Willy Tarreaue7dff022015-04-03 01:14:29 +02003103 if (!(s->flags & SF_ERR_MASK))
3104 s->flags |= SF_ERR_PRXCOND;
3105 if (!(s->flags & SF_FINST_MASK))
3106 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003107 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003108}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003109
Willy Tarreau60b85b02008-11-30 23:28:40 +01003110/* This function is an analyser which processes the HTTP tarpit. It always
3111 * returns zero, at the beginning because it prevents any other processing
3112 * from occurring, and at the end because it terminates the request.
3113 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003114int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003115{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003116 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003117
Christopher Faulete0768eb2018-10-03 16:38:02 +02003118 if (IS_HTX_STRM(s))
3119 return htx_process_tarpit(s, req, an_bit);
3120
Willy Tarreau60b85b02008-11-30 23:28:40 +01003121 /* This connection is being tarpitted. The CLIENT side has
3122 * already set the connect expiration date to the right
3123 * timeout. We just have to check that the client is still
3124 * there and that the timeout has not expired.
3125 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003126 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003127 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003128 !tick_is_expired(req->analyse_exp, now_ms))
3129 return 0;
3130
3131 /* We will set the queue timer to the time spent, just for
3132 * logging purposes. We fake a 500 server error, so that the
3133 * attacker will not suspect his connection has been tarpitted.
3134 * It will not cause trouble to the logs because we can exclude
3135 * the tarpitted connections by filtering on the 'PT' status flags.
3136 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003137 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3138
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003139 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003140 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003141
Christopher Faulet0184ea72017-01-05 14:06:34 +01003142 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003143 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003144
Willy Tarreaue7dff022015-04-03 01:14:29 +02003145 if (!(s->flags & SF_ERR_MASK))
3146 s->flags |= SF_ERR_PRXCOND;
3147 if (!(s->flags & SF_FINST_MASK))
3148 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003149 return 0;
3150}
3151
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003152/* This function is an analyser which waits for the HTTP request body. It waits
3153 * for either the buffer to be full, or the full advertised contents to have
3154 * reached the buffer. It must only be called after the standard HTTP request
3155 * processing has occurred, because it expects the request to be parsed and will
3156 * look for the Expect header. It may send a 100-Continue interim response. It
3157 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3158 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3159 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003160 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003161int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003162{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003163 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003164 struct http_txn *txn = s->txn;
3165 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003166
Christopher Faulete0768eb2018-10-03 16:38:02 +02003167 if (IS_HTX_STRM(s))
3168 return htx_wait_for_request_body(s, req, an_bit);
3169
Willy Tarreaud34af782008-11-30 23:36:37 +01003170 /* We have to parse the HTTP request body to find any required data.
3171 * "balance url_param check_post" should have been the only way to get
3172 * into this. We were brought here after HTTP header analysis, so all
3173 * related structures are ready.
3174 */
3175
Willy Tarreau890988f2014-04-10 11:59:33 +02003176 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3177 /* This is the first call */
3178 if (msg->msg_state < HTTP_MSG_BODY)
3179 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003180
Willy Tarreau890988f2014-04-10 11:59:33 +02003181 if (msg->msg_state < HTTP_MSG_100_SENT) {
3182 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3183 * send an HTTP/1.1 100 Continue intermediate response.
3184 */
3185 if (msg->flags & HTTP_MSGF_VER_11) {
3186 struct hdr_ctx ctx;
3187 ctx.idx = 0;
3188 /* Expect is allowed in 1.1, look for it */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003189 if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) &&
Willy Tarreau890988f2014-04-10 11:59:33 +02003190 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau04f1e2d2018-09-10 18:04:24 +02003191 co_inject(&s->res, HTTP_100.ptr, HTTP_100.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003192 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003193 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003194 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003195 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003196 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003197
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003198 /* we have msg->sov which points to the first byte of message body.
Willy Tarreauf37954d2018-06-15 18:31:02 +02003199 * ci_head(req) still points to the beginning of the message. We
Willy Tarreau877e78d2013-04-07 18:48:08 +02003200 * must save the body in msg->next because it survives buffer
3201 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003202 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003203 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003204
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003205 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003206 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3207 else
3208 msg->msg_state = HTTP_MSG_DATA;
3209 }
3210
Willy Tarreau890988f2014-04-10 11:59:33 +02003211 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3212 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003213 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003214 goto missing_data;
3215
3216 /* OK we have everything we need now */
3217 goto http_end;
3218 }
3219
3220 /* OK here we're parsing a chunked-encoded message */
3221
Willy Tarreau522d6c02009-12-06 18:49:18 +01003222 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003223 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003224 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003225 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003226 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003227 unsigned int chunk;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003228 int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003229
Willy Tarreau115acb92009-12-26 13:56:06 +01003230 if (!ret)
3231 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003232 else if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02003233 msg->err_pos = ci_data(req) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003234 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003235 msg->err_pos += req->buf.size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003236 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003237 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003238 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003239
3240 msg->chunk_len = chunk;
3241 msg->body_len += chunk;
3242
3243 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003244 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003245 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003246 }
3247
Willy Tarreaud98cf932009-12-27 22:54:55 +01003248 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003249 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3250 * for at least a whole chunk or the whole content length bytes after
3251 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003252 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003253 if (msg->msg_state == HTTP_MSG_TRAILERS)
3254 goto http_end;
3255
Willy Tarreaue115b492015-05-01 23:05:14 +02003256 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003257 goto http_end;
3258
3259 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003260 /* we get here if we need to wait for more data. If the buffer is full,
3261 * we have the maximum we can expect.
3262 */
Willy Tarreau23752332018-06-15 14:54:53 +02003263 if (channel_full(req, global.tune.maxrewrite))
Willy Tarreau31a19952014-04-10 11:50:37 +02003264 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01003265
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003266 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003267 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003268 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003269
Willy Tarreaue7dff022015-04-03 01:14:29 +02003270 if (!(s->flags & SF_ERR_MASK))
3271 s->flags |= SF_ERR_CLITO;
3272 if (!(s->flags & SF_FINST_MASK))
3273 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003274 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003275 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003276
3277 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02003278 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003279 /* Not enough data. We'll re-use the http-request
3280 * timeout here. Ideally, we should set the timeout
3281 * relative to the accept() date. We just set the
3282 * request timeout once at the beginning of the
3283 * request.
3284 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003285 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003286 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003287 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003288 return 0;
3289 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003290
3291 http_end:
3292 /* The situation will not evolve, so let's give up on the analysis. */
3293 s->logs.tv_request = now; /* update the request timer to reflect full request */
3294 req->analysers &= ~an_bit;
3295 req->analyse_exp = TICK_ETERNITY;
3296 return 1;
3297
3298 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003299 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003300 txn->req.msg_state = HTTP_MSG_ERROR;
3301 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003302 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003303
Willy Tarreaue7dff022015-04-03 01:14:29 +02003304 if (!(s->flags & SF_ERR_MASK))
3305 s->flags |= SF_ERR_PRXCOND;
3306 if (!(s->flags & SF_FINST_MASK))
3307 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02003308
Willy Tarreau522d6c02009-12-06 18:49:18 +01003309 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01003310 req->analysers &= AN_REQ_FLT_END;
Olivier Houcharda798bf52019-03-08 18:52:00 +01003311 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003312 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003313 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003314 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003315}
3316
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003317/* send a server's name with an outgoing request over an established connection.
3318 * Note: this function is designed to be called once the request has been scheduled
3319 * for being forwarded. This is the reason why it rewinds the buffer before
3320 * proceeding.
3321 */
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003322int http_send_name_header(struct stream *s, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003323
3324 struct hdr_ctx ctx;
Christopher Faulet27a3dc82018-10-23 15:34:07 +02003325 struct http_txn *txn = s->txn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003326 char *hdr_name = be->server_id_hdr_name;
3327 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003328 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003329 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003330 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003331
Christopher Faulet64159df2018-10-24 21:15:35 +02003332 if (IS_HTX_STRM(s))
3333 return htx_send_name_header(s, be, srv_name);
William Lallemandd9e90662012-01-30 17:27:17 +01003334 ctx.idx = 0;
3335
Willy Tarreau211cdec2014-04-17 20:18:08 +02003336 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003337 if (old_o) {
3338 /* The request was already skipped, let's restore it */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003339 c_rew(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003340 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003341 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003342 }
3343
Willy Tarreauf37954d2018-06-15 18:31:02 +02003344 old_i = ci_data(chn);
3345 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 -05003346 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003347 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003348 }
3349
3350 /* Add the new header requested with the server value */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003351 hdr_val = trash.area;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003352 memcpy(hdr_val, hdr_name, hdr_name_len);
3353 hdr_val += hdr_name_len;
3354 *hdr_val++ = ':';
3355 *hdr_val++ = ' ';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003356 hdr_val += strlcpy2(hdr_val, srv_name,
3357 trash.area + trash.size - hdr_val);
3358 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area,
3359 hdr_val - trash.area);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003360
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003361 if (old_o) {
3362 /* If this was a forwarded request, we must readjust the amount of
3363 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02003364 * variations. Note that the current state is >= HTTP_MSG_BODY,
3365 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003366 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003367 old_o += ci_data(chn) - old_i;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003368 c_adv(chn, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02003369 txn->req.next -= old_o;
3370 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003371 }
3372
Mark Lamourinec2247f02012-01-04 13:02:01 -05003373 return 0;
3374}
3375
Willy Tarreau610ecce2010-01-04 21:15:02 +01003376/* Terminate current transaction and prepare a new one. This is very tricky
3377 * right now but it works.
3378 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003379void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003380{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003381 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02003382 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01003383 struct proxy *be = s->be;
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003384 struct conn_stream *cs;
Willy Tarreau323a2d92015-08-04 19:00:17 +02003385 struct connection *srv_conn;
3386 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02003387 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01003388
Willy Tarreau610ecce2010-01-04 21:15:02 +01003389 /* FIXME: We need a more portable way of releasing a backend's and a
3390 * server's connections. We need a safer way to reinitialize buffer
3391 * flags. We also need a more accurate method for computing per-request
3392 * data.
3393 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003394 cs = objt_cs(s->si[1].end);
3395 srv_conn = cs_conn(cs);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003396
Willy Tarreau4213a112013-12-15 10:25:42 +01003397 /* unless we're doing keep-alive, we want to quickly close the connection
3398 * to the server.
3399 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003400 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003401 !si_conn_ready(&s->si[1]) || !srv_conn->owner) {
Willy Tarreau350f4872014-11-28 14:42:25 +01003402 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
3403 si_shutr(&s->si[1]);
3404 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01003405 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003406
Willy Tarreaue7dff022015-04-03 01:14:29 +02003407 if (s->flags & SF_BE_ASSIGNED) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003408 _HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003409 if (unlikely(s->srv_conn))
3410 sess_change_server(s, NULL);
3411 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003412
3413 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02003414 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003415
Willy Tarreaueee5b512015-04-03 23:46:31 +02003416 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003417 int n;
3418
Willy Tarreaueee5b512015-04-03 23:46:31 +02003419 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003420 if (n < 1 || n > 5)
3421 n = 0;
3422
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003423 if (fe->mode == PR_MODE_HTTP) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003424 _HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003425 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02003426 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01003427 (be->mode == PR_MODE_HTTP)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003428 _HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
3429 _HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003430 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003431 }
3432
3433 /* don't count other requests' data */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003434 s->logs.bytes_in -= ci_data(&s->req);
3435 s->logs.bytes_out -= ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003436
Willy Tarreau66425e32018-07-25 06:55:12 +02003437 /* we may need to know the position in the queue */
3438 pendconn_free(s);
3439
Willy Tarreau610ecce2010-01-04 21:15:02 +01003440 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003441 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02003442 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003443 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003444 s->do_log(s);
3445 }
3446
Willy Tarreaud713bcc2014-06-25 15:36:04 +02003447 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02003448 stream_stop_content_counters(s);
3449 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02003450
Willy Tarreau9efd7452018-05-31 14:48:54 +02003451 /* reset the profiling counter */
3452 s->task->calls = 0;
3453 s->task->cpu_time = 0;
3454 s->task->lat_time = 0;
3455 s->task->call_date = (profiling & HA_PROF_TASKS) ? now_mono_time() : 0;
3456
Willy Tarreau610ecce2010-01-04 21:15:02 +01003457 s->logs.accept_date = date; /* user-visible date for logging */
3458 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02003459 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
3460 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003461 tv_zero(&s->logs.tv_request);
3462 s->logs.t_queue = -1;
3463 s->logs.t_connect = -1;
3464 s->logs.t_data = -1;
3465 s->logs.t_close = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -04003466 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
3467 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003468
Willy Tarreauf37954d2018-06-15 18:31:02 +02003469 s->logs.bytes_in = s->req.total = ci_data(&s->req);
3470 s->logs.bytes_out = s->res.total = ci_data(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003471
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003472 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02003473 if (s->flags & SF_CURR_SESS) {
3474 s->flags &= ~SF_CURR_SESS;
Olivier Houcharda798bf52019-03-08 18:52:00 +01003475 _HA_ATOMIC_SUB(&__objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003476 }
Willy Tarreau858b1032015-12-07 17:04:59 +01003477 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003478 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003479 }
3480
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003481 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003482
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003483
3484 /* If we're doing keepalive, first call the mux detach() method
3485 * to let it know we want to detach without freing the connection.
3486 * We then can call si_release_endpoint() to destroy the conn_stream
Willy Tarreau4213a112013-12-15 10:25:42 +01003487 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003488 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Olivier Houcharda70e1762018-12-13 18:01:00 +01003489 !si_conn_ready(&s->si[1]) ||
3490 (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 +02003491 srv_conn = NULL;
Olivier Houchard985f1392018-11-30 17:31:52 +01003492 else if (!srv_conn->owner) {
3493 srv_conn->owner = s->sess;
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003494 /* Add it unconditionally to the session list, it'll be removed
3495 * later if needed by session_check_idle_conn(), once we'll
3496 * have released the endpoint and know if it no longer has
3497 * attached streams, and so an idling connection
3498 */
Olivier Houchard351411f2018-12-27 17:20:54 +01003499 if (!session_add_conn(s->sess, srv_conn, s->target)) {
3500 srv_conn->owner = NULL;
3501 /* Try to add the connection to the server idle list.
3502 * If it fails, as the connection no longer has an
3503 * owner, it will be destroy later by
3504 * si_release_endpoint(), anyway
3505 */
3506 srv_add_to_idle_list(objt_server(srv_conn->target), srv_conn);
3507 srv_conn = NULL;
3508
3509 }
Olivier Houchard985f1392018-11-30 17:31:52 +01003510 }
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003511 si_release_endpoint(&s->si[1]);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003512 if (srv_conn && srv_conn->owner == s->sess) {
3513 if (session_check_idle_conn(s->sess, srv_conn) != 0)
3514 srv_conn = NULL;
3515 }
3516
Willy Tarreau4213a112013-12-15 10:25:42 +01003517
Willy Tarreau350f4872014-11-28 14:42:25 +01003518 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
3519 s->si[1].err_type = SI_ET_NONE;
3520 s->si[1].conn_retries = 0; /* used for logging too */
3521 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02003522 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 +01003523 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 +02003524 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 +02003525 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
3526 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
3527 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01003528
Patrick Hemmere3faf022018-08-22 10:02:00 -04003529 hlua_ctx_destroy(s->hlua);
3530 s->hlua = NULL;
3531
Willy Tarreaueee5b512015-04-03 23:46:31 +02003532 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003533 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02003534 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01003535
3536 if (prev_status == 401 || prev_status == 407) {
3537 /* In HTTP keep-alive mode, if we receive a 401, we still have
3538 * a chance of being able to send the visitor again to the same
3539 * server over the same connection. This is required by some
3540 * broken protocols such as NTLM, and anyway whenever there is
3541 * an opportunity for sending the challenge to the proper place,
Lukas Tribus80512b12018-10-27 20:07:40 +02003542 * it's better to do it (at least it helps with debugging), at
3543 * least for non-deterministic load balancing algorithms.
Willy Tarreau068621e2013-12-23 15:11:25 +01003544 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003545 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreau068621e2013-12-23 15:11:25 +01003546 }
3547
Willy Tarreau53f96852016-02-02 18:50:47 +01003548 /* Never ever allow to reuse a connection from a non-reuse backend */
3549 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3550 srv_conn->flags |= CO_FL_PRIVATE;
3551
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003552 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01003553 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003554
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003555 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003556 s->req.flags |= CF_NEVER_WAIT;
3557 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02003558 }
3559
Willy Tarreau714ea782015-11-25 20:11:11 +01003560 /* we're removing the analysers, we MUST re-enable events detection.
3561 * We don't enable close on the response channel since it's either
3562 * already closed, or in keep-alive with an idle connection handler.
3563 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003564 channel_auto_read(&s->req);
3565 channel_auto_close(&s->req);
3566 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003567
Willy Tarreau1c59bd52015-11-02 20:20:11 +01003568 /* we're in keep-alive with an idle connection, monitor it if not already done */
3569 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02003570 srv = objt_server(srv_conn->target);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01003571 if (srv) {
3572 if (srv_conn->flags & CO_FL_PRIVATE)
3573 LIST_ADD(&srv->priv_conns[tid], &srv_conn->list);
3574 else if (prev_flags & TX_NOT_FIRST)
3575 /* note: we check the request, not the connection, but
3576 * this is valid for strategies SAFE and AGGR, and in
3577 * case of ALWS, we don't care anyway.
3578 */
3579 LIST_ADD(&srv->safe_conns[tid], &srv_conn->list);
3580 else
3581 LIST_ADD(&srv->idle_conns[tid], &srv_conn->list);
3582 }
Willy Tarreau4320eaa2015-08-05 11:08:30 +02003583 }
Christopher Faulete6006242017-03-10 11:52:44 +01003584 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
3585 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003586}
3587
3588
3589/* This function updates the request state machine according to the response
3590 * state machine and buffer flags. It returns 1 if it changes anything (flag
3591 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3592 * it is only used to find when a request/response couple is complete. Both
3593 * this function and its equivalent should loop until both return zero. It
3594 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3595 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003596int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003597{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003598 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003599 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003600 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003601 unsigned int old_state = txn->req.msg_state;
3602
Christopher Faulet4be98032017-07-18 10:48:24 +02003603 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003604 return 0;
3605
3606 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003607 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003608 * We can shut the read side unless we want to abort_on_close,
3609 * or we have a POST request. The issue with POST requests is
3610 * that some browsers still send a CRLF after the request, and
3611 * this CRLF must be read so that it does not remain in the kernel
3612 * buffers, otherwise a close could cause an RST on some systems
3613 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01003614 * Note that if we're using keep-alive on the client side, we'd
3615 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02003616 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01003617 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01003618 */
Willy Tarreau3988d932013-12-27 23:03:08 +01003619 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3620 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003621 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3622 (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
Willy Tarreau3988d932013-12-27 23:03:08 +01003623 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003624 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003625
Willy Tarreau40f151a2012-12-20 12:10:09 +01003626 /* if the server closes the connection, we want to immediately react
3627 * and close the socket to save packets and syscalls.
3628 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003629 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01003630
Willy Tarreau7f876a12015-11-18 11:59:55 +01003631 /* In any case we've finished parsing the request so we must
3632 * disable Nagle when sending data because 1) we're not going
3633 * to shut this side, and 2) the server is waiting for us to
3634 * send pending data.
3635 */
3636 chn->flags |= CF_NEVER_WAIT;
3637
Willy Tarreau610ecce2010-01-04 21:15:02 +01003638 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3639 goto wait_other_side;
3640
3641 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3642 /* The server has not finished to respond, so we
3643 * don't want to move in order not to upset it.
3644 */
3645 goto wait_other_side;
3646 }
3647
Willy Tarreau610ecce2010-01-04 21:15:02 +01003648 /* When we get here, it means that both the request and the
3649 * response have finished receiving. Depending on the connection
3650 * mode, we'll have to wait for the last bytes to leave in either
3651 * direction, and sometimes for a close to be effective.
3652 */
3653
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003654 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3655 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003656 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
3657 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003658 }
3659 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3660 /* Option forceclose is set, or either side wants to close,
3661 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003662 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003663 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003664 *
3665 * However, there is an exception if the response
3666 * length is undefined. In this case, we need to wait
3667 * the close from the server. The response will be
3668 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003669 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02003670 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
3671 txn->rsp.msg_state != HTTP_MSG_CLOSED)
3672 goto check_channel_flags;
3673
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003674 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
3675 channel_shutr_now(chn);
3676 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003677 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003678 }
3679 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003680 /* The last possible modes are keep-alive and tunnel. Tunnel mode
3681 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003682 */
Willy Tarreau4213a112013-12-15 10:25:42 +01003683 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
3684 channel_auto_read(chn);
3685 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01003686 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003687 }
3688
Christopher Faulet4be98032017-07-18 10:48:24 +02003689 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003690 }
3691
3692 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3693 http_msg_closing:
3694 /* nothing else to forward, just waiting for the output buffer
3695 * to be empty and for the shutw_now to take effect.
3696 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003697 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003698 txn->req.msg_state = HTTP_MSG_CLOSED;
3699 goto http_msg_closed;
3700 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003701 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003702 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003703 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003704 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003705 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003706 }
3707
3708 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3709 http_msg_closed:
Willy Tarreau80593512017-12-14 10:43:31 +01003710 /* if we don't know whether the server will close, we need to hard close */
3711 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
3712 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3713
Willy Tarreau3988d932013-12-27 23:03:08 +01003714 /* see above in MSG_DONE why we only do this in these states */
3715 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
3716 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
Willy Tarreau7aa15b02017-12-20 16:56:50 +01003717 (!(s->be->options & PR_O_ABRT_CLOSE) ||
3718 (s->si[0].flags & SI_FL_CLEAN_ABRT)))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01003719 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003720 goto wait_other_side;
3721 }
3722
Christopher Faulet4be98032017-07-18 10:48:24 +02003723 check_channel_flags:
3724 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
3725 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
3726 /* if we've just closed an output, let's switch */
Christopher Faulet4be98032017-07-18 10:48:24 +02003727 txn->req.msg_state = HTTP_MSG_CLOSING;
3728 goto http_msg_closing;
3729 }
3730
3731
Willy Tarreau610ecce2010-01-04 21:15:02 +01003732 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003733 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003734}
3735
3736
3737/* This function updates the response state machine according to the request
3738 * state machine and buffer flags. It returns 1 if it changes anything (flag
3739 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3740 * it is only used to find when a request/response couple is complete. Both
3741 * this function and its equivalent should loop until both return zero. It
3742 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3743 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003744int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003745{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003746 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003747 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003748 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003749 unsigned int old_state = txn->rsp.msg_state;
3750
Christopher Faulet4be98032017-07-18 10:48:24 +02003751 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003752 return 0;
3753
3754 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3755 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003756 * still monitor the server connection for a possible close
3757 * while the request is being uploaded, so we don't disable
3758 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003759 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003760 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003761
3762 if (txn->req.msg_state == HTTP_MSG_ERROR)
3763 goto wait_other_side;
3764
3765 if (txn->req.msg_state < HTTP_MSG_DONE) {
3766 /* The client seems to still be sending data, probably
3767 * because we got an error response during an upload.
3768 * We have the choice of either breaking the connection
3769 * or letting it pass through. Let's do the later.
3770 */
3771 goto wait_other_side;
3772 }
3773
Willy Tarreau610ecce2010-01-04 21:15:02 +01003774 /* When we get here, it means that both the request and the
3775 * response have finished receiving. Depending on the connection
3776 * mode, we'll have to wait for the last bytes to leave in either
3777 * direction, and sometimes for a close to be effective.
3778 */
3779
3780 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3781 /* Server-close mode : shut read and wait for the request
3782 * side to close its output buffer. The caller will detect
3783 * when we're in DONE and the other is in CLOSED and will
3784 * catch that for the final cleanup.
3785 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003786 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
3787 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003788 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003789 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3790 /* Option forceclose is set, or either side wants to close,
3791 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02003792 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003793 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003794 */
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01003795 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003796 channel_shutr_now(chn);
3797 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003798 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003799 }
3800 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01003801 /* The last possible modes are keep-alive and tunnel. Tunnel will
3802 * need to forward remaining data. Keep-alive will need to monitor
3803 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003804 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003805 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02003806 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01003807 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
3808 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003809 }
3810
Christopher Faulet4be98032017-07-18 10:48:24 +02003811 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003812 }
3813
3814 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
3815 http_msg_closing:
3816 /* nothing else to forward, just waiting for the output buffer
3817 * to be empty and for the shutw_now to take effect.
3818 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003819 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003820 txn->rsp.msg_state = HTTP_MSG_CLOSED;
3821 goto http_msg_closed;
3822 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003823 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02003824 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003825 txn->rsp.msg_state = HTTP_MSG_ERROR;
Olivier Houcharda798bf52019-03-08 18:52:00 +01003826 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003827 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01003828 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003829 }
Christopher Faulet56d26092017-07-20 11:05:10 +02003830 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003831 }
3832
3833 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
3834 http_msg_closed:
3835 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01003836 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003837 channel_auto_close(chn);
3838 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003839 goto wait_other_side;
3840 }
3841
Christopher Faulet4be98032017-07-18 10:48:24 +02003842 check_channel_flags:
3843 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
3844 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
3845 /* if we've just closed an output, let's switch */
3846 txn->rsp.msg_state = HTTP_MSG_CLOSING;
3847 goto http_msg_closing;
3848 }
3849
Willy Tarreau610ecce2010-01-04 21:15:02 +01003850 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02003851 /* We force the response to leave immediately if we're waiting for the
3852 * other side, since there is no pending shutdown to push it out.
3853 */
3854 if (!channel_is_empty(chn))
3855 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02003856 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003857}
3858
3859
Christopher Faulet894da4c2017-07-18 11:29:07 +02003860/* Resync the request and response state machines. */
3861void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003862{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003863 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02003864#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01003865 int old_req_state = txn->req.msg_state;
3866 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02003867#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01003868
Willy Tarreau610ecce2010-01-04 21:15:02 +01003869 http_sync_req_state(s);
3870 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003871 if (!http_sync_res_state(s))
3872 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003873 if (!http_sync_req_state(s))
3874 break;
3875 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02003876
Christopher Faulet894da4c2017-07-18 11:29:07 +02003877 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
3878 "req->analysers=0x%08x res->analysers=0x%08x\n",
3879 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02003880 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
3881 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02003882 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02003883
3884
Willy Tarreau610ecce2010-01-04 21:15:02 +01003885 /* OK, both state machines agree on a compatible state.
3886 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01003887 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
3888 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02003889 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
3890 * means we must abort the request.
3891 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
3892 * corresponding channel.
3893 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
3894 * on the response with server-close mode means we've completed one
3895 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003896 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02003897 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
3898 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01003899 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003900 channel_auto_close(&s->req);
3901 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003902 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003903 channel_auto_close(&s->res);
3904 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003905 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02003906 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
3907 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01003908 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01003909 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003910 channel_auto_close(&s->res);
3911 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003912 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003913 channel_abort(&s->req);
3914 channel_auto_close(&s->req);
3915 channel_auto_read(&s->req);
3916 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003917 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02003918 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
3919 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3920 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3921 s->req.analysers &= AN_REQ_FLT_END;
3922 if (HAS_REQ_DATA_FILTERS(s))
3923 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
3924 }
3925 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3926 s->res.analysers &= AN_RES_FLT_END;
3927 if (HAS_RSP_DATA_FILTERS(s))
3928 s->res.analysers |= AN_RES_FLT_XFER_DATA;
3929 }
3930 channel_auto_close(&s->req);
3931 channel_auto_read(&s->req);
3932 channel_auto_close(&s->res);
3933 channel_auto_read(&s->res);
3934 }
Willy Tarreau4213a112013-12-15 10:25:42 +01003935 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
3936 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003937 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01003938 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3939 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3940 /* server-close/keep-alive: terminate this transaction,
3941 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02003942 * a fresh-new transaction, but only once we're sure there's
3943 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02003944 * another request. They must not hold any pending output data
3945 * and the response buffer must realigned
3946 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01003947 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02003948 if (co_data(&s->req))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02003949 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreauf37954d2018-06-15 18:31:02 +02003950 else if (co_data(&s->res))
Willy Tarreau3de5bd62016-05-02 16:07:07 +02003951 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02003952 else {
3953 s->req.analysers = AN_REQ_FLT_END;
3954 s->res.analysers = AN_RES_FLT_END;
3955 txn->flags |= TX_WAIT_CLEANUP;
3956 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003957 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003958}
3959
Willy Tarreaud98cf932009-12-27 22:54:55 +01003960/* This function is an analyser which forwards request body (including chunk
3961 * sizes if any). It is called as soon as we must forward, even if we forward
3962 * zero byte. The only situation where it must not be called is when we're in
3963 * tunnel mode and we want to forward till the close. It's used both to forward
3964 * remaining data and to resync after end of body. It expects the msg_state to
3965 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02003966 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01003967 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02003968 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003969 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003970int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01003971{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003972 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003973 struct http_txn *txn = s->txn;
3974 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01003975 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01003976
Christopher Faulete0768eb2018-10-03 16:38:02 +02003977 if (IS_HTX_STRM(s))
3978 return htx_request_forward_body(s, req, an_bit);
3979
Christopher Faulet45073512018-07-20 10:16:29 +02003980 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 +02003981 now_ms, __FUNCTION__,
3982 s,
3983 req,
3984 req->rex, req->wex,
3985 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02003986 ci_data(req),
Christopher Faulet814d2702017-03-30 11:33:44 +02003987 req->analysers);
3988
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01003989 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3990 return 0;
3991
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003992 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02003993 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02003994 /* Output closed while we were sending data. We must abort and
3995 * wake the other side up.
3996 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003997 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02003998 msg->msg_state = HTTP_MSG_ERROR;
3999 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004000 return 1;
4001 }
4002
Willy Tarreaud98cf932009-12-27 22:54:55 +01004003 /* Note that we don't have to send 100-continue back because we don't
4004 * need the data to complete our job, and it's up to the server to
4005 * decide whether to return 100, 417 or anything else in return of
4006 * an "Expect: 100-continue" header.
4007 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004008 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004009 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4010 ? HTTP_MSG_CHUNK_SIZE
4011 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004012
4013 /* TODO/filters: when http-buffer-request option is set or if a
4014 * rule on url_param exists, the first chunk size could be
4015 * already parsed. In that case, msg->next is after the chunk
4016 * size (including the CRLF after the size). So this case should
4017 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004018 }
4019
Willy Tarreau7ba23542014-04-17 21:50:00 +02004020 /* Some post-connect processing might want us to refrain from starting to
4021 * forward data. Currently, the only reason for this is "balance url_param"
4022 * whichs need to parse/process the request after we've enabled forwarding.
4023 */
4024 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004025 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004026 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004027 req->flags |= CF_WAKE_CONNECT;
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004028 channel_dont_close(req); /* don't fail on early shutr */
4029 goto waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004030 }
4031 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4032 }
4033
Willy Tarreau80a92c02014-03-12 10:41:13 +01004034 /* in most states, we should abort in case of early close */
4035 channel_auto_close(req);
4036
Willy Tarreauefdf0942014-04-24 20:08:57 +02004037 if (req->to_forward) {
4038 /* We can't process the buffer's contents yet */
4039 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004040 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004041 }
4042
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004043 if (msg->msg_state < HTTP_MSG_DONE) {
4044 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4045 ? http_msg_forward_chunked_body(s, msg)
4046 : http_msg_forward_body(s, msg));
4047 if (!ret)
4048 goto missing_data_or_waiting;
4049 if (ret < 0)
4050 goto return_bad_req;
4051 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004052
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004053 /* other states, DONE...TUNNEL */
4054 /* we don't want to forward closes on DONE except in tunnel mode. */
4055 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4056 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004057
Christopher Faulet894da4c2017-07-18 11:29:07 +02004058 http_resync_states(s);
4059 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004060 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4061 if (req->flags & CF_SHUTW) {
4062 /* request errors are most likely due to the
4063 * server aborting the transfer. */
4064 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004065 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004066 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004067 http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004068 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004069 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004070 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004071 }
4072
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004073 /* If "option abortonclose" is set on the backend, we want to monitor
4074 * the client's connection and forward any shutdown notification to the
4075 * server, which will decide whether to close or to go on processing the
4076 * request. We only do that in tunnel mode, and not in other modes since
4077 * it can be abused to exhaust source ports. */
Willy Tarreau7aa15b02017-12-20 16:56:50 +01004078 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004079 channel_auto_read(req);
4080 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4081 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4082 s->si[1].flags |= SI_FL_NOLINGER;
4083 channel_auto_close(req);
4084 }
4085 else if (s->txn->meth == HTTP_METH_POST) {
4086 /* POST requests may require to read extra CRLF sent by broken
4087 * browsers and which could cause an RST to be sent upon close
4088 * on some systems (eg: Linux). */
4089 channel_auto_read(req);
4090 }
4091 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004092
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004093 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004094 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004095 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004096 if (!(s->flags & SF_ERR_MASK))
4097 s->flags |= SF_ERR_CLICL;
4098 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004099 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004100 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004101 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004102 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004103 }
4104
Olivier Houcharda798bf52019-03-08 18:52:00 +01004105 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4106 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004107 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004108 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004109
4110 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004111 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004112
Willy Tarreauba20dfc2018-05-16 11:35:05 +02004113 waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004114 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004115 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004116 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004117
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004118 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004119 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004120 * And when content-length is used, we never want to let the possible
4121 * shutdown be forwarded to the other side, as the state machine will
4122 * take care of it once the client responds. It's also important to
4123 * prevent TIME_WAITs from accumulating on the backend side, and for
4124 * HTTP/2 where the last frame comes with a shutdown.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004125 */
Willy Tarreauff47b3f2017-12-29 16:30:31 +01004126 if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN))
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004127 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004128
Willy Tarreau5c620922011-05-11 19:56:11 +02004129 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004130 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004131 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004132 * modes are already handled by the stream sock layer. We must not do
4133 * this in content-length mode because it could present the MSG_MORE
4134 * flag with the last block of forwarded data, which would cause an
4135 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004136 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004137 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004138 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004139
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004140 return 0;
4141
Willy Tarreaud98cf932009-12-27 22:54:55 +01004142 return_bad_req: /* let's centralize all bad requests */
Olivier Houcharda798bf52019-03-08 18:52:00 +01004143 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004144 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01004145 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004146
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004147 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004148 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004149 txn->req.msg_state = HTTP_MSG_ERROR;
4150 if (txn->status) {
4151 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004152 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004153 } else {
4154 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004155 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004156 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004157 req->analysers &= AN_REQ_FLT_END;
4158 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004159
Willy Tarreaue7dff022015-04-03 01:14:29 +02004160 if (!(s->flags & SF_ERR_MASK))
4161 s->flags |= SF_ERR_PRXCOND;
4162 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004163 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004164 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004165 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004166 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004167 }
4168 return 0;
4169
4170 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004171 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004172 txn->req.msg_state = HTTP_MSG_ERROR;
4173 if (txn->status) {
4174 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004175 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004176 } else {
4177 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004178 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004179 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004180 req->analysers &= AN_REQ_FLT_END;
4181 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004182
Olivier Houcharda798bf52019-03-08 18:52:00 +01004183 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4184 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004185 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004186 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004187
Willy Tarreaue7dff022015-04-03 01:14:29 +02004188 if (!(s->flags & SF_ERR_MASK))
4189 s->flags |= SF_ERR_SRVCL;
4190 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004191 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004192 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004193 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004194 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004195 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004196 return 0;
4197}
4198
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004199/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4200 * processing can continue on next analysers, or zero if it either needs more
4201 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004202 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004203 * when it has nothing left to do, and may remove any analyser when it wants to
4204 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004205 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004206int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004207{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004208 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004209 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004210 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004211 struct hdr_ctx ctx;
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004212 struct connection *srv_conn;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004213 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004214 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004215 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004216
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004217 srv_conn = cs_conn(objt_cs(s->si[1].end));
4218
Christopher Faulete0768eb2018-10-03 16:38:02 +02004219 if (IS_HTX_STRM(s))
4220 return htx_wait_for_response(s, rep, an_bit);
4221
Christopher Faulet45073512018-07-20 10:16:29 +02004222 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 +02004223 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004224 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004225 rep,
4226 rep->rex, rep->wex,
4227 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004228 ci_data(rep),
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004229 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004230
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004231 /*
4232 * Now parse the partial (or complete) lines.
4233 * We will check the response syntax, and also join multi-line
4234 * headers. An index of all the lines will be elaborated while
4235 * parsing.
4236 *
4237 * For the parsing, we use a 28 states FSM.
4238 *
4239 * Here is the information we currently have :
Willy Tarreauf37954d2018-06-15 18:31:02 +02004240 * ci_head(rep) = beginning of response
4241 * ci_head(rep) + msg->eoh = end of processed headers / start of current one
4242 * ci_tail(rep) = end of input data
4243 * msg->eol = end of current header or line (LF or CRLF)
4244 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004245 */
4246
Willy Tarreau628c40c2014-04-24 19:11:26 +02004247 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004248 /* There's a protected area at the end of the buffer for rewriting
4249 * purposes. We don't want to start to parse the request if the
4250 * protected area is affected, because we may have to move processed
4251 * data later, which is much more complicated.
4252 */
Willy Tarreaud760eec2018-07-10 09:50:25 +02004253 if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004254 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004255 /* some data has still not left the buffer, wake us once that's done */
4256 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4257 goto abort_response;
4258 channel_dont_close(rep);
4259 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004260 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004261 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004262 }
4263
Willy Tarreau188e2302018-06-15 11:11:53 +02004264 if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004265 ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite))
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004266 channel_slow_realign(rep, trash.area);
Willy Tarreau379357a2013-06-08 12:55:46 +02004267
Willy Tarreauf37954d2018-06-15 18:31:02 +02004268 if (likely(msg->next < ci_data(rep)))
Willy Tarreaua560c212012-03-09 13:50:57 +01004269 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004270 }
4271
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004272 /* 1: we might have to print this header in debug mode */
4273 if (unlikely((global.mode & MODE_DEBUG) &&
4274 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004275 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004276 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004277
Willy Tarreauf37954d2018-06-15 18:31:02 +02004278 sol = ci_head(rep);
4279 eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004280 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004281
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004282 sol += hdr_idx_first_pos(&txn->hdr_idx);
4283 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004284
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004285 while (cur_idx) {
4286 eol = sol + txn->hdr_idx.v[cur_idx].len;
4287 debug_hdr("srvhdr", s, sol, eol);
4288 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4289 cur_idx = txn->hdr_idx.v[cur_idx].next;
4290 }
4291 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004292
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004293 /*
4294 * Now we quickly check if we have found a full valid response.
4295 * If not so, we check the FD and buffer states before leaving.
4296 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004297 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004298 * responses are checked first.
4299 *
4300 * Depending on whether the client is still there or not, we
4301 * may send an error response back or not. Note that normally
4302 * we should only check for HTTP status there, and check I/O
4303 * errors somewhere else.
4304 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004305
Willy Tarreau655dce92009-11-08 13:10:58 +01004306 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004307 /* Invalid response */
4308 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4309 /* we detected a parsing error. We want to archive this response
4310 * in the dedicated proxy area for later troubleshooting.
4311 */
4312 hdr_response_bad:
4313 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004314 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004315
Olivier Houcharda798bf52019-03-08 18:52:00 +01004316 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004317 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004318 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004319 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004320 }
Willy Tarreau64648412010-03-05 10:41:54 +01004321 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004322 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004323 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004324 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004325 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004326 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004327 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004328
Willy Tarreaue7dff022015-04-03 01:14:29 +02004329 if (!(s->flags & SF_ERR_MASK))
4330 s->flags |= SF_ERR_PRXCOND;
4331 if (!(s->flags & SF_FINST_MASK))
4332 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004333
4334 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004335 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004336
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004337 /* too large response does not fit in buffer. */
Willy Tarreau23752332018-06-15 14:54:53 +02004338 else if (channel_full(rep, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004339 if (msg->err_pos < 0)
Willy Tarreauf37954d2018-06-15 18:31:02 +02004340 msg->err_pos = ci_data(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004341 goto hdr_response_bad;
4342 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004343
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004344 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004345 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004346 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004347 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004348 else if (txn->flags & TX_NOT_FIRST)
4349 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02004350
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_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004355 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004356
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 = 502;
Olivier Houchard522eea72017-11-03 16:27:47 +01004360
4361 /* Check to see if the server refused the early data.
4362 * If so, just send a 425
4363 */
4364 if (objt_cs(s->si[1].end)) {
4365 struct connection *conn = objt_cs(s->si[1].end)->conn;
4366
4367 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
4368 txn->status = 425;
4369 }
4370
Willy Tarreau350f4872014-11-28 14:42:25 +01004371 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004372 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004373 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02004374
Willy Tarreaue7dff022015-04-03 01:14:29 +02004375 if (!(s->flags & SF_ERR_MASK))
4376 s->flags |= SF_ERR_SRVCL;
4377 if (!(s->flags & SF_FINST_MASK))
4378 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004379 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004380 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004381
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004382 /* read timeout : return a 504 to the client. */
4383 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004384 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004385 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004386
Olivier Houcharda798bf52019-03-08 18:52:00 +01004387 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004388 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004389 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004390 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004391 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004392
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004393 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004394 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004395 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01004396 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004397 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004398 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02004399
Willy Tarreaue7dff022015-04-03 01:14:29 +02004400 if (!(s->flags & SF_ERR_MASK))
4401 s->flags |= SF_ERR_SRVTO;
4402 if (!(s->flags & SF_FINST_MASK))
4403 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004404 return 0;
4405 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004406
Willy Tarreauf003d372012-11-26 13:35:37 +01004407 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004408 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004409 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4410 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004411 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004412 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01004413
Christopher Faulet0184ea72017-01-05 14:06:34 +01004414 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01004415 channel_auto_close(rep);
4416
4417 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01004418 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004419 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01004420
Willy Tarreaue7dff022015-04-03 01:14:29 +02004421 if (!(s->flags & SF_ERR_MASK))
4422 s->flags |= SF_ERR_CLICL;
4423 if (!(s->flags & SF_FINST_MASK))
4424 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01004425
Willy Tarreau87b09662015-04-03 00:22:06 +02004426 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01004427 return 0;
4428 }
4429
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004430 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004431 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004432 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004433 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004434 else if (txn->flags & TX_NOT_FIRST)
4435 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004436
Olivier Houcharda798bf52019-03-08 18:52:00 +01004437 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004438 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004439 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004440 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004441 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004442
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004443 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004444 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004445 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01004446 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004447 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004448 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004449
Willy Tarreaue7dff022015-04-03 01:14:29 +02004450 if (!(s->flags & SF_ERR_MASK))
4451 s->flags |= SF_ERR_SRVCL;
4452 if (!(s->flags & SF_FINST_MASK))
4453 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004454 return 0;
4455 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004456
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004457 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004458 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004459 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004460 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01004461 else if (txn->flags & TX_NOT_FIRST)
4462 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004463
Olivier Houcharda798bf52019-03-08 18:52:00 +01004464 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004465 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004466 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004467
Willy Tarreaue7dff022015-04-03 01:14:29 +02004468 if (!(s->flags & SF_ERR_MASK))
4469 s->flags |= SF_ERR_CLICL;
4470 if (!(s->flags & SF_FINST_MASK))
4471 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004472
Willy Tarreau87b09662015-04-03 00:22:06 +02004473 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004474 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004475 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004476
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004477 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01004478 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004479 return 0;
4480 }
4481
4482 /* More interesting part now : we know that we have a complete
4483 * response which at least looks like HTTP. We have an indicator
4484 * of each header's length, so we can parse them quickly.
4485 */
4486
4487 if (unlikely(msg->err_pos >= 0))
Willy Tarreaufd9419d2018-09-07 18:01:03 +02004488 http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004489
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004490 /*
4491 * 1: get the status code
4492 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02004493 n = ci_head(rep)[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004494 if (n < 1 || n > 5)
4495 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004496 /* when the client triggers a 4xx from the server, it's most often due
4497 * to a missing object or permission. These events should be tracked
4498 * because if they happen often, it may indicate a brute force or a
4499 * vulnerability scan.
4500 */
4501 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02004502 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02004503
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004504 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004505 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004506
Willy Tarreau91852eb2015-05-01 13:26:00 +02004507 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
4508 * exactly one digit "." one digit. This check may be disabled using
4509 * option accept-invalid-http-response.
4510 */
4511 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
4512 if (msg->sl.st.v_l != 8) {
4513 msg->err_pos = 0;
4514 goto hdr_response_bad;
4515 }
4516
Willy Tarreauf37954d2018-06-15 18:31:02 +02004517 if (ci_head(rep)[4] != '/' ||
4518 !isdigit((unsigned char)ci_head(rep)[5]) ||
4519 ci_head(rep)[6] != '.' ||
4520 !isdigit((unsigned char)ci_head(rep)[7])) {
Willy Tarreau91852eb2015-05-01 13:26:00 +02004521 msg->err_pos = 4;
4522 goto hdr_response_bad;
4523 }
4524 }
4525
Willy Tarreau5b154472009-12-21 20:11:07 +01004526 /* check if the response is HTTP/1.1 or above */
4527 if ((msg->sl.st.v_l == 8) &&
Willy Tarreauf37954d2018-06-15 18:31:02 +02004528 ((ci_head(rep)[5] > '1') ||
4529 ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004530 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004531
4532 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004533 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 +01004534
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004535 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004536 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004537
Willy Tarreauf37954d2018-06-15 18:31:02 +02004538 txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004539
Willy Tarreau39650402010-03-15 19:44:39 +01004540 /* Adjust server's health based on status code. Note: status codes 501
4541 * and 505 are triggered on demand by client request, so we must not
4542 * count them as server failures.
4543 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004544 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004545 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004546 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004547 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004548 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004549 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004550
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004551 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02004552 * We may be facing a 100-continue response, or any other informational
4553 * 1xx response which is non-final, in which case this is not the right
4554 * response, and we're waiting for the next one. Let's allow this response
4555 * to go to the client and wait for the next one. There's an exception for
4556 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004557 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02004558 if (txn->status < 200 &&
4559 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02004560 hdr_idx_init(&txn->hdr_idx);
4561 msg->next -= channel_forward(rep, msg->next);
4562 msg->msg_state = HTTP_MSG_RPBEFORE;
4563 txn->status = 0;
4564 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01004565 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02004566 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02004567 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02004568
Willy Tarreaua14ad722017-07-07 11:36:32 +02004569 /*
4570 * 2: check for cacheability.
4571 */
4572
4573 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004574 case 200:
4575 case 203:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004576 case 204:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004577 case 206:
4578 case 300:
4579 case 301:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004580 case 404:
4581 case 405:
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004582 case 410:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004583 case 414:
4584 case 501:
Willy Tarreau83ece462017-12-21 15:13:09 +01004585 break;
4586 default:
Willy Tarreauc55ddce2017-12-21 11:41:38 +01004587 /* RFC7231#6.1:
4588 * Responses with status codes that are defined as
4589 * cacheable by default (e.g., 200, 203, 204, 206,
4590 * 300, 301, 404, 405, 410, 414, and 501 in this
4591 * specification) can be reused by a cache with
4592 * heuristic expiration unless otherwise indicated
4593 * by the method definition or explicit cache
4594 * controls [RFC7234]; all other status codes are
4595 * not cacheable by default.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004596 */
Willy Tarreau83ece462017-12-21 15:13:09 +01004597 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004598 break;
4599 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004600
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004601 /*
4602 * 3: we may need to capture headers
4603 */
4604 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02004605 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet10079f52018-10-03 15:17:28 +02004606 http_capture_headers(ci_head(rep), &txn->hdr_idx,
4607 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004608
Willy Tarreau557f1992015-05-01 10:05:17 +02004609 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
4610 * by RFC7230#3.3.3 :
4611 *
4612 * The length of a message body is determined by one of the following
4613 * (in order of precedence):
4614 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004615 * 1. Any 2xx (Successful) response to a CONNECT request implies that
4616 * the connection will become a tunnel immediately after the empty
4617 * line that concludes the header fields. A client MUST ignore
4618 * any Content-Length or Transfer-Encoding header fields received
4619 * in such a message. Any 101 response (Switching Protocols) is
4620 * managed in the same manner.
4621 *
4622 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02004623 * (Informational), 204 (No Content), or 304 (Not Modified) status
4624 * code is always terminated by the first empty line after the
4625 * header fields, regardless of the header fields present in the
4626 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004627 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004628 * 3. If a Transfer-Encoding header field is present and the chunked
4629 * transfer coding (Section 4.1) is the final encoding, the message
4630 * body length is determined by reading and decoding the chunked
4631 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004632 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004633 * If a Transfer-Encoding header field is present in a response and
4634 * the chunked transfer coding is not the final encoding, the
4635 * message body length is determined by reading the connection until
4636 * it is closed by the server. If a Transfer-Encoding header field
4637 * is present in a request and the chunked transfer coding is not
4638 * the final encoding, the message body length cannot be determined
4639 * reliably; the server MUST respond with the 400 (Bad Request)
4640 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004641 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004642 * If a message is received with both a Transfer-Encoding and a
4643 * Content-Length header field, the Transfer-Encoding overrides the
4644 * Content-Length. Such a message might indicate an attempt to
4645 * perform request smuggling (Section 9.5) or response splitting
4646 * (Section 9.4) and ought to be handled as an error. A sender MUST
4647 * remove the received Content-Length field prior to forwarding such
4648 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004649 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004650 * 4. If a message is received without Transfer-Encoding and with
4651 * either multiple Content-Length header fields having differing
4652 * field-values or a single Content-Length header field having an
4653 * invalid value, then the message framing is invalid and the
4654 * recipient MUST treat it as an unrecoverable error. If this is a
4655 * request message, the server MUST respond with a 400 (Bad Request)
4656 * status code and then close the connection. If this is a response
4657 * message received by a proxy, the proxy MUST close the connection
4658 * to the server, discard the received response, and send a 502 (Bad
4659 * Gateway) response to the client. If this is a response message
4660 * received by a user agent, the user agent MUST close the
4661 * connection to the server and discard the received response.
4662 *
4663 * 5. If a valid Content-Length header field is present without
4664 * Transfer-Encoding, its decimal value defines the expected message
4665 * body length in octets. If the sender closes the connection or
4666 * the recipient times out before the indicated number of octets are
4667 * received, the recipient MUST consider the message to be
4668 * incomplete and close the connection.
4669 *
4670 * 6. If this is a request message and none of the above are true, then
4671 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004672 *
Willy Tarreau557f1992015-05-01 10:05:17 +02004673 * 7. Otherwise, this is a response message without a declared message
4674 * body length, so the message body length is determined by the
4675 * number of octets received prior to the server closing the
4676 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004677 */
4678
4679 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004680 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004681 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004682 * FIXME: should we parse anyway and return an error on chunked encoding ?
4683 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004684 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4685 txn->status == 101)) {
4686 /* Either we've established an explicit tunnel, or we're
4687 * switching the protocol. In both cases, we're very unlikely
4688 * to understand the next protocols. We have to switch to tunnel
4689 * mode, so that we transfer the request and responses then let
4690 * this protocol pass unmodified. When we later implement specific
4691 * parsers for such protocols, we'll want to check the Upgrade
4692 * header which contains information about that protocol for
4693 * responses with status 101 (eg: see RFC2817 about TLS).
4694 */
4695 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4696 msg->flags |= HTTP_MSGF_XFER_LEN;
4697 goto end;
4698 }
4699
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004700 if (txn->meth == HTTP_METH_HEAD ||
4701 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004702 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004703 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004704 goto skip_content_length;
4705 }
4706
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004707 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004708 ctx.idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02004709 while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004710 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004711 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4712 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004713 /* bad transfer-encoding (chunked followed by something else) */
4714 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004715 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004716 break;
4717 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004718 }
4719
Willy Tarreau1c913912015-04-30 10:57:51 +02004720 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004721 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02004722 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004723 while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx))
Willy Tarreau1c913912015-04-30 10:57:51 +02004724 http_remove_header2(msg, &txn->hdr_idx, &ctx);
4725 }
Willy Tarreauf37954d2018-06-15 18:31:02 +02004726 else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004727 signed long long cl;
4728
Willy Tarreauad14f752011-09-02 20:33:27 +02004729 if (!ctx.vlen) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004730 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004731 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004732 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004733
Willy Tarreauad14f752011-09-02 20:33:27 +02004734 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004735 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004736 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004737 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004738
Willy Tarreauad14f752011-09-02 20:33:27 +02004739 if (cl < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004740 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004741 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004742 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004743
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004744 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02004745 msg->err_pos = ctx.line + ctx.val - ci_head(rep);
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004746 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004747 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004748
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004749 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004750 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004751 }
4752
Lukas Tribusfd9b68c2018-10-27 20:06:59 +02004753 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
4754 * 407 (Proxy-Authenticate) responses and set the connection to private
4755 */
4756 if (srv_conn && txn->status == 401) {
4757 /* check for Negotiate/NTLM WWW-Authenticate headers */
4758 ctx.idx = 0;
4759 while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) {
4760 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
4761 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
4762 srv_conn->flags |= CO_FL_PRIVATE;
4763 }
4764 } else if (srv_conn && txn->status == 407) {
4765 /* check for Negotiate/NTLM Proxy-Authenticate headers */
4766 ctx.idx = 0;
4767 while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) {
4768 if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) ||
4769 (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4)))
4770 srv_conn->flags |= CO_FL_PRIVATE;
4771 }
4772 }
4773
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004774 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01004775 /* Now we have to check if we need to modify the Connection header.
4776 * This is more difficult on the response than it is on the request,
4777 * because we can have two different HTTP versions and we don't know
4778 * how the client will interprete a response. For instance, let's say
4779 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4780 * HTTP/1.1 response without any header. Maybe it will bound itself to
4781 * HTTP/1.0 because it only knows about it, and will consider the lack
4782 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4783 * the lack of header as a keep-alive. Thus we will use two flags
4784 * indicating how a request MAY be understood by the client. In case
4785 * of multiple possibilities, we'll fix the header to be explicit. If
4786 * ambiguous cases such as both close and keepalive are seen, then we
4787 * will fall back to explicit close. Note that we won't take risks with
4788 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004789 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004790 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004791 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02004792 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau60466522010-01-18 19:08:45 +01004793 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004794
Willy Tarreau60466522010-01-18 19:08:45 +01004795 /* on unknown transfer length, we must close */
Christopher Faulet315b39c2018-09-21 16:26:19 +02004796 if (!(msg->flags & HTTP_MSGF_XFER_LEN))
Willy Tarreau60466522010-01-18 19:08:45 +01004797 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004798
Willy Tarreau60466522010-01-18 19:08:45 +01004799 /* now adjust header transformations depending on current state */
Christopher Faulet315b39c2018-09-21 16:26:19 +02004800 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
Willy Tarreau60466522010-01-18 19:08:45 +01004801 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004802 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004803 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004804 }
Willy Tarreau60466522010-01-18 19:08:45 +01004805 else { /* SCL / KAL */
4806 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004807 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004808 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004809 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004810
Willy Tarreau60466522010-01-18 19:08:45 +01004811 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004812 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004813
Willy Tarreau60466522010-01-18 19:08:45 +01004814 /* Some keep-alive responses are converted to Server-close if
4815 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004816 */
Willy Tarreau60466522010-01-18 19:08:45 +01004817 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4818 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004819 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004820 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004821 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004822 }
4823
Christopher Fauletd1cd2092016-11-28 10:14:03 +01004824 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02004825 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02004826 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02004827
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004828 /* end of job, return OK */
4829 rep->analysers &= ~an_bit;
4830 rep->analyse_exp = TICK_ETERNITY;
4831 channel_auto_close(rep);
4832 return 1;
4833
4834 abort_keep_alive:
4835 /* A keep-alive request to the server failed on a network error.
4836 * The client is required to retry. We need to close without returning
4837 * any other information so that the client retries.
4838 */
4839 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004840 rep->analysers &= AN_RES_FLT_END;
4841 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004842 channel_auto_close(rep);
4843 s->logs.logwait = 0;
4844 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004845 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01004846 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004847 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004848 return 0;
4849}
4850
4851/* This function performs all the processing enabled for the current response.
4852 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004853 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004854 * other functions. It works like process_request (see indications above).
4855 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004856int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004857{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004858 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004859 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004860 struct http_msg *msg = &txn->rsp;
4861 struct proxy *cur_proxy;
4862 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004863 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004864
Christopher Faulete0768eb2018-10-03 16:38:02 +02004865 if (IS_HTX_STRM(s))
4866 return htx_process_res_common(s, rep, an_bit, px);
4867
Christopher Faulet45073512018-07-20 10:16:29 +02004868 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 +02004869 now_ms, __FUNCTION__,
4870 s,
4871 rep,
4872 rep->rex, rep->wex,
4873 rep->flags,
Willy Tarreauf37954d2018-06-15 18:31:02 +02004874 ci_data(rep),
Willy Tarreauf118d9f2014-04-24 18:26:08 +02004875 rep->analysers);
4876
4877 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
4878 return 0;
4879
Willy Tarreau70730dd2014-04-24 18:06:27 +02004880 /* The stats applet needs to adjust the Connection header but we don't
4881 * apply any filter there.
4882 */
Willy Tarreau612adb82015-03-10 15:25:54 +01004883 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
4884 rep->analysers &= ~an_bit;
4885 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02004886 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01004887 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02004888
Willy Tarreau58975672014-04-24 21:13:57 +02004889 /*
4890 * We will have to evaluate the filters.
4891 * As opposed to version 1.2, now they will be evaluated in the
4892 * filters order and not in the header order. This means that
4893 * each filter has to be validated among all headers.
4894 *
4895 * Filters are tried with ->be first, then with ->fe if it is
4896 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004897 *
4898 * Maybe we are in resume condiion. In this case I choose the
4899 * "struct proxy" which contains the rule list matching the resume
4900 * pointer. If none of theses "struct proxy" match, I initialise
4901 * the process with the first one.
4902 *
4903 * In fact, I check only correspondance betwwen the current list
4904 * pointer and the ->fe rule list. If it doesn't match, I initialize
4905 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02004906 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004907 if (s->current_rule_list == &sess->fe->http_res_rules)
4908 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004909 else
4910 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02004911 while (1) {
4912 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004913
Willy Tarreau58975672014-04-24 21:13:57 +02004914 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02004915 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004916 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004917
Willy Tarreau51d861a2015-05-22 17:30:48 +02004918 if (ret == HTTP_RULE_RES_BADREQ)
4919 goto return_srv_prx_502;
4920
4921 if (ret == HTTP_RULE_RES_DONE) {
4922 rep->analysers &= ~an_bit;
4923 rep->analyse_exp = TICK_ETERNITY;
4924 return 1;
4925 }
4926 }
4927
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004928 /* we need to be called again. */
4929 if (ret == HTTP_RULE_RES_YIELD) {
4930 channel_dont_close(rep);
4931 return 0;
4932 }
4933
Willy Tarreau58975672014-04-24 21:13:57 +02004934 /* try headers filters */
4935 if (rule_set->rsp_exp != NULL) {
4936 if (apply_filters_to_response(s, rep, rule_set) < 0) {
4937 return_bad_resp:
4938 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01004939 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01004940 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004941 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01004942 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02004943 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004944 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02004945 txn->status = 502;
4946 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01004947 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01004948 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004949 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02004950 if (!(s->flags & SF_ERR_MASK))
4951 s->flags |= SF_ERR_PRXCOND;
4952 if (!(s->flags & SF_FINST_MASK))
4953 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02004954 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004955 }
Willy Tarreau58975672014-04-24 21:13:57 +02004956 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004957
Willy Tarreau58975672014-04-24 21:13:57 +02004958 /* has the response been denied ? */
4959 if (txn->flags & TX_SVDENY) {
4960 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004961 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004962
Olivier Houcharda798bf52019-03-08 18:52:00 +01004963 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
4964 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004965 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01004966 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02004967
Willy Tarreau58975672014-04-24 21:13:57 +02004968 goto return_srv_prx_502;
4969 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02004970
Willy Tarreau58975672014-04-24 21:13:57 +02004971 /* add response headers from the rule sets in the same order */
4972 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02004973 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004974 break;
Willy Tarreau58975672014-04-24 21:13:57 +02004975 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004976 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02004977 ret = acl_pass(ret);
4978 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4979 ret = !ret;
4980 if (!ret)
4981 continue;
4982 }
Christopher Faulet10079f52018-10-03 15:17:28 +02004983 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02004984 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004985 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004986
Willy Tarreau58975672014-04-24 21:13:57 +02004987 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004988 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02004989 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004990 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02004991 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01004992
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004993 /* After this point, this anayzer can't return yield, so we can
4994 * remove the bit corresponding to this analyzer from the list.
4995 *
4996 * Note that the intermediate returns and goto found previously
4997 * reset the analyzers.
4998 */
4999 rep->analysers &= ~an_bit;
5000 rep->analyse_exp = TICK_ETERNITY;
5001
Willy Tarreau58975672014-04-24 21:13:57 +02005002 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005003 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005004 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005005
Willy Tarreau58975672014-04-24 21:13:57 +02005006 /*
5007 * Now check for a server cookie.
5008 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005009 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005010 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005011
Willy Tarreau58975672014-04-24 21:13:57 +02005012 /*
5013 * Check for cache-control or pragma headers if required.
5014 */
Willy Tarreau12b32f22017-12-21 16:08:09 +01005015 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Willy Tarreau58975672014-04-24 21:13:57 +02005016 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005017
Willy Tarreau58975672014-04-24 21:13:57 +02005018 /*
5019 * Add server cookie in the response if needed
5020 */
5021 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5022 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005023 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005024 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5025 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5026 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5027 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5028 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005029 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005030 /* the server is known, it's not the one the client requested, or the
5031 * cookie's last seen date needs to be refreshed. We have to
5032 * insert a set-cookie here, except if we want to insert only on POST
5033 * requests and this one isn't. Note that servers which don't have cookies
5034 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005035 */
Willy Tarreau58975672014-04-24 21:13:57 +02005036 if (!objt_server(s->target)->cookie) {
5037 chunk_printf(&trash,
5038 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5039 s->be->cookie_name);
5040 }
5041 else {
5042 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005043
Willy Tarreau58975672014-04-24 21:13:57 +02005044 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5045 /* emit last_date, which is mandatory */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005046 trash.area[trash.data++] = COOKIE_DELIM_DATE;
5047 s30tob64((date.tv_sec+3) >> 2,
5048 trash.area + trash.data);
5049 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005050
Willy Tarreau58975672014-04-24 21:13:57 +02005051 if (s->be->cookie_maxlife) {
5052 /* emit first_date, which is either the original one or
5053 * the current date.
5054 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005055 trash.area[trash.data++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005056 s30tob64(txn->cookie_first_date ?
5057 txn->cookie_first_date >> 2 :
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005058 (date.tv_sec+3) >> 2,
5059 trash.area + trash.data);
5060 trash.data += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005061 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005062 }
Willy Tarreau58975672014-04-24 21:13:57 +02005063 chunk_appendf(&trash, "; path=/");
5064 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005065
Willy Tarreau58975672014-04-24 21:13:57 +02005066 if (s->be->cookie_domain)
5067 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005068
Willy Tarreau58975672014-04-24 21:13:57 +02005069 if (s->be->ck_opts & PR_CK_HTTPONLY)
5070 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005071
Willy Tarreau58975672014-04-24 21:13:57 +02005072 if (s->be->ck_opts & PR_CK_SECURE)
5073 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005074
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005075 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0))
Willy Tarreau58975672014-04-24 21:13:57 +02005076 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005077
Willy Tarreau58975672014-04-24 21:13:57 +02005078 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaub05e48a2018-09-20 11:12:58 +02005079 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005080 /* the server did not change, only the date was updated */
5081 txn->flags |= TX_SCK_UPDATED;
5082 else
5083 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005084
Willy Tarreau58975672014-04-24 21:13:57 +02005085 /* Here, we will tell an eventual cache on the client side that we don't
5086 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5087 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5088 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005089 */
Willy Tarreau58975672014-04-24 21:13:57 +02005090 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005091
Willy Tarreau58975672014-04-24 21:13:57 +02005092 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005093
Willy Tarreau58975672014-04-24 21:13:57 +02005094 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5095 "Cache-control: private", 22) < 0))
5096 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005097 }
Willy Tarreau58975672014-04-24 21:13:57 +02005098 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005099
Willy Tarreau58975672014-04-24 21:13:57 +02005100 /*
5101 * Check if result will be cacheable with a cookie.
5102 * We'll block the response if security checks have caught
5103 * nasty things such as a cacheable cookie.
5104 */
5105 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5106 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5107 (s->be->options & PR_O_CHK_CACHE)) {
5108 /* we're in presence of a cacheable response containing
5109 * a set-cookie header. We'll block it as requested by
5110 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005111 */
Willy Tarreau58975672014-04-24 21:13:57 +02005112 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01005113 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005114
Olivier Houcharda798bf52019-03-08 18:52:00 +01005115 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5116 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005117 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01005118 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005119
Christopher Faulet767a84b2017-11-24 16:50:31 +01005120 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5121 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Willy Tarreau58975672014-04-24 21:13:57 +02005122 send_log(s->be, LOG_ALERT,
5123 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5124 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5125 goto return_srv_prx_502;
5126 }
Willy Tarreau03945942009-12-22 16:50:27 +01005127
Willy Tarreau70730dd2014-04-24 18:06:27 +02005128 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005129 /*
5130 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5131 * If an "Upgrade" token is found, the header is left untouched in order
5132 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005133 * if anything but "Upgrade" is present in the Connection header. We don't
5134 * want to touch any 101 response either since it's switching to another
5135 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005136 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005137 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Christopher Faulet315b39c2018-09-21 16:26:19 +02005138 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreau58975672014-04-24 21:13:57 +02005139 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005140
Willy Tarreau58975672014-04-24 21:13:57 +02005141 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5142 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5143 /* we want a keep-alive response here. Keep-alive header
5144 * required if either side is not 1.1.
5145 */
5146 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5147 want_flags |= TX_CON_KAL_SET;
5148 }
Christopher Faulet315b39c2018-09-21 16:26:19 +02005149 else { /* CLO */
Willy Tarreau58975672014-04-24 21:13:57 +02005150 /* we want a close response here. Close header required if
5151 * the server is 1.1, regardless of the client.
5152 */
5153 if (msg->flags & HTTP_MSGF_VER_11)
5154 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005155 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005156
Willy Tarreau58975672014-04-24 21:13:57 +02005157 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5158 http_change_connection_header(txn, msg, want_flags);
5159 }
5160
5161 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005162 /* Always enter in the body analyzer */
5163 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5164 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005165
Willy Tarreau58975672014-04-24 21:13:57 +02005166 /* if the user wants to log as soon as possible, without counting
5167 * bytes from the server, then this is the right moment. We have
5168 * to temporarily assign bytes_out to log what we currently have.
5169 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005170 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005171 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5172 s->logs.bytes_out = txn->rsp.eoh;
5173 s->do_log(s);
5174 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005175 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005176 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005177}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005178
Willy Tarreaud98cf932009-12-27 22:54:55 +01005179/* This function is an analyser which forwards response body (including chunk
5180 * sizes if any). It is called as soon as we must forward, even if we forward
5181 * zero byte. The only situation where it must not be called is when we're in
5182 * tunnel mode and we want to forward till the close. It's used both to forward
5183 * remaining data and to resync after end of body. It expects the msg_state to
5184 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005185 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005186 *
5187 * It is capable of compressing response data both in content-length mode and
5188 * in chunked mode. The state machines follows different flows depending on
5189 * whether content-length and chunked modes are used, since there are no
5190 * trailers in content-length :
5191 *
5192 * chk-mode cl-mode
5193 * ,----- BODY -----.
5194 * / \
5195 * V size > 0 V chk-mode
5196 * .--> SIZE -------------> DATA -------------> CRLF
5197 * | | size == 0 | last byte |
5198 * | v final crlf v inspected |
5199 * | TRAILERS -----------> DONE |
5200 * | |
5201 * `----------------------------------------------'
5202 *
5203 * Compression only happens in the DATA state, and must be flushed in final
5204 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5205 * is performed at once on final states for all bytes parsed, or when leaving
5206 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005207 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005208int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005209{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005210 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005211 struct http_txn *txn = s->txn;
5212 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005213 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005214
Christopher Faulete0768eb2018-10-03 16:38:02 +02005215 if (IS_HTX_STRM(s))
5216 return htx_response_forward_body(s, res, an_bit);
5217
Christopher Faulet45073512018-07-20 10:16:29 +02005218 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 +02005219 now_ms, __FUNCTION__,
5220 s,
5221 res,
5222 res->rex, res->wex,
5223 res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02005224 ci_data(res),
Christopher Faulet814d2702017-03-30 11:33:44 +02005225 res->analysers);
5226
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005227 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5228 return 0;
5229
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005230 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreauf37954d2018-06-15 18:31:02 +02005231 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005232 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005233 /* Output closed while we were sending data. We must abort and
5234 * wake the other side up.
5235 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005236 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005237 msg->msg_state = HTTP_MSG_ERROR;
5238 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005239 return 1;
5240 }
5241
Willy Tarreau4fe41902010-06-07 22:27:41 +02005242 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005243 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005244
Christopher Fauletd7c91962015-04-30 11:48:27 +02005245 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005246 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5247 ? HTTP_MSG_CHUNK_SIZE
5248 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005249 }
5250
Willy Tarreauefdf0942014-04-24 20:08:57 +02005251 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005252 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005253 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005254 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005255 }
5256
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005257 if (msg->msg_state < HTTP_MSG_DONE) {
5258 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5259 ? http_msg_forward_chunked_body(s, msg)
5260 : http_msg_forward_body(s, msg));
5261 if (!ret)
5262 goto missing_data_or_waiting;
5263 if (ret < 0)
5264 goto return_bad_res;
5265 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005266
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005267 /* other states, DONE...TUNNEL */
5268 /* for keep-alive we don't want to forward closes on DONE */
5269 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5270 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5271 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005272
Christopher Faulet894da4c2017-07-18 11:29:07 +02005273 http_resync_states(s);
5274 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005275 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5276 if (res->flags & CF_SHUTW) {
5277 /* response errors are most likely due to the
5278 * client aborting the transfer. */
5279 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005280 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005281 if (msg->err_pos >= 0)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005282 http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005283 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005284 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005285 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005286 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005287 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005288
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005289 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005290 if (res->flags & CF_SHUTW)
5291 goto aborted_xfer;
5292
5293 /* stop waiting for data if the input is closed before the end. If the
5294 * client side was already closed, it means that the client has aborted,
5295 * so we don't want to count this as a server abort. Otherwise it's a
5296 * server abort.
5297 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005298 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005299 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005300 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005301 /* If we have some pending data, we continue the processing */
Willy Tarreau5ba65522018-06-15 15:14:53 +02005302 if (!ci_data(res)) {
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005303 if (!(s->flags & SF_ERR_MASK))
5304 s->flags |= SF_ERR_SRVCL;
Olivier Houcharda798bf52019-03-08 18:52:00 +01005305 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005306 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01005307 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005308 goto return_bad_res_stats_ok;
5309 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005310 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005311
Willy Tarreau40dba092010-03-04 18:14:51 +01005312 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005313 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005314 goto return_bad_res;
5315
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005316 /* When TE: chunked is used, we need to get there again to parse
5317 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005318 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5319 * or if there are filters registered on the stream, we don't want to
5320 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005321 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005322 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005323 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005324 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5325 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005326 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005327
Willy Tarreau5c620922011-05-11 19:56:11 +02005328 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005329 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005330 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005331 * modes are already handled by the stream sock layer. We must not do
5332 * this in content-length mode because it could present the MSG_MORE
5333 * flag with the last block of forwarded data, which would cause an
5334 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005335 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005336 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005337 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005338
Willy Tarreau87b09662015-04-03 00:22:06 +02005339 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005340 return 0;
5341
Willy Tarreau40dba092010-03-04 18:14:51 +01005342 return_bad_res: /* let's centralize all bad responses */
Olivier Houcharda798bf52019-03-08 18:52:00 +01005343 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005344 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01005345 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005346
5347 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005348 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005349 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005350 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005351 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005352 res->analysers &= AN_RES_FLT_END;
5353 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005354 if (objt_server(s->target))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01005355 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005356
Willy Tarreaue7dff022015-04-03 01:14:29 +02005357 if (!(s->flags & SF_ERR_MASK))
5358 s->flags |= SF_ERR_PRXCOND;
5359 if (!(s->flags & SF_FINST_MASK))
5360 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005361 return 0;
5362
5363 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005364 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005365 txn->rsp.msg_state = HTTP_MSG_ERROR;
5366 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005367 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005368 res->analysers &= AN_RES_FLT_END;
5369 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005370
Olivier Houcharda798bf52019-03-08 18:52:00 +01005371 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5372 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005373 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01005374 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005375
Willy Tarreaue7dff022015-04-03 01:14:29 +02005376 if (!(s->flags & SF_ERR_MASK))
5377 s->flags |= SF_ERR_CLICL;
5378 if (!(s->flags & SF_FINST_MASK))
5379 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005380 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005381}
5382
5383
Christopher Faulet10079f52018-10-03 15:17:28 +02005384int http_msg_forward_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005385{
5386 struct channel *chn = msg->chn;
5387 int ret;
5388
5389 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
5390
5391 if (msg->msg_state == HTTP_MSG_ENDING)
5392 goto ending;
5393
5394 /* Neither content-length, nor transfer-encoding was found, so we must
5395 * read the body until the server connection is closed. In that case, we
5396 * eat data as they come. Of course, this happens for response only. */
5397 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005398 unsigned long long len = ci_data(chn) - msg->next;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005399 msg->chunk_len += len;
5400 msg->body_len += len;
5401 }
Christopher Fauletda02e172015-12-04 09:25:05 +01005402 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005403 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005404 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005405 msg->next += ret;
5406 msg->chunk_len -= ret;
5407 if (msg->chunk_len) {
5408 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005409 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005410 chn->flags |= CF_WAKE_WRITE;
5411 goto missing_data_or_waiting;
5412 }
5413
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005414 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
5415 * always set for a request. */
5416 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
5417 /* The server still sending data that should be filtered */
5418 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
5419 goto missing_data_or_waiting;
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005420 msg->msg_state = HTTP_MSG_TUNNEL;
5421 goto ending;
Christopher Faulet1486b0a2017-07-18 11:42:08 +02005422 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005423
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005424 msg->msg_state = HTTP_MSG_ENDING;
5425
5426 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005427 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005428 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005429 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5430 /* default_ret */ msg->next,
5431 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005432 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005433 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005434 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5435 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005436 if (msg->next)
5437 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005438
Christopher Fauletda02e172015-12-04 09:25:05 +01005439 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
5440 /* default_ret */ 1,
5441 /* on_error */ goto error,
5442 /* on_wait */ goto waiting);
Christopher Fauletfd04fcf2018-02-02 15:54:15 +01005443 if (msg->msg_state == HTTP_MSG_ENDING)
5444 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005445 return 1;
5446
5447 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005448 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005449 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
5450 /* default_ret */ msg->next,
5451 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005452 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005453 msg->next -= ret;
5454 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5455 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005456 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005457 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005458 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005459 return 0;
5460 error:
5461 return -1;
5462}
5463
Christopher Faulet10079f52018-10-03 15:17:28 +02005464int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005465{
5466 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005467 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005468 int ret;
5469
5470 /* Here we have the guarantee to be in one of the following state:
5471 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
5472 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
5473
Christopher Fauletca874b82018-09-20 11:31:01 +02005474 if (msg->msg_state == HTTP_MSG_ENDING)
5475 goto ending;
5476
5477 /* Don't parse chunks if there is no input data */
5478 if (!ci_data(chn))
5479 goto waiting;
5480
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005481 switch_states:
5482 switch (msg->msg_state) {
5483 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01005484 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
Willy Tarreauf37954d2018-06-15 18:31:02 +02005485 /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next),
Christopher Fauletda02e172015-12-04 09:25:05 +01005486 /* on_error */ goto error);
5487 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005488 msg->chunk_len -= ret;
5489 if (msg->chunk_len) {
5490 /* input empty or output full */
Willy Tarreauf37954d2018-06-15 18:31:02 +02005491 if (ci_data(chn) > msg->next)
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005492 chn->flags |= CF_WAKE_WRITE;
5493 goto missing_data_or_waiting;
5494 }
5495
5496 /* nothing left to forward for this chunk*/
5497 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
5498 /* fall through for HTTP_MSG_CHUNK_CRLF */
5499
5500 case HTTP_MSG_CHUNK_CRLF:
5501 /* we want the CRLF after the data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005502 ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005503 if (ret == 0)
5504 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02005505 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005506 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaub2892562017-09-21 11:33:54 +02005507 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005508 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005509 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02005510 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005511 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005512 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5513 /* fall through for HTTP_MSG_CHUNK_SIZE */
5514
5515 case HTTP_MSG_CHUNK_SIZE:
5516 /* read the chunk size and assign it to ->chunk_len,
5517 * then set ->next to point to the body and switch to
5518 * DATA or TRAILERS state.
5519 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005520 ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005521 if (ret == 0)
5522 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005523 if (ret < 0) {
Willy Tarreauf37954d2018-06-15 18:31:02 +02005524 msg->err_pos = ci_data(chn) + ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005525 if (msg->err_pos < 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005526 msg->err_pos += chn->buf.size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005527 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005528 }
5529
5530 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01005531 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02005532 msg->chunk_len = chunk;
5533 msg->body_len += chunk;
5534
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005535 if (msg->chunk_len) {
5536 msg->msg_state = HTTP_MSG_DATA;
5537 goto switch_states;
5538 }
5539 msg->msg_state = HTTP_MSG_TRAILERS;
5540 /* fall through for HTTP_MSG_TRAILERS */
5541
5542 case HTTP_MSG_TRAILERS:
5543 ret = http_forward_trailers(msg);
5544 if (ret < 0)
5545 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01005546 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
5547 /* default_ret */ 1,
5548 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005549 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005550 if (!ret)
5551 goto missing_data_or_waiting;
5552 break;
5553
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005554 default:
5555 /* This should no happen in this function */
5556 goto error;
5557 }
5558
5559 msg->msg_state = HTTP_MSG_ENDING;
5560 ending:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005561 /* we may have some pending data starting at res->buf.p such as a last
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005562 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01005563 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005564 /* default_ret */ msg->next,
5565 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005566 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005567 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005568 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
5569 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02005570 if (msg->next)
5571 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02005572
Christopher Fauletda02e172015-12-04 09:25:05 +01005573 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005574 /* default_ret */ 1,
5575 /* on_error */ goto error,
5576 /* on_wait */ goto waiting);
5577 msg->msg_state = HTTP_MSG_DONE;
5578 return 1;
5579
5580 missing_data_or_waiting:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005581 /* we may have some pending data starting at chn->buf.p */
Christopher Fauletda02e172015-12-04 09:25:05 +01005582 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005583 /* default_ret */ msg->next,
5584 /* on_error */ goto error);
Willy Tarreaubcbd3932018-06-06 07:13:22 +02005585 c_adv(chn, ret);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005586 msg->next -= ret;
5587 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
5588 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01005589 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005590 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02005591 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005592 return 0;
5593
5594 chunk_parsing_error:
5595 if (msg->err_pos >= 0) {
5596 if (chn->flags & CF_ISRESP)
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005597 http_capture_bad_message(s->be, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005598 msg->msg_state, strm_fe(s));
5599 else
Willy Tarreaufd9419d2018-09-07 18:01:03 +02005600 http_capture_bad_message(strm_fe(s), s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005601 msg, msg->msg_state, s->be);
5602 }
5603 error:
5604 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005605}
5606
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005607
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005608/* Iterate the same filter through all request headers.
5609 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005610 * Since it can manage the switch to another backend, it updates the per-proxy
5611 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005612 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005613int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005614{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005615 char *cur_ptr, *cur_end, *cur_next;
5616 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005617 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005618 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005619 int delta, len;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005620
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005621 last_hdr = 0;
5622
Willy Tarreauf37954d2018-06-15 18:31:02 +02005623 cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005624 old_idx = 0;
5625
5626 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005627 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005628 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005629 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005630 (exp->action == ACT_ALLOW ||
5631 exp->action == ACT_DENY ||
5632 exp->action == ACT_TARPIT))
5633 return 0;
5634
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005635 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005636 if (!cur_idx)
5637 break;
5638
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005639 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005640 cur_ptr = cur_next;
5641 cur_end = cur_ptr + cur_hdr->len;
5642 cur_next = cur_end + cur_hdr->cr + 1;
5643
5644 /* Now we have one header between cur_ptr and cur_end,
5645 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005646 */
5647
Willy Tarreau15a53a42015-01-21 13:39:42 +01005648 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005649 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005650 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005651 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005652 last_hdr = 1;
5653 break;
5654
5655 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005656 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005657 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005658 break;
5659
5660 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005661 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005662 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005663 break;
5664
5665 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005666 len = exp_replace(trash.area,
5667 trash.size, cur_ptr,
5668 exp->replace, pmatch);
5669 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005670 return -1;
5671
Willy Tarreau6e27be12018-08-22 04:46:47 +02005672 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5673
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005674 /* FIXME: if the user adds a newline in the replacement, the
5675 * index will not be recalculated for now, and the new line
5676 * will not be counted as a new header.
5677 */
5678
5679 cur_end += delta;
5680 cur_next += delta;
5681 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005682 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005683 break;
5684
5685 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02005686 delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005687 cur_next += delta;
5688
Willy Tarreaufa355d42009-11-29 18:12:29 +01005689 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005690 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5691 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005692 cur_hdr->len = 0;
5693 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005694 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005695 break;
5696
5697 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005698 }
5699
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005700 /* keep the link from this header to next one in case of later
5701 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005702 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005703 old_idx = cur_idx;
5704 }
5705 return 0;
5706}
5707
5708
5709/* Apply the filter to the request line.
5710 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5711 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005712 * Since it can manage the switch to another backend, it updates the per-proxy
5713 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005714 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005715int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005716{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005717 char *cur_ptr, *cur_end;
5718 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005719 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02005720 int delta, len;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005721
Willy Tarreau3d300592007-03-18 18:34:41 +01005722 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005723 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005724 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005725 (exp->action == ACT_ALLOW ||
5726 exp->action == ACT_DENY ||
5727 exp->action == ACT_TARPIT))
5728 return 0;
5729 else if (exp->action == ACT_REMOVE)
5730 return 0;
5731
5732 done = 0;
5733
Willy Tarreauf37954d2018-06-15 18:31:02 +02005734 cur_ptr = ci_head(req);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005735 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005736
5737 /* Now we have the request line between cur_ptr and cur_end */
5738
Willy Tarreau15a53a42015-01-21 13:39:42 +01005739 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005740 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005741 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005742 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005743 done = 1;
5744 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005745
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005746 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005747 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005748 done = 1;
5749 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005751 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005752 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005753 done = 1;
5754 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005755
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005756 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02005757 len = exp_replace(trash.area, trash.size,
5758 cur_ptr, exp->replace, pmatch);
5759 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06005760 return -1;
5761
Willy Tarreau6e27be12018-08-22 04:46:47 +02005762 delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len);
5763
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005764 /* FIXME: if the user adds a newline in the replacement, the
5765 * index will not be recalculated for now, and the new line
5766 * will not be counted as a new header.
5767 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005768
Willy Tarreaufa355d42009-11-29 18:12:29 +01005769 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005770 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02005771 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005772 HTTP_MSG_RQMETH,
5773 cur_ptr, cur_end + 1,
5774 NULL, NULL);
5775 if (unlikely(!cur_end))
5776 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005777
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005778 /* we have a full request and we know that we have either a CR
5779 * or an LF at <ptr>.
5780 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005781 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5782 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005783 /* there is no point trying this regex on headers */
5784 return 1;
5785 }
5786 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005787 return done;
5788}
Willy Tarreau97de6242006-12-27 17:18:38 +01005789
Willy Tarreau58f10d72006-12-04 02:26:12 +01005790
Willy Tarreau58f10d72006-12-04 02:26:12 +01005791
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005792/*
Willy Tarreau87b09662015-04-03 00:22:06 +02005793 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005794 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005795 * unparsable request. Since it can manage the switch to another backend, it
5796 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005797 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005798int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005799{
Willy Tarreau192252e2015-04-04 01:47:55 +02005800 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005801 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005802 struct hdr_exp *exp;
5803
5804 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005805 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005806
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005807 /*
5808 * The interleaving of transformations and verdicts
5809 * makes it difficult to decide to continue or stop
5810 * the evaluation.
5811 */
5812
Willy Tarreau6c123b12010-01-28 20:22:06 +01005813 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5814 break;
5815
Willy Tarreau3d300592007-03-18 18:34:41 +01005816 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005817 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005818 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005819 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005820
5821 /* if this filter had a condition, evaluate it now and skip to
5822 * next filter if the condition does not match.
5823 */
5824 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005825 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01005826 ret = acl_pass(ret);
5827 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5828 ret = !ret;
5829
5830 if (!ret)
5831 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005832 }
5833
5834 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005835 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005836 if (unlikely(ret < 0))
5837 return -1;
5838
5839 if (likely(ret == 0)) {
5840 /* The filter did not match the request, it can be
5841 * iterated through all headers.
5842 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01005843 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
5844 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005845 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005846 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005847 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005848}
5849
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005850
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005851/* Delete a value in a header between delimiters <from> and <next> in buffer
5852 * <buf>. The number of characters displaced is returned, and the pointer to
5853 * the first delimiter is updated if required. The function tries as much as
5854 * possible to respect the following principles :
5855 * - replace <from> delimiter by the <next> one unless <from> points to a
5856 * colon, in which case <next> is simply removed
5857 * - set exactly one space character after the new first delimiter, unless
5858 * there are not enough characters in the block being moved to do so.
5859 * - remove unneeded spaces before the previous delimiter and after the new
5860 * one.
5861 *
5862 * It is the caller's responsibility to ensure that :
5863 * - <from> points to a valid delimiter or the colon ;
5864 * - <next> points to a valid delimiter or the final CR/LF ;
5865 * - there are non-space chars before <from> ;
5866 * - there is a CR/LF at or after <next>.
5867 */
Willy Tarreauc5a4fd52018-12-11 11:42:27 +01005868static int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005869{
5870 char *prev = *from;
5871
5872 if (*prev == ':') {
5873 /* We're removing the first value, preserve the colon and add a
5874 * space if possible.
5875 */
Willy Tarreau2235b262016-11-05 15:50:20 +01005876 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005877 next++;
5878 prev++;
5879 if (prev < next)
5880 *prev++ = ' ';
5881
Willy Tarreau2235b262016-11-05 15:50:20 +01005882 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005883 next++;
5884 } else {
5885 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01005886 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005887 prev--;
5888 *from = prev;
5889
5890 /* copy the delimiter and if possible a space if we're
5891 * not at the end of the line.
5892 */
Willy Tarreau2235b262016-11-05 15:50:20 +01005893 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005894 *prev++ = *next++;
5895 if (prev + 1 < next)
5896 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01005897 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005898 next++;
5899 }
5900 }
Willy Tarreaue3128022018-07-12 15:55:34 +02005901 return b_rep_blk(buf, prev, next, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005902}
5903
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005904/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005905 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005906 * desirable to call it only when needed. This code is quite complex because
5907 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5908 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005909 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005910void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005911{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005912 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005913 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005914 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005915 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005916 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5917 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005918
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005919 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005920 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02005921 hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005922
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005923 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005924 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005925 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005926
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005927 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005928 hdr_beg = hdr_next;
5929 hdr_end = hdr_beg + cur_hdr->len;
5930 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005931
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005932 /* We have one full header between hdr_beg and hdr_end, and the
5933 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005934 * "Cookie:" headers.
5935 */
5936
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005937 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005938 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005939 old_idx = cur_idx;
5940 continue;
5941 }
5942
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005943 del_from = NULL; /* nothing to be deleted */
5944 preserve_hdr = 0; /* assume we may kill the whole header */
5945
Willy Tarreau58f10d72006-12-04 02:26:12 +01005946 /* Now look for cookies. Conforming to RFC2109, we have to support
5947 * attributes whose name begin with a '$', and associate them with
5948 * the right cookie, if we want to delete this cookie.
5949 * So there are 3 cases for each cookie read :
5950 * 1) it's a special attribute, beginning with a '$' : ignore it.
5951 * 2) it's a server id cookie that we *MAY* want to delete : save
5952 * some pointers on it (last semi-colon, beginning of cookie...)
5953 * 3) it's an application cookie : we *MAY* have to delete a previous
5954 * "special" cookie.
5955 * At the end of loop, if a "special" cookie remains, we may have to
5956 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005957 * *MUST* delete it.
5958 *
5959 * Note: RFC2965 is unclear about the processing of spaces around
5960 * the equal sign in the ATTR=VALUE form. A careful inspection of
5961 * the RFC explicitly allows spaces before it, and not within the
5962 * tokens (attrs or values). An inspection of RFC2109 allows that
5963 * too but section 10.1.3 lets one think that spaces may be allowed
5964 * after the equal sign too, resulting in some (rare) buggy
5965 * implementations trying to do that. So let's do what servers do.
5966 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5967 * allowed quoted strings in values, with any possible character
5968 * after a backslash, including control chars and delimitors, which
5969 * causes parsing to become ambiguous. Browsers also allow spaces
5970 * within values even without quotes.
5971 *
5972 * We have to keep multiple pointers in order to support cookie
5973 * removal at the beginning, middle or end of header without
5974 * corrupting the header. All of these headers are valid :
5975 *
5976 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5977 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5978 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5979 * | | | | | | | | |
5980 * | | | | | | | | hdr_end <--+
5981 * | | | | | | | +--> next
5982 * | | | | | | +----> val_end
5983 * | | | | | +-----------> val_beg
5984 * | | | | +--------------> equal
5985 * | | | +----------------> att_end
5986 * | | +---------------------> att_beg
5987 * | +--------------------------> prev
5988 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005989 */
5990
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005991 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
5992 /* Iterate through all cookies on this line */
5993
5994 /* find att_beg */
5995 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01005996 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005997 att_beg++;
5998
5999 /* find att_end : this is the first character after the last non
6000 * space before the equal. It may be equal to hdr_end.
6001 */
6002 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006003
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006004 while (equal < hdr_end) {
6005 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006006 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006007 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006008 continue;
6009 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006010 }
6011
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006012 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6013 * is between <att_beg> and <equal>, both may be identical.
6014 */
6015
6016 /* look for end of cookie if there is an equal sign */
6017 if (equal < hdr_end && *equal == '=') {
6018 /* look for the beginning of the value */
6019 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006020 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006021 val_beg++;
6022
6023 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006024 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006025
6026 /* make val_end point to the first white space or delimitor after the value */
6027 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006028 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006029 val_end--;
6030 } else {
6031 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006032 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006033
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006034 /* We have nothing to do with attributes beginning with '$'. However,
6035 * they will automatically be removed if a header before them is removed,
6036 * since they're supposed to be linked together.
6037 */
6038 if (*att_beg == '$')
6039 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006040
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006041 /* Ignore cookies with no equal sign */
6042 if (equal == next) {
6043 /* This is not our cookie, so we must preserve it. But if we already
6044 * scheduled another cookie for removal, we cannot remove the
6045 * complete header, but we can remove the previous block itself.
6046 */
6047 preserve_hdr = 1;
6048 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006049 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006050 val_end += delta;
6051 next += delta;
6052 hdr_end += delta;
6053 hdr_next += delta;
6054 cur_hdr->len += delta;
6055 http_msg_move_end(&txn->req, delta);
6056 prev = del_from;
6057 del_from = NULL;
6058 }
6059 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006060 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006061
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006062 /* if there are spaces around the equal sign, we need to
6063 * strip them otherwise we'll get trouble for cookie captures,
6064 * or even for rewrites. Since this happens extremely rarely,
6065 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006066 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006067 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6068 int stripped_before = 0;
6069 int stripped_after = 0;
6070
6071 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006072 stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006073 equal += stripped_before;
6074 val_beg += stripped_before;
6075 }
6076
6077 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006078 stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006079 val_beg += stripped_after;
6080 stripped_before += stripped_after;
6081 }
6082
6083 val_end += stripped_before;
6084 next += stripped_before;
6085 hdr_end += stripped_before;
6086 hdr_next += stripped_before;
6087 cur_hdr->len += stripped_before;
6088 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006089 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006090 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006091
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006092 /* First, let's see if we want to capture this cookie. We check
6093 * that we don't already have a client side cookie, because we
6094 * can only capture one. Also as an optimisation, we ignore
6095 * cookies shorter than the declared name.
6096 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006097 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6098 (val_end - att_beg >= sess->fe->capture_namelen) &&
6099 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006100 int log_len = val_end - att_beg;
6101
Willy Tarreaubafbe012017-11-24 17:34:44 +01006102 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006103 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006104 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006105 if (log_len > sess->fe->capture_len)
6106 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006107 memcpy(txn->cli_cookie, att_beg, log_len);
6108 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006109 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006110 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006111
Willy Tarreaubca99692010-10-06 19:25:55 +02006112 /* Persistence cookies in passive, rewrite or insert mode have the
6113 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006114 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006115 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006116 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006117 * For cookies in prefix mode, the form is :
6118 *
6119 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006120 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006121 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6122 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6123 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006124 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006125
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006126 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6127 * have the server ID between val_beg and delim, and the original cookie between
6128 * delim+1 and val_end. Otherwise, delim==val_end :
6129 *
6130 * Cookie: NAME=SRV; # in all but prefix modes
6131 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6132 * | || || | |+-> next
6133 * | || || | +--> val_end
6134 * | || || +---------> delim
6135 * | || |+------------> val_beg
6136 * | || +-------------> att_end = equal
6137 * | |+-----------------> att_beg
6138 * | +------------------> prev
6139 * +-------------------------> hdr_beg
6140 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006141
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006142 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006143 for (delim = val_beg; delim < val_end; delim++)
6144 if (*delim == COOKIE_DELIM)
6145 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006146 } else {
6147 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006148 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006149 /* Now check if the cookie contains a date field, which would
6150 * appear after a vertical bar ('|') just after the server name
6151 * and before the delimiter.
6152 */
6153 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6154 if (vbar1) {
6155 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006156 * right is the last seen date. It is a base64 encoded
6157 * 30-bit value representing the UNIX date since the
6158 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006159 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006160 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006161 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006162 if (val_end - vbar1 >= 5) {
6163 val = b64tos30(vbar1);
6164 if (val > 0)
6165 txn->cookie_last_date = val << 2;
6166 }
6167 /* look for a second vertical bar */
6168 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6169 if (vbar1 && (val_end - vbar1 > 5)) {
6170 val = b64tos30(vbar1 + 1);
6171 if (val > 0)
6172 txn->cookie_first_date = val << 2;
6173 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006174 }
6175 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006176
Willy Tarreauf64d1412010-10-07 20:06:11 +02006177 /* if the cookie has an expiration date and the proxy wants to check
6178 * it, then we do that now. We first check if the cookie is too old,
6179 * then only if it has expired. We detect strict overflow because the
6180 * time resolution here is not great (4 seconds). Cookies with dates
6181 * in the future are ignored if their offset is beyond one day. This
6182 * allows an admin to fix timezone issues without expiring everyone
6183 * and at the same time avoids keeping unwanted side effects for too
6184 * long.
6185 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006186 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6187 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006188 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006189 txn->flags &= ~TX_CK_MASK;
6190 txn->flags |= TX_CK_OLD;
6191 delim = val_beg; // let's pretend we have not found the cookie
6192 txn->cookie_first_date = 0;
6193 txn->cookie_last_date = 0;
6194 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006195 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6196 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006197 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006198 txn->flags &= ~TX_CK_MASK;
6199 txn->flags |= TX_CK_EXPIRED;
6200 delim = val_beg; // let's pretend we have not found the cookie
6201 txn->cookie_first_date = 0;
6202 txn->cookie_last_date = 0;
6203 }
6204
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006205 /* Here, we'll look for the first running server which supports the cookie.
6206 * This allows to share a same cookie between several servers, for example
6207 * to dedicate backup servers to specific servers only.
6208 * However, to prevent clients from sticking to cookie-less backup server
6209 * when they have incidentely learned an empty cookie, we simply ignore
6210 * empty cookies and mark them as invalid.
6211 * The same behaviour is applied when persistence must be ignored.
6212 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006213 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006214 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006215
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006216 while (srv) {
6217 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6218 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006219 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006220 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006221 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006222 /* we found the server and we can use it */
6223 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006224 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006225 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006226 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006227 break;
6228 } else {
6229 /* we found a server, but it's down,
6230 * mark it as such and go on in case
6231 * another one is available.
6232 */
6233 txn->flags &= ~TX_CK_MASK;
6234 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006235 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006236 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006237 srv = srv->next;
6238 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006239
Willy Tarreauf64d1412010-10-07 20:06:11 +02006240 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006241 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006242 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006243 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006244 txn->flags |= TX_CK_UNUSED;
6245 else
6246 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006247 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006248
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006249 /* depending on the cookie mode, we may have to either :
6250 * - delete the complete cookie if we're in insert+indirect mode, so that
6251 * the server never sees it ;
6252 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlant5ba80252018-11-15 09:25:36 -08006253 * application cookie so that it does not get accidently removed later,
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006254 * if we're in cookie prefix mode
6255 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006256 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006257 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006258
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006259 delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006260 val_end += delta;
6261 next += delta;
6262 hdr_end += delta;
6263 hdr_next += delta;
6264 cur_hdr->len += delta;
6265 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006266
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006267 del_from = NULL;
6268 preserve_hdr = 1; /* we want to keep this cookie */
6269 }
6270 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006271 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006272 del_from = prev;
6273 }
6274 } else {
6275 /* This is not our cookie, so we must preserve it. But if we already
6276 * scheduled another cookie for removal, we cannot remove the
6277 * complete header, but we can remove the previous block itself.
6278 */
6279 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006280
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006281 if (del_from != NULL) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006282 int delta = del_hdr_value(&req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006283 if (att_beg >= del_from)
6284 att_beg += delta;
6285 if (att_end >= del_from)
6286 att_end += delta;
6287 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006288 val_end += delta;
6289 next += delta;
6290 hdr_end += delta;
6291 hdr_next += delta;
6292 cur_hdr->len += delta;
6293 http_msg_move_end(&txn->req, delta);
6294 prev = del_from;
6295 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006296 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006297 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006298
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006299 /* continue with next cookie on this header line */
6300 att_beg = next;
6301 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006302
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006303 /* There are no more cookies on this line.
6304 * We may still have one (or several) marked for deletion at the
6305 * end of the line. We must do this now in two ways :
6306 * - if some cookies must be preserved, we only delete from the
6307 * mark to the end of line ;
6308 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006309 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006310 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006311 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006312 if (preserve_hdr) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006313 delta = del_hdr_value(&req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006314 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006315 cur_hdr->len += delta;
6316 } else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006317 delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006318
6319 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006320 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6321 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006322 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006323 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006324 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006325 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006326 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006327 }
6328
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006329 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006330 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006331 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006332}
6333
6334
Willy Tarreaua15645d2007-03-18 16:22:39 +01006335/* Iterate the same filter through all response headers contained in <rtr>.
6336 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6337 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006338int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006339{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006340 char *cur_ptr, *cur_end, *cur_next;
6341 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006342 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006343 struct hdr_idx_elem *cur_hdr;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006344 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006345
6346 last_hdr = 0;
6347
Willy Tarreauf37954d2018-06-15 18:31:02 +02006348 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006349 old_idx = 0;
6350
6351 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006352 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006353 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006354 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006355 (exp->action == ACT_ALLOW ||
6356 exp->action == ACT_DENY))
6357 return 0;
6358
6359 cur_idx = txn->hdr_idx.v[old_idx].next;
6360 if (!cur_idx)
6361 break;
6362
6363 cur_hdr = &txn->hdr_idx.v[cur_idx];
6364 cur_ptr = cur_next;
6365 cur_end = cur_ptr + cur_hdr->len;
6366 cur_next = cur_end + cur_hdr->cr + 1;
6367
6368 /* Now we have one header between cur_ptr and cur_end,
6369 * and the next header starts at cur_next.
6370 */
6371
Willy Tarreau15a53a42015-01-21 13:39:42 +01006372 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006373 switch (exp->action) {
6374 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006375 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006376 last_hdr = 1;
6377 break;
6378
6379 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006380 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006381 last_hdr = 1;
6382 break;
6383
6384 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006385 len = exp_replace(trash.area,
6386 trash.size, cur_ptr,
6387 exp->replace, pmatch);
6388 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006389 return -1;
6390
Willy Tarreau6e27be12018-08-22 04:46:47 +02006391 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6392
Willy Tarreaua15645d2007-03-18 16:22:39 +01006393 /* FIXME: if the user adds a newline in the replacement, the
6394 * index will not be recalculated for now, and the new line
6395 * will not be counted as a new header.
6396 */
6397
6398 cur_end += delta;
6399 cur_next += delta;
6400 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006401 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006402 break;
6403
6404 case ACT_REMOVE:
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006405 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006406 cur_next += delta;
6407
Willy Tarreaufa355d42009-11-29 18:12:29 +01006408 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006409 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6410 txn->hdr_idx.used--;
6411 cur_hdr->len = 0;
6412 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006413 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006414 break;
6415
6416 }
6417 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006418
6419 /* keep the link from this header to next one in case of later
6420 * removal of next header.
6421 */
6422 old_idx = cur_idx;
6423 }
6424 return 0;
6425}
6426
6427
6428/* Apply the filter to the status line in the response buffer <rtr>.
6429 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6430 * or -1 if a replacement resulted in an invalid status line.
6431 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006432int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006433{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006434 char *cur_ptr, *cur_end;
6435 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006436 struct http_txn *txn = s->txn;
Willy Tarreau6e27be12018-08-22 04:46:47 +02006437 int delta, len;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006438
Willy Tarreau3d300592007-03-18 18:34:41 +01006439 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006440 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006441 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006442 (exp->action == ACT_ALLOW ||
6443 exp->action == ACT_DENY))
6444 return 0;
6445 else if (exp->action == ACT_REMOVE)
6446 return 0;
6447
6448 done = 0;
6449
Willy Tarreauf37954d2018-06-15 18:31:02 +02006450 cur_ptr = ci_head(rtr);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006451 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006452
6453 /* Now we have the status line between cur_ptr and cur_end */
6454
Willy Tarreau15a53a42015-01-21 13:39:42 +01006455 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006456 switch (exp->action) {
6457 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006458 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006459 done = 1;
6460 break;
6461
6462 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006463 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006464 done = 1;
6465 break;
6466
6467 case ACT_REPLACE:
Willy Tarreau6e27be12018-08-22 04:46:47 +02006468 len = exp_replace(trash.area, trash.size,
6469 cur_ptr, exp->replace, pmatch);
6470 if (len < 0)
Sasha Pachevc6002042014-05-26 12:33:48 -06006471 return -1;
6472
Willy Tarreau6e27be12018-08-22 04:46:47 +02006473 delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len);
6474
Willy Tarreaua15645d2007-03-18 16:22:39 +01006475 /* FIXME: if the user adds a newline in the replacement, the
6476 * index will not be recalculated for now, and the new line
6477 * will not be counted as a new header.
6478 */
6479
Willy Tarreaufa355d42009-11-29 18:12:29 +01006480 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006481 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006482 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006483 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006484 cur_ptr, cur_end + 1,
6485 NULL, NULL);
6486 if (unlikely(!cur_end))
6487 return -1;
6488
6489 /* we have a full respnse and we know that we have either a CR
6490 * or an LF at <ptr>.
6491 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02006492 txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006493 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006494 /* there is no point trying this regex on headers */
6495 return 1;
6496 }
6497 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006498 return done;
6499}
6500
6501
6502
6503/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006504 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006505 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6506 * unparsable response.
6507 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006508int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006509{
Willy Tarreau192252e2015-04-04 01:47:55 +02006510 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006511 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006512 struct hdr_exp *exp;
6513
6514 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006515 int ret;
6516
6517 /*
6518 * The interleaving of transformations and verdicts
6519 * makes it difficult to decide to continue or stop
6520 * the evaluation.
6521 */
6522
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006523 if (txn->flags & TX_SVDENY)
6524 break;
6525
Willy Tarreau3d300592007-03-18 18:34:41 +01006526 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006527 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6528 exp->action == ACT_PASS)) {
6529 exp = exp->next;
6530 continue;
6531 }
6532
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006533 /* if this filter had a condition, evaluate it now and skip to
6534 * next filter if the condition does not match.
6535 */
6536 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006537 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006538 ret = acl_pass(ret);
6539 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6540 ret = !ret;
6541 if (!ret)
6542 continue;
6543 }
6544
Willy Tarreaua15645d2007-03-18 16:22:39 +01006545 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006546 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006547 if (unlikely(ret < 0))
6548 return -1;
6549
6550 if (likely(ret == 0)) {
6551 /* The filter did not match the response, it can be
6552 * iterated through all headers.
6553 */
Sasha Pachevc6002042014-05-26 12:33:48 -06006554 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
6555 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006556 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006557 }
6558 return 0;
6559}
6560
6561
Willy Tarreaua15645d2007-03-18 16:22:39 +01006562/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006563 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006564 * desirable to call it only when needed. This function is also used when we
6565 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006566 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006567void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006568{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006569 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006570 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01006571 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006572 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006573 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006574 char *hdr_beg, *hdr_end, *hdr_next;
6575 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006576
Willy Tarreaua15645d2007-03-18 16:22:39 +01006577 /* Iterate through the headers.
6578 * we start with the start line.
6579 */
6580 old_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006581 hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006582
6583 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6584 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006585 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006586
6587 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006588 hdr_beg = hdr_next;
6589 hdr_end = hdr_beg + cur_hdr->len;
6590 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006591
Willy Tarreau24581ba2010-08-31 22:39:35 +02006592 /* We have one full header between hdr_beg and hdr_end, and the
6593 * next header starts at hdr_next. We're only interested in
6594 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006595 */
6596
Willy Tarreau24581ba2010-08-31 22:39:35 +02006597 is_cookie2 = 0;
6598 prev = hdr_beg + 10;
6599 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006600 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006601 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6602 if (!val) {
6603 old_idx = cur_idx;
6604 continue;
6605 }
6606 is_cookie2 = 1;
6607 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006608 }
6609
Willy Tarreau24581ba2010-08-31 22:39:35 +02006610 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6611 * <prev> points to the colon.
6612 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006613 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006614
Willy Tarreau24581ba2010-08-31 22:39:35 +02006615 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6616 * check-cache is enabled) and we are not interested in checking
6617 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006618 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006619 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006620 return;
6621
Willy Tarreau24581ba2010-08-31 22:39:35 +02006622 /* OK so now we know we have to process this response cookie.
6623 * The format of the Set-Cookie header is slightly different
6624 * from the format of the Cookie header in that it does not
6625 * support the comma as a cookie delimiter (thus the header
6626 * cannot be folded) because the Expires attribute described in
6627 * the original Netscape's spec may contain an unquoted date
6628 * with a comma inside. We have to live with this because
6629 * many browsers don't support Max-Age and some browsers don't
6630 * support quoted strings. However the Set-Cookie2 header is
6631 * clean.
6632 *
6633 * We have to keep multiple pointers in order to support cookie
6634 * removal at the beginning, middle or end of header without
6635 * corrupting the header (in case of set-cookie2). A special
6636 * pointer, <scav> points to the beginning of the set-cookie-av
6637 * fields after the first semi-colon. The <next> pointer points
6638 * either to the end of line (set-cookie) or next unquoted comma
6639 * (set-cookie2). All of these headers are valid :
6640 *
6641 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6642 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6643 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6644 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6645 * | | | | | | | | | |
6646 * | | | | | | | | +-> next hdr_end <--+
6647 * | | | | | | | +------------> scav
6648 * | | | | | | +--------------> val_end
6649 * | | | | | +--------------------> val_beg
6650 * | | | | +----------------------> equal
6651 * | | | +------------------------> att_end
6652 * | | +----------------------------> att_beg
6653 * | +------------------------------> prev
6654 * +-----------------------------------------> hdr_beg
6655 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006656
Willy Tarreau24581ba2010-08-31 22:39:35 +02006657 for (; prev < hdr_end; prev = next) {
6658 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006659
Willy Tarreau24581ba2010-08-31 22:39:35 +02006660 /* find att_beg */
6661 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006662 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006663 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006664
Willy Tarreau24581ba2010-08-31 22:39:35 +02006665 /* find att_end : this is the first character after the last non
6666 * space before the equal. It may be equal to hdr_end.
6667 */
6668 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006669
Willy Tarreau24581ba2010-08-31 22:39:35 +02006670 while (equal < hdr_end) {
6671 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6672 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006673 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006674 continue;
6675 att_end = equal;
6676 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006677
Willy Tarreau24581ba2010-08-31 22:39:35 +02006678 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6679 * is between <att_beg> and <equal>, both may be identical.
6680 */
6681
6682 /* look for end of cookie if there is an equal sign */
6683 if (equal < hdr_end && *equal == '=') {
6684 /* look for the beginning of the value */
6685 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006686 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006687 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006688
Willy Tarreau24581ba2010-08-31 22:39:35 +02006689 /* find the end of the value, respecting quotes */
Willy Tarreauab813a42018-09-10 18:41:28 +02006690 next = http_find_cookie_value_end(val_beg, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006691
6692 /* make val_end point to the first white space or delimitor after the value */
6693 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006694 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02006695 val_end--;
6696 } else {
6697 /* <equal> points to next comma, semi-colon or EOL */
6698 val_beg = val_end = next = equal;
6699 }
6700
6701 if (next < hdr_end) {
6702 /* Set-Cookie2 supports multiple cookies, and <next> points to
6703 * a colon or semi-colon before the end. So skip all attr-value
6704 * pairs and look for the next comma. For Set-Cookie, since
6705 * commas are permitted in values, skip to the end.
6706 */
6707 if (is_cookie2)
Willy Tarreauab813a42018-09-10 18:41:28 +02006708 next = http_find_hdr_value_end(next, hdr_end);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006709 else
6710 next = hdr_end;
6711 }
6712
6713 /* Now everything is as on the diagram above */
6714
6715 /* Ignore cookies with no equal sign */
6716 if (equal == val_end)
6717 continue;
6718
6719 /* If there are spaces around the equal sign, we need to
6720 * strip them otherwise we'll get trouble for cookie captures,
6721 * or even for rewrites. Since this happens extremely rarely,
6722 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006723 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006724 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6725 int stripped_before = 0;
6726 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006727
Willy Tarreau24581ba2010-08-31 22:39:35 +02006728 if (att_end != equal) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006729 stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006730 equal += stripped_before;
6731 val_beg += stripped_before;
6732 }
6733
6734 if (val_beg > equal + 1) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006735 stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006736 val_beg += stripped_after;
6737 stripped_before += stripped_after;
6738 }
6739
6740 val_end += stripped_before;
6741 next += stripped_before;
6742 hdr_end += stripped_before;
6743 hdr_next += stripped_before;
6744 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006745 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006746 }
6747
6748 /* First, let's see if we want to capture this cookie. We check
6749 * that we don't already have a server side cookie, because we
6750 * can only capture one. Also as an optimisation, we ignore
6751 * cookies shorter than the declared name.
6752 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006753 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006754 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006755 (val_end - att_beg >= sess->fe->capture_namelen) &&
6756 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006757 int log_len = val_end - att_beg;
Willy Tarreaubafbe012017-11-24 17:34:44 +01006758 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006759 ha_alert("HTTP logging : out of memory.\n");
Willy Tarreaua15645d2007-03-18 16:22:39 +01006760 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006761 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006762 if (log_len > sess->fe->capture_len)
6763 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01006764 memcpy(txn->srv_cookie, att_beg, log_len);
6765 txn->srv_cookie[log_len] = 0;
6766 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006767 }
6768
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006769 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006770 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006771 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006772 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6773 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006774 /* assume passive cookie by default */
6775 txn->flags &= ~TX_SCK_MASK;
6776 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006777
6778 /* If the cookie is in insert mode on a known server, we'll delete
6779 * this occurrence because we'll insert another one later.
6780 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006781 * a direct access.
6782 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006783 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006784 /* The "preserve" flag was set, we don't want to touch the
6785 * server's cookie.
6786 */
6787 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006788 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006789 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006790 /* this cookie must be deleted */
6791 if (*prev == ':' && next == hdr_end) {
6792 /* whole header */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006793 delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006794 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6795 txn->hdr_idx.used--;
6796 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006797 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006798 hdr_next += delta;
6799 http_msg_move_end(&txn->rsp, delta);
6800 /* note: while both invalid now, <next> and <hdr_end>
6801 * are still equal, so the for() will stop as expected.
6802 */
6803 } else {
6804 /* just remove the value */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006805 int delta = del_hdr_value(&res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006806 next = prev;
6807 hdr_end += delta;
6808 hdr_next += delta;
6809 cur_hdr->len += delta;
6810 http_msg_move_end(&txn->rsp, delta);
6811 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006812 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006813 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006814 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006815 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006816 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006817 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006818 * with this server since we know it.
6819 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006820 delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006821 next += delta;
6822 hdr_end += delta;
6823 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006824 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006825 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006826
Willy Tarreauf1348312010-10-07 15:54:11 +02006827 txn->flags &= ~TX_SCK_MASK;
6828 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006829 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006830 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006831 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006832 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006833 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02006834 delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006835 next += delta;
6836 hdr_end += delta;
6837 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006838 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006839 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006840
Willy Tarreau827aee92011-03-10 16:55:02 +01006841 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006842 txn->flags &= ~TX_SCK_MASK;
6843 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006844 }
6845 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006846 /* that's done for this cookie, check the next one on the same
6847 * line when next != hdr_end (only if is_cookie2).
6848 */
6849 }
6850 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006851 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006852 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006853}
6854
6855
Willy Tarreaua15645d2007-03-18 16:22:39 +01006856/*
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006857 * Parses the Cache-Control and Pragma request header fields to determine if
6858 * the request may be served from the cache and/or if it is cacheable. Updates
6859 * s->txn->flags.
6860 */
6861void check_request_for_cacheability(struct stream *s, struct channel *chn)
6862{
6863 struct http_txn *txn = s->txn;
6864 char *p1, *p2;
6865 char *cur_ptr, *cur_end, *cur_next;
6866 int pragma_found;
6867 int cc_found;
6868 int cur_idx;
6869
Christopher Faulet25a02f62018-10-24 12:00:25 +02006870 if (IS_HTX_STRM(s))
6871 return htx_check_request_for_cacheability(s, chn);
6872
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006873 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
6874 return; /* nothing more to do here */
6875
6876 cur_idx = 0;
6877 pragma_found = cc_found = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006878 cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006879
6880 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6881 struct hdr_idx_elem *cur_hdr;
6882 int val;
6883
6884 cur_hdr = &txn->hdr_idx.v[cur_idx];
6885 cur_ptr = cur_next;
6886 cur_end = cur_ptr + cur_hdr->len;
6887 cur_next = cur_end + cur_hdr->cr + 1;
6888
6889 /* We have one full header between cur_ptr and cur_end, and the
6890 * next header starts at cur_next.
6891 */
6892
6893 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6894 if (val) {
6895 if ((cur_end - (cur_ptr + val) >= 8) &&
6896 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6897 pragma_found = 1;
6898 continue;
6899 }
6900 }
6901
William Lallemand8a16fe02018-05-22 11:04:33 +02006902 /* Don't use the cache and don't try to store if we found the
6903 * Authorization header */
6904 val = http_header_match2(cur_ptr, cur_end, "Authorization", 13);
6905 if (val) {
6906 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6907 txn->flags |= TX_CACHE_IGNORE;
6908 continue;
6909 }
6910
Willy Tarreau0ad8e0d2017-12-22 15:03:36 +01006911 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6912 if (!val)
6913 continue;
6914
6915 /* OK, right now we know we have a cache-control header at cur_ptr */
6916 cc_found = 1;
6917 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
6918
6919 if (p1 >= cur_end) /* no more info */
6920 continue;
6921
6922 /* p1 is at the beginning of the value */
6923 p2 = p1;
6924 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
6925 p2++;
6926
6927 /* we have a complete value between p1 and p2. We don't check the
6928 * values after max-age, max-stale nor min-fresh, we simply don't
6929 * use the cache when they're specified.
6930 */
6931 if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) ||
6932 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
6933 ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) ||
6934 ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) {
6935 txn->flags |= TX_CACHE_IGNORE;
6936 continue;
6937 }
6938
6939 if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) {
6940 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6941 continue;
6942 }
6943 }
6944
6945 /* RFC7234#5.4:
6946 * When the Cache-Control header field is also present and
6947 * understood in a request, Pragma is ignored.
6948 * When the Cache-Control header field is not present in a
6949 * request, caches MUST consider the no-cache request
6950 * pragma-directive as having the same effect as if
6951 * "Cache-Control: no-cache" were present.
6952 */
6953 if (!cc_found && pragma_found)
6954 txn->flags |= TX_CACHE_IGNORE;
6955}
6956
6957/*
Willy Tarreaud3900cc2017-12-22 15:35:11 +01006958 * Check if response is cacheable or not. Updates s->txn->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006959 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006960void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006961{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006962 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006963 char *p1, *p2;
6964
6965 char *cur_ptr, *cur_end, *cur_next;
6966 int cur_idx;
6967
Christopher Faulet25a02f62018-10-24 12:00:25 +02006968
6969 if (IS_HTX_STRM(s))
6970 return htx_check_response_for_cacheability(s, rtr);
6971
Willy Tarreau12b32f22017-12-21 16:08:09 +01006972 if (txn->status < 200) {
6973 /* do not try to cache interim responses! */
6974 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006975 return;
Willy Tarreau12b32f22017-12-21 16:08:09 +01006976 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006977
6978 /* Iterate through the headers.
6979 * we start with the start line.
6980 */
6981 cur_idx = 0;
Willy Tarreauf37954d2018-06-15 18:31:02 +02006982 cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006983
6984 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6985 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006986 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006987
6988 cur_hdr = &txn->hdr_idx.v[cur_idx];
6989 cur_ptr = cur_next;
6990 cur_end = cur_ptr + cur_hdr->len;
6991 cur_next = cur_end + cur_hdr->cr + 1;
6992
6993 /* We have one full header between cur_ptr and cur_end, and the
Willy Tarreaud3900cc2017-12-22 15:35:11 +01006994 * next header starts at cur_next.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006995 */
6996
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006997 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6998 if (val) {
6999 if ((cur_end - (cur_ptr + val) >= 8) &&
7000 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7001 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7002 return;
7003 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007004 }
7005
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007006 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7007 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007008 continue;
7009
7010 /* OK, right now we know we have a cache-control header at cur_ptr */
7011
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007012 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007013
7014 if (p1 >= cur_end) /* no more info */
7015 continue;
7016
7017 /* p1 is at the beginning of the value */
7018 p2 = p1;
7019
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007020 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007021 p2++;
7022
7023 /* we have a complete value between p1 and p2 */
7024 if (p2 < cur_end && *p2 == '=') {
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007025 if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7026 ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
7027 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7028 continue;
7029 }
7030
Willy Tarreaua15645d2007-03-18 16:22:39 +01007031 /* we have something of the form no-cache="set-cookie" */
7032 if ((cur_end - p1 >= 21) &&
7033 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7034 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007035 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007036 continue;
7037 }
7038
7039 /* OK, so we know that either p2 points to the end of string or to a comma */
7040 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007041 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaud3900cc2017-12-22 15:35:11 +01007042 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007043 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007044 return;
7045 }
7046
7047 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007048 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007049 continue;
7050 }
7051 }
7052}
7053
Willy Tarreau58f10d72006-12-04 02:26:12 +01007054
Willy Tarreaub2513902006-12-17 14:52:38 +01007055/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007056 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007057 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007058 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007059 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007060 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007061 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007062 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007063 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007064int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007065{
7066 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007067 struct http_msg *msg = &txn->req;
Willy Tarreauf37954d2018-06-15 18:31:02 +02007068 const char *uri = ci_head(msg->chn)+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007069
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007070 if (!uri_auth)
7071 return 0;
7072
Cyril Bonté70be45d2010-10-12 00:14:35 +02007073 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007074 return 0;
7075
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007076 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007077 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007078 return 0;
7079
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007080 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007081 return 0;
7082
Willy Tarreaub2513902006-12-17 14:52:38 +01007083 return 1;
7084}
7085
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007086/* Append the description of what is present in error snapshot <es> into <out>.
7087 * The description must be small enough to always fit in a trash. The output
7088 * buffer may be the trash so the trash must not be used inside this function.
7089 */
7090void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es)
7091{
Christopher Fauleted26fb82018-11-29 22:53:30 +01007092 chunk_appendf(out,
Willy Tarreau7ccdd8d2018-09-07 14:01:39 +02007093 " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n"
7094 " HTTP msg state %s(%d), msg flags 0x%08x\n"
7095 " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n",
7096 es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags,
7097 h1_msg_state_str(es->ctx.http.state), es->ctx.http.state,
7098 es->ctx.http.m_flags, es->ctx.http.m_clen,
7099 es->ctx.http.m_blen, es->ctx.http.b_flags);
7100}
7101
Willy Tarreau4076a152009-04-02 15:18:36 +02007102/*
7103 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007104 * By default it tries to report the error position as msg->err_pos. However if
7105 * this one is not set, it will then report msg->next, which is the last known
7106 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007107 * displays buffers as a contiguous area starting at buf->p. The direction is
7108 * determined thanks to the channel's flags.
Willy Tarreau4076a152009-04-02 15:18:36 +02007109 */
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007110void http_capture_bad_message(struct proxy *proxy, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007111 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007112 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007113{
Willy Tarreauef3ca732018-09-07 15:47:35 +02007114 union error_snapshot_ctx ctx;
7115 long ofs;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007116
Willy Tarreauef3ca732018-09-07 15:47:35 +02007117 /* http-specific part now */
7118 ctx.http.sid = s->uniq_id;
7119 ctx.http.state = state;
7120 ctx.http.b_flags = msg->chn->flags;
7121 ctx.http.s_flags = s->flags;
7122 ctx.http.t_flags = s->txn->flags;
7123 ctx.http.m_flags = msg->flags;
7124 ctx.http.m_clen = msg->chunk_len;
7125 ctx.http.m_blen = msg->body_len;
Willy Tarreau7480f322018-09-06 19:41:22 +02007126
Willy Tarreauef3ca732018-09-07 15:47:35 +02007127 ofs = msg->chn->total - ci_data(msg->chn);
7128 if (ofs < 0)
7129 ofs = 0;
Willy Tarreau0b5b4802018-09-07 13:49:44 +02007130
Willy Tarreaufd9419d2018-09-07 18:01:03 +02007131 proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP),
Willy Tarreauef3ca732018-09-07 15:47:35 +02007132 other_end, s->target,
7133 strm_sess(s), &msg->chn->buf,
7134 ofs, co_data(msg->chn),
7135 (msg->err_pos >= 0) ? msg->err_pos : msg->next,
7136 &ctx, http_show_error_snapshot);
Willy Tarreau4076a152009-04-02 15:18:36 +02007137}
Willy Tarreaub2513902006-12-17 14:52:38 +01007138
Willy Tarreaubaaee002006-06-26 02:48:02 +02007139/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007140 * Print a debug line with a header. Always stop at the first CR or LF char,
7141 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7142 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007143 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007144void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007145{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007146 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007147 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007148
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007149 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007150 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007151 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02007152 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 +02007153
7154 for (max = 0; start + max < end; max++)
7155 if (start[max] == '\r' || start[max] == '\n')
7156 break;
7157
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007158 UBOUND(max, trash.size - trash.data - 3);
7159 trash.data += strlcpy2(trash.area + trash.data, start, max + 1);
7160 trash.area[trash.data++] = '\n';
7161 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007162}
7163
Willy Tarreaueee5b512015-04-03 23:46:31 +02007164
7165/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7166 * The hdr_idx is allocated as well. In case of allocation failure, everything
7167 * allocated is freed and NULL is returned. Otherwise the new transaction is
7168 * assigned to the stream and returned.
7169 */
7170struct http_txn *http_alloc_txn(struct stream *s)
7171{
7172 struct http_txn *txn = s->txn;
7173
7174 if (txn)
7175 return txn;
7176
Willy Tarreaubafbe012017-11-24 17:34:44 +01007177 txn = pool_alloc(pool_head_http_txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007178 if (!txn)
7179 return txn;
7180
7181 txn->hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007182 txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007183 if (!txn->hdr_idx.v) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01007184 pool_free(pool_head_http_txn, txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +02007185 return NULL;
7186 }
7187
7188 s->txn = txn;
7189 return txn;
7190}
7191
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007192void http_txn_reset_req(struct http_txn *txn)
7193{
7194 txn->req.flags = 0;
7195 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7196 txn->req.next = 0;
7197 txn->req.chunk_len = 0LL;
7198 txn->req.body_len = 0LL;
7199 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7200}
7201
7202void http_txn_reset_res(struct http_txn *txn)
7203{
7204 txn->rsp.flags = 0;
7205 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7206 txn->rsp.next = 0;
7207 txn->rsp.chunk_len = 0LL;
7208 txn->rsp.body_len = 0LL;
7209 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7210}
7211
Willy Tarreau0937bc42009-12-22 15:03:09 +01007212/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007213 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007214 * the required fields are properly allocated and that we only need to (re)init
7215 * them. This should be used before processing any new request.
7216 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007217void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007218{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007219 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007220 struct proxy *fe = strm_fe(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02007221 struct conn_stream *cs = objt_cs(s->si[0].end);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007222
Christopher Fauletf2824e62018-10-01 12:12:37 +02007223 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
7224 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
7225 : 0);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007226 txn->status = -1;
Willy Tarreauc9036c02019-01-11 19:38:25 +01007227 *(unsigned int *)txn->cache_hash = 0;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007228
Willy Tarreauf64d1412010-10-07 20:06:11 +02007229 txn->cookie_first_date = 0;
7230 txn->cookie_last_date = 0;
7231
Willy Tarreaueee5b512015-04-03 23:46:31 +02007232 txn->srv_cookie = NULL;
7233 txn->cli_cookie = NULL;
7234 txn->uri = NULL;
7235
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007236 http_txn_reset_req(txn);
7237 http_txn_reset_res(txn);
7238
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007239 txn->req.chn = &s->req;
7240 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007241
7242 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007243
7244 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7245 if (fe->options2 & PR_O2_REQBUG_OK)
7246 txn->req.err_pos = -1; /* let buggy requests pass */
7247
Willy Tarreau0937bc42009-12-22 15:03:09 +01007248 if (txn->hdr_idx.v)
7249 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007250
7251 vars_init(&s->vars_txn, SCOPE_TXN);
7252 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007253}
7254
7255/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007256void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007257{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007258 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007259 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007260
7261 /* these ones will have been dynamically allocated */
Willy Tarreaubafbe012017-11-24 17:34:44 +01007262 pool_free(pool_head_requri, txn->uri);
7263 pool_free(pool_head_capture, txn->cli_cookie);
7264 pool_free(pool_head_capture, txn->srv_cookie);
7265 pool_free(pool_head_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007266
William Lallemanda73203e2012-03-12 12:48:57 +01007267 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007268 txn->uri = NULL;
7269 txn->srv_cookie = NULL;
7270 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007271
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007272 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007273 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007274 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007275 pool_free(h->pool, s->req_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007276 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007277 }
7278
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007279 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007280 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007281 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaubafbe012017-11-24 17:34:44 +01007282 pool_free(h->pool, s->res_cap[h->index]);
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007283 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007284 }
7285
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01007286 if (!LIST_ISEMPTY(&s->vars_txn.head))
7287 vars_prune(&s->vars_txn, s->sess, s);
7288 if (!LIST_ISEMPTY(&s->vars_reqres.head))
7289 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007290}
7291
7292/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007293void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007294{
7295 http_end_txn(s);
7296 http_init_txn(s);
7297
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007298 /* reinitialise the current rule list pointer to NULL. We are sure that
7299 * any rulelist match the NULL pointer.
7300 */
7301 s->current_rule_list = NULL;
7302
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007303 s->be = strm_fe(s);
7304 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007305 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007306 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007307 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007308 /* re-init store persistence */
7309 s->store_count = 0;
Olivier Houcharda798bf52019-03-08 18:52:00 +01007310 s->uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007311
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007312 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007313
Willy Tarreau739cfba2010-01-25 23:11:14 +01007314 /* We must trim any excess data from the response buffer, because we
7315 * may have blocked an invalid response from a server that we don't
Joseph Herlant5ba80252018-11-15 09:25:36 -08007316 * want to accidently forward once we disable the analysers, nor do
Willy Tarreau739cfba2010-01-25 23:11:14 +01007317 * we want those data to come along with next response. A typical
7318 * example of such data would be from a buggy server responding to
7319 * a HEAD with some data, or sending more than the advertised
7320 * content-length.
7321 */
Willy Tarreauf37954d2018-06-15 18:31:02 +02007322 if (unlikely(ci_data(&s->res)))
Willy Tarreauc9fa0482018-07-10 17:43:27 +02007323 b_set_data(&s->res.buf, co_data(&s->res));
Willy Tarreau739cfba2010-01-25 23:11:14 +01007324
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007325 /* Now we can realign the response buffer */
Willy Tarreaud5b343b2018-06-06 06:42:46 +02007326 c_realign_if_empty(&s->res);
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007327
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007328 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007329 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007330
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007331 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007332 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007333
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007334 s->req.rex = TICK_ETERNITY;
7335 s->req.wex = TICK_ETERNITY;
7336 s->req.analyse_exp = TICK_ETERNITY;
7337 s->res.rex = TICK_ETERNITY;
7338 s->res.wex = TICK_ETERNITY;
7339 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01007340 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007341}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007342
Willy Tarreau79e57332018-10-02 16:01:16 +02007343/* This function executes one of the set-{method,path,query,uri} actions. It
7344 * takes the string from the variable 'replace' with length 'len', then modifies
7345 * the relevant part of the request line accordingly. Then it updates various
7346 * pointers to the next elements which were moved, and the total buffer length.
7347 * It finds the action to be performed in p[2], previously filled by function
7348 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
7349 * error, though this can be revisited when this code is finally exploited.
7350 *
7351 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
7352 * query string and 3 to replace uri.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007353 *
Willy Tarreau79e57332018-10-02 16:01:16 +02007354 * In query string case, the mark question '?' must be set at the start of the
7355 * string by the caller, event if the replacement query string is empty.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007356 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007357int http_replace_req_line(int action, const char *replace, int len,
7358 struct proxy *px, struct stream *s)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007359{
Willy Tarreau79e57332018-10-02 16:01:16 +02007360 struct http_txn *txn = s->txn;
7361 char *cur_ptr, *cur_end;
7362 int offset = 0;
7363 int delta;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007364
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007365 if (IS_HTX_STRM(s))
7366 return htx_req_replace_stline(action, replace, len, px, s);
7367
Willy Tarreau79e57332018-10-02 16:01:16 +02007368 switch (action) {
7369 case 0: // method
7370 cur_ptr = ci_head(&s->req);
7371 cur_end = cur_ptr + txn->req.sl.rq.m_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007372
Willy Tarreau79e57332018-10-02 16:01:16 +02007373 /* adjust req line offsets and lengths */
7374 delta = len - offset - (cur_end - cur_ptr);
7375 txn->req.sl.rq.m_l += delta;
7376 txn->req.sl.rq.u += delta;
7377 txn->req.sl.rq.v += delta;
7378 break;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007379
Willy Tarreau79e57332018-10-02 16:01:16 +02007380 case 1: // path
7381 cur_ptr = http_txn_get_path(txn);
7382 if (!cur_ptr)
7383 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007384
Willy Tarreau79e57332018-10-02 16:01:16 +02007385 cur_end = cur_ptr;
7386 while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
7387 cur_end++;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007388
Willy Tarreau79e57332018-10-02 16:01:16 +02007389 /* adjust req line offsets and lengths */
7390 delta = len - offset - (cur_end - cur_ptr);
7391 txn->req.sl.rq.u_l += delta;
7392 txn->req.sl.rq.v += delta;
7393 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007394
Willy Tarreau79e57332018-10-02 16:01:16 +02007395 case 2: // query
7396 offset = 1;
7397 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7398 cur_end = cur_ptr + txn->req.sl.rq.u_l;
7399 while (cur_ptr < cur_end && *cur_ptr != '?')
7400 cur_ptr++;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007401
Willy Tarreau79e57332018-10-02 16:01:16 +02007402 /* skip the question mark or indicate that we must insert it
7403 * (but only if the format string is not empty then).
7404 */
7405 if (cur_ptr < cur_end)
7406 cur_ptr++;
7407 else if (len > 1)
7408 offset = 0;
Willy Tarreauaae75e32013-03-29 12:31:49 +01007409
Willy Tarreau79e57332018-10-02 16:01:16 +02007410 /* adjust req line offsets and lengths */
7411 delta = len - offset - (cur_end - cur_ptr);
7412 txn->req.sl.rq.u_l += delta;
7413 txn->req.sl.rq.v += delta;
7414 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007415
Willy Tarreau79e57332018-10-02 16:01:16 +02007416 case 3: // uri
7417 cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u;
7418 cur_end = cur_ptr + txn->req.sl.rq.u_l;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007419
Willy Tarreau79e57332018-10-02 16:01:16 +02007420 /* adjust req line offsets and lengths */
7421 delta = len - offset - (cur_end - cur_ptr);
7422 txn->req.sl.rq.u_l += delta;
7423 txn->req.sl.rq.v += delta;
7424 break;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007425
Willy Tarreau79e57332018-10-02 16:01:16 +02007426 default:
7427 return -1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007428 }
7429
Willy Tarreau79e57332018-10-02 16:01:16 +02007430 /* commit changes and adjust end of message */
7431 delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
7432 txn->req.sl.rq.l += delta;
7433 txn->hdr_idx.v[0].len += delta;
7434 http_msg_move_end(&txn->req, delta);
7435 return 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02007436}
7437
Willy Tarreau79e57332018-10-02 16:01:16 +02007438/* This function replace the HTTP status code and the associated message. The
7439 * variable <status> contains the new status code. This function never fails.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007440 */
Willy Tarreau79e57332018-10-02 16:01:16 +02007441void http_set_status(unsigned int status, const char *reason, struct stream *s)
Willy Tarreau8797c062007-05-07 00:55:35 +02007442{
Willy Tarreau79e57332018-10-02 16:01:16 +02007443 struct http_txn *txn = s->txn;
7444 char *cur_ptr, *cur_end;
7445 int delta;
7446 char *res;
7447 int c_l;
7448 const char *msg = reason;
7449 int msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007450
Christopher Faulet8d8ac192018-10-24 11:27:39 +02007451 if (IS_HTX_STRM(s))
7452 return htx_res_set_status(status, reason, s);
7453
Willy Tarreau79e57332018-10-02 16:01:16 +02007454 chunk_reset(&trash);
Willy Tarreau8797c062007-05-07 00:55:35 +02007455
Willy Tarreau79e57332018-10-02 16:01:16 +02007456 res = ultoa_o(status, trash.area, trash.size);
7457 c_l = res - trash.area;
Willy Tarreau8797c062007-05-07 00:55:35 +02007458
Willy Tarreau79e57332018-10-02 16:01:16 +02007459 trash.area[c_l] = ' ';
7460 trash.data = c_l + 1;
Willy Tarreau8797c062007-05-07 00:55:35 +02007461
Willy Tarreau79e57332018-10-02 16:01:16 +02007462 /* Do we have a custom reason format string? */
7463 if (msg == NULL)
7464 msg = http_get_reason(status);
7465 msg_len = strlen(msg);
7466 strncpy(&trash.area[trash.data], msg, trash.size - trash.data);
7467 trash.data += msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007468
Willy Tarreau79e57332018-10-02 16:01:16 +02007469 cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c;
7470 cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
Willy Tarreauc11416f2007-06-17 16:58:38 +02007471
Willy Tarreau79e57332018-10-02 16:01:16 +02007472 /* commit changes and adjust message */
7473 delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area,
7474 trash.data);
Willy Tarreauf26b2522012-12-14 08:33:14 +01007475
Willy Tarreau79e57332018-10-02 16:01:16 +02007476 /* adjust res line offsets and lengths */
7477 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
7478 txn->rsp.sl.st.c_l = c_l;
7479 txn->rsp.sl.st.r_l = msg_len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007480
Willy Tarreau79e57332018-10-02 16:01:16 +02007481 delta = trash.data - (cur_end - cur_ptr);
7482 txn->rsp.sl.st.l += delta;
7483 txn->hdr_idx.v[0].len += delta;
7484 http_msg_move_end(&txn->rsp, delta);
Willy Tarreau8797c062007-05-07 00:55:35 +02007485}
7486
Willy Tarreau58f10d72006-12-04 02:26:12 +01007487/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02007488 * Local variables:
7489 * c-indent-level: 8
7490 * c-basic-offset: 8
7491 * End:
7492 */