blob: 7cbec5885a4d62fa22254eca01c1e8f29e61492d [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
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
Willy Tarreaudcc048a2020-06-04 19:11:43 +020013#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020014#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020016#include <haproxy/backend.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/base64.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020018#include <haproxy/capture-t.h>
Amaury Denoyelle03517732021-05-07 14:25:01 +020019#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020020#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020021#include <haproxy/check.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020022#include <haproxy/connection.h>
Christopher Faulet8da67aa2022-03-29 17:53:09 +020023#include <haproxy/conn_stream.h>
24#include <haproxy/cs_utils.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020025#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020026#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020027#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020028#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020029#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020030#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020031#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020032#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020033#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020034#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020035#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020036#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/stream.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020038#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020039#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020040#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020041
Christopher Faulete0768eb2018-10-03 16:38:02 +020042
Christopher Fauleteea8fc72019-11-05 16:18:10 +010043#define TRACE_SOURCE &trace_strm
44
Christopher Faulet377c5a52018-10-24 21:21:30 +020045extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020046
Willy Tarreauff882702021-04-10 17:23:00 +020047struct pool_head *pool_head_requri __read_mostly = NULL;
48struct pool_head *pool_head_capture __read_mostly = NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +020049
50
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020051static void http_end_request(struct stream *s);
52static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020053
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020054static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
55static int http_del_hdr_value(char *start, char *end, char **from, char *next);
56static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020057static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
58static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
Christopher Faulet0f226952018-10-22 09:29:56 +020059
Christopher Fauletd4150ad2021-10-13 15:35:55 +020060static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s);
61static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020062
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020063static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
64static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020065
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020066static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
67static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020068
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020069static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
70static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010071
Christopher Faulete0768eb2018-10-03 16:38:02 +020072/* This stream analyser waits for a complete HTTP request. It returns 1 if the
73 * processing can continue on next analysers, or zero if it either needs more
74 * data or wants to immediately abort the request (eg: timeout, error, ...). It
75 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
76 * when it has nothing left to do, and may remove any analyser when it wants to
77 * abort.
78 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020079int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020080{
Christopher Faulet9768c262018-10-22 09:34:31 +020081
Christopher Faulete0768eb2018-10-03 16:38:02 +020082 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020083 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020084 *
Christopher Faulet9768c262018-10-22 09:34:31 +020085 * Once the start line and all headers are received, we may perform a
86 * capture of the error (if any), and we will set a few fields. We also
87 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020088 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020089 struct session *sess = s->sess;
90 struct http_txn *txn = s->txn;
91 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020092 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010093 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020094
Christopher Fauleteea8fc72019-11-05 16:18:10 +010095 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020096
Christopher Fauletda46a0d2021-01-21 17:32:58 +010097 if (unlikely(!IS_HTX_STRM(s))) {
98 /* It is only possible when a TCP stream is upgrade to HTTP.
99 * There is a transition period during which there is no
100 * data. The stream is still in raw mode and SF_IGNORE flag is
101 * still set. When this happens, the new mux is responsible to
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500102 * handle all errors. Thus we may leave immediately.
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100103 */
104 BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req));
Christopher Faulet9768c262018-10-22 09:34:31 +0200105
Christopher Faulet97b3a612021-03-15 17:10:12 +0100106 /* Don't connect for now */
107 channel_dont_connect(req);
108
109 /* A SHUTR at this stage means we are performing a "destructive"
110 * HTTP upgrade (TCP>H2). In this case, we can leave.
111 */
112 if (req->flags & CF_SHUTR) {
113 s->logs.logwait = 0;
114 s->logs.level = 0;
115 channel_abort(&s->req);
116 channel_abort(&s->res);
117 req->analysers &= AN_REQ_FLT_END;
118 req->analyse_exp = TICK_ETERNITY;
119 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
120 return 1;
121 }
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100122 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
123 return 0;
124 }
125
126 htx = htxbuf(&req->buf);
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200127
Willy Tarreau4236f032019-03-05 10:43:32 +0100128 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200129 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100130 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200131 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100132 if (htx->flags & HTX_FL_PARSING_ERROR) {
133 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200134 goto return_bad_req;
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100135 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200136 else
137 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100138 }
139
Christopher Faulete0768eb2018-10-03 16:38:02 +0200140 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200141 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200142
Christopher Faulet9768c262018-10-22 09:34:31 +0200143 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200144 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200145 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200146
Christopher Faulet9768c262018-10-22 09:34:31 +0200147 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200148 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200149
Christopher Faulet29f17582019-05-23 11:03:26 +0200150 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200151 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100152
Christopher Faulet9768c262018-10-22 09:34:31 +0200153 /* 0: we might have to print this header in debug mode */
154 if (unlikely((global.mode & MODE_DEBUG) &&
155 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
156 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200157
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200158 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200159
Christopher Fauleta3f15502019-05-13 15:27:23 +0200160 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200161 struct htx_blk *blk = htx_get_blk(htx, pos);
162 enum htx_blk_type type = htx_get_blk_type(blk);
163
164 if (type == HTX_BLK_EOH)
165 break;
166 if (type != HTX_BLK_HDR)
167 continue;
168
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200169 http_debug_hdr("clihdr", s,
170 htx_get_blk_name(htx, blk),
171 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200172 }
173 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200174
175 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100176 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200177 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100178 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100179 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200180 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100181 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100182 if (sl->flags & HTX_SL_F_CLEN)
183 msg->flags |= HTTP_MSGF_CNT_LEN;
184 else if (sl->flags & HTX_SL_F_CHNK)
185 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100186 if (sl->flags & HTX_SL_F_BODYLESS)
187 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100188 if (sl->flags & HTX_SL_F_CONN_UPG)
189 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200190
191 /* we can make use of server redirect on GET and HEAD */
192 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
193 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100194 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200195 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200196 goto return_bad_req;
197 }
198
199 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100200 * 2: check if the URI matches the monitor_uri. We have to do this for
201 * every request which gets in, because the monitor-uri is defined by
202 * the frontend. If the monitor-uri starts with a '/', the matching is
203 * done against the request's path. Otherwise, the request's uri is
204 * used. It is a workaround to let HTTP/2 health-checks work as
205 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200206 */
Tim Duesterhus4b1fcaa2022-03-05 00:52:40 +0100207 if (unlikely(isttest(sess->fe->monitor_uri))) {
208 const struct ist monitor_uri = sess->fe->monitor_uri;
Amaury Denoyellec453f952021-07-06 11:40:12 +0200209 struct http_uri_parser parser = http_uri_parser_init(htx_sl_req_uri(sl));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200210
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200211 if ((istptr(monitor_uri)[0] == '/' &&
Amaury Denoyellec453f952021-07-06 11:40:12 +0200212 isteq(http_parse_path(&parser), monitor_uri)) ||
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200213 isteq(htx_sl_req_uri(sl), monitor_uri)) {
214 /*
215 * We have found the monitor URI
216 */
217 struct acl_cond *cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200218
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200219 s->flags |= SF_MONITOR;
220 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200221
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200222 /* Check if we want to fail this monitor request or not */
223 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
224 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200225
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200226 ret = acl_pass(ret);
227 if (cond->pol == ACL_COND_UNLESS)
228 ret = !ret;
229
230 if (ret) {
231 /* we fail this request, let's return 503 service unavail */
232 txn->status = 503;
233 if (!(s->flags & SF_ERR_MASK))
234 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
235 goto return_prx_cond;
236 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200237 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200238
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200239 /* nothing to fail, let's reply normally */
240 txn->status = 200;
241 if (!(s->flags & SF_ERR_MASK))
242 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
243 goto return_prx_cond;
244 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200245 }
246
247 /*
248 * 3: Maybe we have to copy the original REQURI for the logs ?
249 * Note: we cannot log anymore if the request has been
250 * classified as invalid.
251 */
252 if (unlikely(s->logs.logwait & LW_REQ)) {
253 /* we have a complete HTTP request that we must log */
254 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200255 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200256
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200257 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200258 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200259
260 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
261 s->do_log(s);
262 } else {
263 ha_alert("HTTP logging : out of memory.\n");
264 }
265 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200266
Christopher Faulete0768eb2018-10-03 16:38:02 +0200267 /* if the frontend has "option http-use-proxy-header", we'll check if
268 * we have what looks like a proxied connection instead of a connection,
269 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
270 * Note that this is *not* RFC-compliant, however browsers and proxies
271 * happen to do that despite being non-standard :-(
272 * We consider that a request not beginning with either '/' or '*' is
273 * a proxied connection, which covers both "scheme://location" and
274 * CONNECT ip:port.
275 */
276 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100277 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200278 txn->flags |= TX_USE_PX_CONN;
279
Christopher Faulete0768eb2018-10-03 16:38:02 +0200280 /* 5: we may need to capture headers */
281 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200282 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200283
Christopher Faulete0768eb2018-10-03 16:38:02 +0200284 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200285 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200286 req->analysers |= AN_REQ_HTTP_BODY;
287
288 /*
289 * RFC7234#4:
290 * A cache MUST write through requests with methods
291 * that are unsafe (Section 4.2.1 of [RFC7231]) to
292 * the origin server; i.e., a cache is not allowed
293 * to generate a reply to such a request before
294 * having forwarded the request and having received
295 * a corresponding response.
296 *
297 * RFC7231#4.2.1:
298 * Of the request methods defined by this
299 * specification, the GET, HEAD, OPTIONS, and TRACE
300 * methods are defined to be safe.
301 */
302 if (likely(txn->meth == HTTP_METH_GET ||
303 txn->meth == HTTP_METH_HEAD ||
304 txn->meth == HTTP_METH_OPTIONS ||
305 txn->meth == HTTP_METH_TRACE))
306 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
307
308 /* end of job, return OK */
309 req->analysers &= ~an_bit;
310 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200311
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100312 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200313 return 1;
314
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200315 return_int_err:
316 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200317 if (!(s->flags & SF_ERR_MASK))
318 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200319 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100320 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200321 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200322 goto return_prx_cond;
323
Christopher Faulete0768eb2018-10-03 16:38:02 +0200324 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200325 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200326 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100327 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200328 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200329 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200330
331 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200332 http_reply_and_close(s, txn->status, http_error_message(s));
333
Christopher Faulete0768eb2018-10-03 16:38:02 +0200334 if (!(s->flags & SF_ERR_MASK))
335 s->flags |= SF_ERR_PRXCOND;
336 if (!(s->flags & SF_FINST_MASK))
337 s->flags |= SF_FINST_R;
338
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100339 DBG_TRACE_DEVEL("leaving on error",
340 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200341 return 0;
342}
343
344
345/* This stream analyser runs all HTTP request processing which is common to
346 * frontends and backends, which means blocking ACLs, filters, connection-close,
347 * reqadd, stats and redirects. This is performed for the designated proxy.
348 * It returns 1 if the processing can continue on next analysers, or zero if it
349 * either needs more data or wants to immediately abort the request (eg: deny,
350 * error, ...).
351 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200352int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200353{
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200354 struct list *def_rules, *rules;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200355 struct session *sess = s->sess;
356 struct http_txn *txn = s->txn;
357 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200358 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200359 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200360 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200361 struct connection *conn = objt_conn(sess->origin);
362
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100363 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200364
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100365 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200366
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200367 /* just in case we have some per-backend tracking. Only called the first
368 * execution of the analyser. */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200369 if (!s->current_rule && !s->current_rule_list)
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200370 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200371
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200372 def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL);
373 rules = &px->http_req_rules;
374
Christopher Faulete0768eb2018-10-03 16:38:02 +0200375 /* evaluate http-request rules */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200376 if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) {
377 verdict = http_req_get_intercept_rule(px, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200378
379 switch (verdict) {
380 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
381 goto return_prx_yield;
382
383 case HTTP_RULE_RES_CONT:
384 case HTTP_RULE_RES_STOP: /* nothing to do */
385 break;
386
387 case HTTP_RULE_RES_DENY: /* deny or tarpit */
388 if (txn->flags & TX_CLTARPIT)
389 goto tarpit;
390 goto deny;
391
392 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
393 goto return_prx_cond;
394
395 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
396 goto done;
397
398 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
399 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100400
401 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
402 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200403 }
404 }
405
406 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100407 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200408 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200409
Christopher Fauletff2759f2018-10-24 11:13:16 +0200410 ctx.blk = NULL;
411 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
412 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100413 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200414 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200415 }
416
417 /* OK at this stage, we know that the request was accepted according to
418 * the http-request rules, we can check for the stats. Note that the
419 * URI is detected *before* the req* rules in order not to be affected
420 * by a possible reqrep, while they are processed *after* so that a
421 * reqdeny can still block them. This clearly needs to change in 1.6!
422 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200423 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200424 s->target = &http_stats_applet.obj_type;
Christopher Faulet1336ccf2022-04-12 18:15:16 +0200425 if (unlikely(!cs_applet_create(s->csb, objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200426 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200427 if (!(s->flags & SF_ERR_MASK))
428 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100429 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200430 }
431
432 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200433 http_handle_stats(s, req);
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200434 verdict = http_req_get_intercept_rule(px, NULL, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200435 /* not all actions implemented: deny, allow, auth */
436
437 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
438 goto deny;
439
440 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
441 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100442
443 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
444 goto return_bad_req;
445
446 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
447 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200448 }
449
Christopher Faulet2571bc62019-03-01 11:44:26 +0100450 /* Proceed with the applets now. */
451 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200452 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200453 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200454
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200455 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100456 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100457
Christopher Faulete0768eb2018-10-03 16:38:02 +0200458 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
459 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
460 if (!(s->flags & SF_FINST_MASK))
461 s->flags |= SF_FINST_R;
462
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100463 if (HAS_FILTERS(s))
464 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
465
Christopher Faulete0768eb2018-10-03 16:38:02 +0200466 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
467 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
468 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
469 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100470
471 req->flags |= CF_SEND_DONTWAIT;
472 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200473 goto done;
474 }
475
476 /* check whether we have some ACLs set to redirect this request */
477 list_for_each_entry(rule, &px->redirect_rules, list) {
478 if (rule->cond) {
479 int ret;
480
481 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
482 ret = acl_pass(ret);
483 if (rule->cond->pol == ACL_COND_UNLESS)
484 ret = !ret;
485 if (!ret)
486 continue;
487 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200488 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100489 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200490 goto done;
491 }
492
493 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
494 * If this happens, then the data will not come immediately, so we must
495 * send all what we have without waiting. Note that due to the small gain
496 * in waiting for the body of the request, it's easier to simply put the
497 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
498 * itself once used.
499 */
500 req->flags |= CF_SEND_DONTWAIT;
501
502 done: /* done with this analyser, continue with next ones that the calling
503 * points will have set, if any.
504 */
505 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500506 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200507 req->analysers &= ~an_bit;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200508 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100509 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200510 return 1;
511
512 tarpit:
513 /* Allow cookie logging
514 */
515 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200516 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200517
518 /* When a connection is tarpitted, we use the tarpit timeout,
519 * which may be the same as the connect timeout if unspecified.
520 * If unset, then set it to zero because we really want it to
521 * eventually expire. We build the tarpit as an analyser.
522 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100523 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200524
525 /* wipe the request out so that we can drop the connection early
526 * if the client closes first.
527 */
528 channel_dont_connect(req);
529
Christopher Faulete0768eb2018-10-03 16:38:02 +0200530 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
531 req->analysers |= AN_REQ_HTTP_TARPIT;
532 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
533 if (!req->analyse_exp)
534 req->analyse_exp = tick_add(now_ms, 0);
535 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200536 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100537 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200538 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100539 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200540 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200541 goto done_without_exp;
542
543 deny: /* this request was blocked (denied) */
544
545 /* Allow cookie logging
546 */
547 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200548 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200549
Christopher Faulete0768eb2018-10-03 16:38:02 +0200550 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200551 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200552 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100553 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200554 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100555 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200556 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100557 goto return_prx_err;
558
559 return_int_err:
560 txn->status = 500;
561 if (!(s->flags & SF_ERR_MASK))
562 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200563 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100564 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200565 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100566 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200567 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100568 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200569
570 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200571 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200572 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100573 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200574 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100575 /* fall through */
576
577 return_prx_err:
578 http_reply_and_close(s, txn->status, http_error_message(s));
579 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200580
581 return_prx_cond:
582 if (!(s->flags & SF_ERR_MASK))
583 s->flags |= SF_ERR_PRXCOND;
584 if (!(s->flags & SF_FINST_MASK))
585 s->flags |= SF_FINST_R;
586
587 req->analysers &= AN_REQ_FLT_END;
588 req->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200589 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100590 DBG_TRACE_DEVEL("leaving on error",
591 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200592 return 0;
593
594 return_prx_yield:
595 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100596 DBG_TRACE_DEVEL("waiting for more data",
597 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200598 return 0;
599}
600
601/* This function performs all the processing enabled for the current request.
602 * It returns 1 if the processing can continue on next analysers, or zero if it
603 * needs more data, encounters an error, or wants to immediately abort the
604 * request. It relies on buffers flags, and updates s->req.analysers.
605 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200606int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200607{
608 struct session *sess = s->sess;
609 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200610 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200611 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
612
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200613 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200614
615 /*
616 * Right now, we know that we have processed the entire headers
617 * and that unwanted requests have been filtered out. We can do
618 * whatever we want with the remaining request. Also, now we
619 * may have separate values for ->fe, ->be.
620 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100621 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200622
623 /*
Christopher Faulete0768eb2018-10-03 16:38:02 +0200624 * 7: Now we can work with the cookies.
625 * Note that doing so might move headers in the request, but
626 * the fields will stay coherent and the URI will not move.
627 * This should only be performed in the backend.
628 */
629 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200630 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200631
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100632 /* 8: Generate unique ID if a "unique-id-format" is defined.
633 *
634 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
635 * fetches only available in the HTTP request processing stage.
636 */
637 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100638 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200639
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100640 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100641 if (!(s->flags & SF_ERR_MASK))
642 s->flags |= SF_ERR_RESOURCE;
643 goto return_int_err;
644 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200645
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100646 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100647 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100648 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100649 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200650 }
651
652 /*
653 * 9: add X-Forwarded-For if either the frontend or the backend
654 * asks for it.
655 */
656 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200657 const struct sockaddr_storage *src = cs_src(s->csf);
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200658 struct http_hdr_ctx ctx = { .blk = NULL };
Tim Duesterhusb50ab842022-03-05 00:52:41 +0100659 struct ist hdr = isttest(s->be->fwdfor_hdr_name) ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200660
Christopher Faulete0768eb2018-10-03 16:38:02 +0200661 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200662 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200663 /* The header is set to be added only if none is present
664 * and we found it, so don't do anything.
665 */
666 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200667 else if (src && src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200668 /* Add an X-Forwarded-For header unless the source IP is
669 * in the 'except' network range.
670 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200671 if (ipcmp2net(src, &sess->fe->except_xff_net) &&
672 ipcmp2net(src, &s->be->except_xff_net)) {
673 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200674
675 /* Note: we rely on the backend to get the header name to be used for
676 * x-forwarded-for, because the header is really meant for the backends.
677 * However, if the backend did not specify any option, we have to rely
678 * on the frontend's header name.
679 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200680 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
681 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100682 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200683 }
684 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200685 else if (src && src->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100686 /* Add an X-Forwarded-For header unless the source IP is
687 * in the 'except' network range.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200688 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200689 if (ipcmp2net(src, &sess->fe->except_xff_net) &&
690 ipcmp2net(src, &s->be->except_xff_net)) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100691 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200692
Christopher Faulet5d1def62021-02-26 09:19:15 +0100693 inet_ntop(AF_INET6,
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200694 (const void *)&((struct sockaddr_in6 *)(src))->sin6_addr,
Christopher Faulet5d1def62021-02-26 09:19:15 +0100695 pn, sizeof(pn));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200696
Christopher Faulet5d1def62021-02-26 09:19:15 +0100697 /* Note: we rely on the backend to get the header name to be used for
698 * x-forwarded-for, because the header is really meant for the backends.
699 * However, if the backend did not specify any option, we have to rely
700 * on the frontend's header name.
701 */
702 chunk_printf(&trash, "%s", pn);
703 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
704 goto return_int_err;
705 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200706 }
707 }
708
709 /*
710 * 10: add X-Original-To if either the frontend or the backend
711 * asks for it.
712 */
713 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200714 const struct sockaddr_storage *dst = cs_dst(s->csf);
Tim Duesterhuse502c3e2022-03-05 00:52:42 +0100715 struct ist hdr = isttest(s->be->orgto_hdr_name) ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200716
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200717 if (dst && dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200718 /* Add an X-Original-To header unless the destination IP is
719 * in the 'except' network range.
720 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200721 if (ipcmp2net(dst, &sess->fe->except_xot_net) &&
722 ipcmp2net(dst, &s->be->except_xot_net)) {
723 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200724
725 /* Note: we rely on the backend to get the header name to be used for
726 * x-original-to, because the header is really meant for the backends.
727 * However, if the backend did not specify any option, we have to rely
728 * on the frontend's header name.
729 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200730 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
731 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100732 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200733 }
734 }
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200735 else if (dst && dst->ss_family == AF_INET6) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100736 /* Add an X-Original-To header unless the source IP is
737 * in the 'except' network range.
738 */
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200739 if (ipcmp2net(dst, &sess->fe->except_xot_net) &&
740 ipcmp2net(dst, &s->be->except_xot_net)) {
Christopher Faulet5d1def62021-02-26 09:19:15 +0100741 char pn[INET6_ADDRSTRLEN];
742
743 inet_ntop(AF_INET6,
Christopher Faulet8a104ba2021-10-25 07:41:30 +0200744 (const void *)&((struct sockaddr_in6 *)dst)->sin6_addr,
Christopher Faulet5d1def62021-02-26 09:19:15 +0100745 pn, sizeof(pn));
746
747 /* Note: we rely on the backend to get the header name to be used for
748 * x-forwarded-for, because the header is really meant for the backends.
749 * However, if the backend did not specify any option, we have to rely
750 * on the frontend's header name.
751 */
752 chunk_printf(&trash, "%s", pn);
753 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
754 goto return_int_err;
755 }
756 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200757 }
758
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100759 /* Filter the request headers if there are filters attached to the
760 * stream.
761 */
762 if (HAS_FILTERS(s))
763 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
764
Christopher Faulete0768eb2018-10-03 16:38:02 +0200765 /* If we have no server assigned yet and we're balancing on url_param
766 * with a POST request, we may be interested in checking the body for
767 * that parameter. This will be done in another analyser.
768 */
769 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100770 s->txn->meth == HTTP_METH_POST &&
771 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200772 channel_dont_connect(req);
773 req->analysers |= AN_REQ_HTTP_BODY;
774 }
775
776 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
777 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100778
Christopher Faulete0768eb2018-10-03 16:38:02 +0200779 /* We expect some data from the client. Unless we know for sure
780 * we already have a full request, we have to re-enable quick-ack
781 * in case we previously disabled it, otherwise we might cause
782 * the client to delay further data.
783 */
William Lallemand36119de2021-03-08 15:26:48 +0100784 if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100785 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200786
787 /*************************************************************
788 * OK, that's finished for the headers. We have done what we *
789 * could. Let's switch to the DATA state. *
790 ************************************************************/
791 req->analyse_exp = TICK_ETERNITY;
792 req->analysers &= ~an_bit;
793
794 s->logs.tv_request = now;
795 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100796 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200797 return 1;
798
Christopher Fauletb8a53712019-12-16 11:29:38 +0100799 return_int_err:
800 txn->status = 500;
801 if (!(s->flags & SF_ERR_MASK))
802 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200803 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100804 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200805 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100806 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200807 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100808
Christopher Fauletb8a53712019-12-16 11:29:38 +0100809 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200810
811 if (!(s->flags & SF_ERR_MASK))
812 s->flags |= SF_ERR_PRXCOND;
813 if (!(s->flags & SF_FINST_MASK))
814 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100815
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100816 DBG_TRACE_DEVEL("leaving on error",
817 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200818 return 0;
819}
820
821/* This function is an analyser which processes the HTTP tarpit. It always
822 * returns zero, at the beginning because it prevents any other processing
823 * from occurring, and at the end because it terminates the request.
824 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200825int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200826{
827 struct http_txn *txn = s->txn;
828
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100829 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200830 /* This connection is being tarpitted. The CLIENT side has
831 * already set the connect expiration date to the right
832 * timeout. We just have to check that the client is still
833 * there and that the timeout has not expired.
834 */
835 channel_dont_connect(req);
836 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100837 !tick_is_expired(req->analyse_exp, now_ms)) {
Christopher Fauletb0c87f12021-10-29 14:37:07 +0200838 /* Be sure to drain all data from the request channel */
839 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100840 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
841 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200842 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100843 }
844
Christopher Faulete0768eb2018-10-03 16:38:02 +0200845
846 /* We will set the queue timer to the time spent, just for
847 * logging purposes. We fake a 500 server error, so that the
848 * attacker will not suspect his connection has been tarpitted.
849 * It will not cause trouble to the logs because we can exclude
850 * the tarpitted connections by filtering on the 'PT' status flags.
851 */
852 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
853
Christopher Faulet8dfeccf2020-05-15 14:16:29 +0200854 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +0200855
Christopher Faulete0768eb2018-10-03 16:38:02 +0200856 if (!(s->flags & SF_ERR_MASK))
857 s->flags |= SF_ERR_PRXCOND;
858 if (!(s->flags & SF_FINST_MASK))
859 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100860
861 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200862 return 0;
863}
864
865/* This function is an analyser which waits for the HTTP request body. It waits
866 * for either the buffer to be full, or the full advertised contents to have
867 * reached the buffer. It must only be called after the standard HTTP request
868 * processing has occurred, because it expects the request to be parsed and will
869 * look for the Expect header. It may send a 100-Continue interim response. It
870 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
871 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
872 * needs to read more data, or 1 once it has completed its analysis.
873 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200874int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200875{
876 struct session *sess = s->sess;
877 struct http_txn *txn = s->txn;
878 struct http_msg *msg = &s->txn->req;
879
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100880 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200881
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200882
Christopher Faulet021a8e42021-03-29 10:46:38 +0200883 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
884 case HTTP_RULE_RES_CONT:
885 goto http_end;
886 case HTTP_RULE_RES_YIELD:
887 goto missing_data_or_waiting;
888 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100889 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200890 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200891 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200892 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100893 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200894 default:
895 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200896 }
897
898 http_end:
899 /* The situation will not evolve, so let's give up on the analysis. */
900 s->logs.tv_request = now; /* update the request timer to reflect full request */
901 req->analysers &= ~an_bit;
902 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100903 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200904 return 1;
905
Christopher Faulet021a8e42021-03-29 10:46:38 +0200906 missing_data_or_waiting:
907 channel_dont_connect(req);
908 DBG_TRACE_DEVEL("waiting for more data",
909 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
910 return 0;
911
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200912 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200913 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200914 if (!(s->flags & SF_ERR_MASK))
915 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200916 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100917 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200918 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100919 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200920 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200921 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200922
Christopher Faulete0768eb2018-10-03 16:38:02 +0200923 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200924 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200925 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100926 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200927 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100928 /* fall through */
929
Christopher Faulet021a8e42021-03-29 10:46:38 +0200930 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100931 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200932 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200933
Christopher Faulet021a8e42021-03-29 10:46:38 +0200934 return_prx_cond:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200935 if (!(s->flags & SF_ERR_MASK))
936 s->flags |= SF_ERR_PRXCOND;
937 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100938 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200939
Christopher Faulete0768eb2018-10-03 16:38:02 +0200940 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100941 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100942 DBG_TRACE_DEVEL("leaving on error",
943 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200944 return 0;
945}
946
947/* This function is an analyser which forwards request body (including chunk
948 * sizes if any). It is called as soon as we must forward, even if we forward
949 * zero byte. The only situation where it must not be called is when we're in
950 * tunnel mode and we want to forward till the close. It's used both to forward
951 * remaining data and to resync after end of body. It expects the msg_state to
952 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
953 * read more data, or 1 once we can go on with next request or end the stream.
954 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
955 * bytes of pending data + the headers if not already done.
956 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200957int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200958{
959 struct session *sess = s->sess;
960 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200961 struct http_msg *msg = &txn->req;
962 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100963 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100964 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200965
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100966 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200967
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100968 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200969
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200970 if (htx->flags & HTX_FL_PARSING_ERROR)
971 goto return_bad_req;
972 if (htx->flags & HTX_FL_PROCESSING_ERROR)
973 goto return_int_err;
974
Christopher Faulete0768eb2018-10-03 16:38:02 +0200975 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
976 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
977 /* Output closed while we were sending data. We must abort and
978 * wake the other side up.
Christopher Fauletf506d962021-04-27 10:56:28 +0200979 *
980 * If we have finished to send the request and the response is
981 * still in progress, don't catch write error on the request
982 * side if it is in fact a read error on the server side.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200983 */
Christopher Fauletf506d962021-04-27 10:56:28 +0200984 if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers)
985 return 0;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200986
Olivier Houchard29cac3c2019-07-12 15:48:58 +0200987 /* Don't abort yet if we had L7 retries activated and it
988 * was a write error, we may recover.
989 */
990 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +0200991 (txn->flags & TX_L7_RETRY)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100992 DBG_TRACE_DEVEL("leaving on L7 retry",
993 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +0200994 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100995 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200996 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200997 http_end_request(s);
998 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100999 DBG_TRACE_DEVEL("leaving on error",
1000 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001001 return 1;
1002 }
1003
1004 /* Note that we don't have to send 100-continue back because we don't
1005 * need the data to complete our job, and it's up to the server to
1006 * decide whether to return 100, 417 or anything else in return of
1007 * an "Expect: 100-continue" header.
1008 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001009 if (msg->msg_state == HTTP_MSG_BODY)
1010 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001011
Christopher Faulete0768eb2018-10-03 16:38:02 +02001012 /* in most states, we should abort in case of early close */
1013 channel_auto_close(req);
1014
1015 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001016 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001017 if (req->flags & CF_EOI)
1018 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001019 }
1020 else {
1021 /* We can't process the buffer's contents yet */
1022 req->flags |= CF_WAKE_WRITE;
1023 goto missing_data_or_waiting;
1024 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001025 }
1026
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001027 if (msg->msg_state >= HTTP_MSG_ENDING)
1028 goto ending;
1029
1030 if (txn->meth == HTTP_METH_CONNECT) {
1031 msg->msg_state = HTTP_MSG_ENDING;
1032 goto ending;
1033 }
1034
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001035 /* Forward input data. We get it by removing all outgoing data not
1036 * forwarded yet from HTX data size. If there are some data filters, we
1037 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001038 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001039 if (HAS_REQ_DATA_FILTERS(s)) {
1040 ret = flt_http_payload(s, msg, htx->data);
1041 if (ret < 0)
1042 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001043 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001044 }
1045 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001046 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001047 if (msg->flags & HTTP_MSGF_XFER_LEN)
1048 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001049 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001050
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001051 if (htx->data != co_data(req))
1052 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001053
Christopher Faulet9768c262018-10-22 09:34:31 +02001054 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001055 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1056 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001057 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001058 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02001059 goto missing_data_or_waiting;
1060
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001061 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001062
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001063 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001064 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1065
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001066 /* other states, ENDING...TUNNEL */
1067 if (msg->msg_state >= HTTP_MSG_DONE)
1068 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001069
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001070 if (HAS_REQ_DATA_FILTERS(s)) {
1071 ret = flt_http_end(s, msg);
1072 if (ret <= 0) {
1073 if (!ret)
1074 goto missing_data_or_waiting;
1075 goto return_bad_req;
1076 }
1077 }
1078
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001079 if (txn->meth == HTTP_METH_CONNECT)
1080 msg->msg_state = HTTP_MSG_TUNNEL;
1081 else {
1082 msg->msg_state = HTTP_MSG_DONE;
1083 req->to_forward = 0;
1084 }
1085
1086 done:
1087 /* we don't want to forward closes on DONE except in tunnel mode. */
1088 if (!(txn->flags & TX_CON_WANT_TUN))
1089 channel_dont_close(req);
1090
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001091 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001092 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001093 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001094 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1095 if (req->flags & CF_SHUTW) {
1096 /* request errors are most likely due to the
1097 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001098 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001099 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001100 goto return_bad_req;
1101 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001102 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001103 return 1;
1104 }
1105
1106 /* If "option abortonclose" is set on the backend, we want to monitor
1107 * the client's connection and forward any shutdown notification to the
1108 * server, which will decide whether to close or to go on processing the
1109 * request. We only do that in tunnel mode, and not in other modes since
1110 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001111 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001112 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001113 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulet8abe7122022-03-30 15:10:18 +02001114 s->csb->flags |= CS_FL_NOLINGER;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001115 channel_auto_close(req);
1116 }
1117 else if (s->txn->meth == HTTP_METH_POST) {
1118 /* POST requests may require to read extra CRLF sent by broken
1119 * browsers and which could cause an RST to be sent upon close
1120 * on some systems (eg: Linux). */
1121 channel_auto_read(req);
1122 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001123 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1124 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001125 return 0;
1126
1127 missing_data_or_waiting:
1128 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001129 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001130 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001131
1132 waiting:
1133 /* waiting for the last bits to leave the buffer */
1134 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001135 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001136
1137 /* When TE: chunked is used, we need to get there again to parse remaining
1138 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1139 * And when content-length is used, we never want to let the possible
1140 * shutdown be forwarded to the other side, as the state machine will
1141 * take care of it once the client responds. It's also important to
1142 * prevent TIME_WAITs from accumulating on the backend side, and for
1143 * HTTP/2 where the last frame comes with a shutdown.
1144 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001145 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001146 channel_dont_close(req);
1147
1148 /* We know that more data are expected, but we couldn't send more that
1149 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1150 * system knows it must not set a PUSH on this first part. Interactive
1151 * modes are already handled by the stream sock layer. We must not do
1152 * this in content-length mode because it could present the MSG_MORE
1153 * flag with the last block of forwarded data, which would cause an
1154 * additional delay to be observed by the receiver.
1155 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001156 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001157 req->flags |= CF_EXPECT_MORE;
1158
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001159 DBG_TRACE_DEVEL("waiting for more data to forward",
1160 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001161 return 0;
1162
Christopher Faulet93e02d82019-03-08 14:18:50 +01001163 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001164 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1165 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001166 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001167 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001168 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001169 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001170 if (!(s->flags & SF_ERR_MASK))
1171 s->flags |= SF_ERR_CLICL;
1172 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001173 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001174
1175 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001176 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1177 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001178 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001179 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001180 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001181 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001182 if (!(s->flags & SF_ERR_MASK))
1183 s->flags |= SF_ERR_SRVCL;
1184 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001185 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001186
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001187 return_int_err:
1188 if (!(s->flags & SF_ERR_MASK))
1189 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001190 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1191 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001192 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001193 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001194 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001195 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001196 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001197 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001198
Christopher Faulet93e02d82019-03-08 14:18:50 +01001199 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001200 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001201 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001202 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001203 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001204 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001205
Christopher Fauletb8a53712019-12-16 11:29:38 +01001206 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001207 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001208 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001209 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001210 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001211 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001212 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001213 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001214 if (!(s->flags & SF_ERR_MASK))
1215 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001216 if (!(s->flags & SF_FINST_MASK))
1217 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001218 DBG_TRACE_DEVEL("leaving on error ",
1219 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001220 return 0;
1221}
1222
Christopher Faulet77397992022-04-04 11:07:08 +02001223/* Reset the stream and the backend conn_stream to a situation suitable for attemption connection */
Olivier Houcharda254a372019-04-05 15:30:12 +02001224/* Returns 0 if we can attempt to retry, -1 otherwise */
Christopher Faulet77397992022-04-04 11:07:08 +02001225static __inline int do_l7_retry(struct stream *s, struct conn_stream *cs)
Olivier Houcharda254a372019-04-05 15:30:12 +02001226{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001227 struct channel *req, *res;
1228 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001229
Christopher Faulet731c8e62022-03-29 16:08:44 +02001230 s->conn_retries++;
1231 if (s->conn_retries >= s->be->conn_retries)
Christopher Faulet552601d2021-05-26 10:31:06 +02001232 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001233
Christopher Faulete763c8c2021-05-05 18:23:59 +02001234 if (objt_server(s->target)) {
1235 if (s->flags & SF_CURR_SESS) {
1236 s->flags &= ~SF_CURR_SESS;
1237 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1238 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001239 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001240 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001241 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001242
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001243 req = &s->req;
1244 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001245 /* Remove any write error from the request, and read error from the response */
1246 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1247 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
Christopher Faulet7bf46bb2022-01-04 10:56:03 +01001248 res->analysers &= AN_RES_FLT_END;
Christopher Faulet77397992022-04-04 11:07:08 +02001249 cs->endp->flags &= ~CS_EP_RXBLK_SHUT;
Christopher Faulet50264b42022-03-30 19:39:30 +02001250 s->conn_err_type = STRM_ET_NONE;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001251 s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK);
1252 s->conn_exp = TICK_ETERNITY;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001253 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001254 res->rex = TICK_ETERNITY;
1255 res->to_forward = 0;
1256 res->analyse_exp = TICK_ETERNITY;
1257 res->total = 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001258
1259 if (cs_reset_endp(s->csb) < 0) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001260 if (!(s->flags & SF_ERR_MASK))
1261 s->flags |= SF_ERR_INTERNAL;
1262 return -1;
1263 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001264
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001265 b_free(&req->buf);
1266 /* Swap the L7 buffer with the channel buffer */
1267 /* We know we stored the co_data as b_data, so get it there */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001268 co_data = b_data(&s->txn->l7_buffer);
1269 b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer));
1270 b_xfer(&req->buf, &s->txn->l7_buffer, b_data(&s->txn->l7_buffer));
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001271 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001272
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001273 DBG_TRACE_DEVEL("perform a L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, s->txn);
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001274
Olivier Houcharda254a372019-04-05 15:30:12 +02001275 b_reset(&res->buf);
1276 co_set_data(res, 0);
1277 return 0;
1278}
1279
Christopher Faulete0768eb2018-10-03 16:38:02 +02001280/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1281 * processing can continue on next analysers, or zero if it either needs more
1282 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1283 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1284 * when it has nothing left to do, and may remove any analyser when it wants to
1285 * abort.
1286 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001287int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001288{
Christopher Faulet9768c262018-10-22 09:34:31 +02001289 /*
1290 * We will analyze a complete HTTP response to check the its syntax.
1291 *
1292 * Once the start line and all headers are received, we may perform a
1293 * capture of the error (if any), and we will set a few fields. We also
1294 * logging and finally headers capture.
1295 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001296 struct session *sess = s->sess;
1297 struct http_txn *txn = s->txn;
1298 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001299 struct htx *htx;
Christopher Faulet61608322018-11-23 16:23:45 +01001300 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001301 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001302 int n;
1303
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001304 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001305
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001306 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001307
Willy Tarreau4236f032019-03-05 10:43:32 +01001308 /* Parsing errors are caught here */
1309 if (htx->flags & HTX_FL_PARSING_ERROR)
1310 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001311 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1312 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001313
Christopher Faulete0768eb2018-10-03 16:38:02 +02001314 /*
1315 * Now we quickly check if we have found a full valid response.
1316 * If not so, we check the FD and buffer states before leaving.
1317 * A full response is indicated by the fact that we have seen
1318 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1319 * responses are checked first.
1320 *
1321 * Depending on whether the client is still there or not, we
1322 * may send an error response back or not. Note that normally
1323 * we should only check for HTTP status there, and check I/O
1324 * errors somewhere else.
1325 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001326 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001327 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001328 /* 1: have we encountered a read error ? */
1329 if (rep->flags & CF_READ_ERROR) {
Christopher Faulet95a61e82021-12-22 14:22:03 +01001330 struct connection *conn = cs_conn(s->csb);
Olivier Houchard865d8392019-05-03 22:46:27 +02001331
Christopher Fauletd9769232021-05-26 12:15:37 +02001332 /* Perform a L7 retry because server refuses the early data. */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001333 if ((txn->flags & TX_L7_RETRY) &&
Christopher Fauletd9769232021-05-26 12:15:37 +02001334 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1335 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
Christopher Faulet77397992022-04-04 11:07:08 +02001336 do_l7_retry(s, s->csb) == 0) {
Christopher Fauletd9769232021-05-26 12:15:37 +02001337 DBG_TRACE_DEVEL("leaving on L7 retry",
1338 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1339 return 0;
1340 }
1341
Olivier Houchard6db16992019-05-17 15:40:49 +02001342 if (txn->flags & TX_NOT_FIRST)
1343 goto abort_keep_alive;
1344
Willy Tarreau4781b152021-04-06 13:53:36 +02001345 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001346 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001347 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001348 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001349 }
1350
Christopher Fauletd9769232021-05-26 12:15:37 +02001351 /* if the server refused the early data, just send a 425 */
1352 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001353 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001354 else {
1355 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001356 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001357 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001358
Christopher Faulet8abe7122022-03-30 15:10:18 +02001359 s->csb->flags |= CS_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001360 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001361
1362 if (!(s->flags & SF_ERR_MASK))
1363 s->flags |= SF_ERR_SRVCL;
1364 if (!(s->flags & SF_FINST_MASK))
1365 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001366 DBG_TRACE_DEVEL("leaving on error",
1367 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001368 return 0;
1369 }
1370
Christopher Faulet9768c262018-10-22 09:34:31 +02001371 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001372 else if (rep->flags & CF_READ_TIMEOUT) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001373 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001374 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Faulet77397992022-04-04 11:07:08 +02001375 if (co_data(rep) || do_l7_retry(s, s->csb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001376 DBG_TRACE_DEVEL("leaving on L7 retry",
1377 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001378 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001379 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001380 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001381 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001382 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001383 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001384 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001385 }
1386
Christopher Faulete0768eb2018-10-03 16:38:02 +02001387 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001388 stream_inc_http_fail_ctr(s);
Christopher Faulet8abe7122022-03-30 15:10:18 +02001389 s->csb->flags |= CS_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001390 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001391
1392 if (!(s->flags & SF_ERR_MASK))
1393 s->flags |= SF_ERR_SRVTO;
1394 if (!(s->flags & SF_FINST_MASK))
1395 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001396 DBG_TRACE_DEVEL("leaving on error",
1397 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001398 return 0;
1399 }
1400
Christopher Faulet9768c262018-10-22 09:34:31 +02001401 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001402 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001403 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1404 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001405 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001406 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001407 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001408 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001409
Christopher Faulete0768eb2018-10-03 16:38:02 +02001410 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001411 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001412
1413 if (!(s->flags & SF_ERR_MASK))
1414 s->flags |= SF_ERR_CLICL;
1415 if (!(s->flags & SF_FINST_MASK))
1416 s->flags |= SF_FINST_H;
1417
1418 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001419 DBG_TRACE_DEVEL("leaving on error",
1420 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001421 return 0;
1422 }
1423
Christopher Faulet9768c262018-10-22 09:34:31 +02001424 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001425 else if (rep->flags & CF_SHUTR) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001426 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001427 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Faulet77397992022-04-04 11:07:08 +02001428 if (co_data(rep) || do_l7_retry(s, s->csb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001429 DBG_TRACE_DEVEL("leaving on L7 retry",
1430 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001431 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001432 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001433 }
1434
Olivier Houchard6db16992019-05-17 15:40:49 +02001435 if (txn->flags & TX_NOT_FIRST)
1436 goto abort_keep_alive;
1437
Willy Tarreau4781b152021-04-06 13:53:36 +02001438 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001439 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001440 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001441 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001442 }
1443
Christopher Faulete0768eb2018-10-03 16:38:02 +02001444 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001445 stream_inc_http_fail_ctr(s);
Christopher Faulet8abe7122022-03-30 15:10:18 +02001446 s->csb->flags |= CS_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001447 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001448
1449 if (!(s->flags & SF_ERR_MASK))
1450 s->flags |= SF_ERR_SRVCL;
1451 if (!(s->flags & SF_FINST_MASK))
1452 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001453 DBG_TRACE_DEVEL("leaving on error",
1454 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001455 return 0;
1456 }
1457
Christopher Faulet9768c262018-10-22 09:34:31 +02001458 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001459 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001460 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001461 goto abort_keep_alive;
1462
Willy Tarreau4781b152021-04-06 13:53:36 +02001463 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001464 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001465 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001466 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001467
1468 if (!(s->flags & SF_ERR_MASK))
1469 s->flags |= SF_ERR_CLICL;
1470 if (!(s->flags & SF_FINST_MASK))
1471 s->flags |= SF_FINST_H;
1472
1473 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001474 DBG_TRACE_DEVEL("leaving on error",
1475 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001476 return 0;
1477 }
1478
1479 channel_dont_close(rep);
1480 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001481 DBG_TRACE_DEVEL("waiting for more data",
1482 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001483 return 0;
1484 }
1485
1486 /* More interesting part now : we know that we have a complete
1487 * response which at least looks like HTTP. We have an indicator
1488 * of each header's length, so we can parse them quickly.
1489 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001490 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001491 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001492
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001493 /* Perform a L7 retry because of the status code */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001494 if ((txn->flags & TX_L7_RETRY) &&
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001495 l7_status_match(s->be, sl->info.res.status) &&
Christopher Faulet77397992022-04-04 11:07:08 +02001496 do_l7_retry(s, s->csb) == 0) {
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001497 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1498 return 0;
1499 }
1500
1501 /* Now, L7 buffer is useless, it can be released */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001502 b_free(&txn->l7_buffer);
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001503
1504 msg->msg_state = HTTP_MSG_BODY;
1505
1506
Christopher Faulet9768c262018-10-22 09:34:31 +02001507 /* 0: we might have to print this header in debug mode */
1508 if (unlikely((global.mode & MODE_DEBUG) &&
1509 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1510 int32_t pos;
1511
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001512 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001513
Christopher Fauleta3f15502019-05-13 15:27:23 +02001514 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001515 struct htx_blk *blk = htx_get_blk(htx, pos);
1516 enum htx_blk_type type = htx_get_blk_type(blk);
1517
1518 if (type == HTX_BLK_EOH)
1519 break;
1520 if (type != HTX_BLK_HDR)
1521 continue;
1522
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001523 http_debug_hdr("srvhdr", s,
1524 htx_get_blk_name(htx, blk),
1525 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001526 }
1527 }
1528
Christopher Faulet03599112018-11-27 11:21:21 +01001529 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001530 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001531 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001532 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001533 if (sl->flags & HTX_SL_F_XFER_LEN) {
1534 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001535 if (sl->flags & HTX_SL_F_CLEN)
1536 msg->flags |= HTTP_MSGF_CNT_LEN;
1537 else if (sl->flags & HTX_SL_F_CHNK)
1538 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001539 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001540 if (sl->flags & HTX_SL_F_BODYLESS)
1541 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001542 if (sl->flags & HTX_SL_F_CONN_UPG)
1543 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001544
1545 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001546 if (n < 1 || n > 5)
1547 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001548
Christopher Faulete0768eb2018-10-03 16:38:02 +02001549 /* when the client triggers a 4xx from the server, it's most often due
1550 * to a missing object or permission. These events should be tracked
1551 * because if they happen often, it may indicate a brute force or a
1552 * vulnerability scan.
1553 */
1554 if (n == 4)
1555 stream_inc_http_err_ctr(s);
1556
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001557 if (n == 5 && txn->status != 501 && txn->status != 505)
1558 stream_inc_http_fail_ctr(s);
1559
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001560 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001561 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1562 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001563 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001564
Christopher Faulete0768eb2018-10-03 16:38:02 +02001565 /* Adjust server's health based on status code. Note: status codes 501
1566 * and 505 are triggered on demand by client request, so we must not
1567 * count them as server failures.
1568 */
1569 if (objt_server(s->target)) {
1570 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001571 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001572 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001573 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001574 }
1575
1576 /*
1577 * We may be facing a 100-continue response, or any other informational
1578 * 1xx response which is non-final, in which case this is not the right
1579 * response, and we're waiting for the next one. Let's allow this response
1580 * to go to the client and wait for the next one. There's an exception for
1581 * 101 which is used later in the code to switch protocols.
1582 */
1583 if (txn->status < 200 &&
1584 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001585 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001586 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001587 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001588 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001589 txn->status = 0;
1590 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001591 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001592 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001593 }
1594
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001595 /* A 101-switching-protocols must contains a Connection header with the
1596 * "upgrade" option and the request too. It means both are agree to
1597 * upgrade. It is not so strict because there is no test on the Upgrade
1598 * header content. But it is probably stronger enough for now.
1599 */
1600 if (txn->status == 101 &&
1601 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1602 goto return_bad_res;
1603
Christopher Faulete0768eb2018-10-03 16:38:02 +02001604 /*
1605 * 2: check for cacheability.
1606 */
1607
1608 switch (txn->status) {
1609 case 200:
1610 case 203:
1611 case 204:
1612 case 206:
1613 case 300:
1614 case 301:
1615 case 404:
1616 case 405:
1617 case 410:
1618 case 414:
1619 case 501:
1620 break;
1621 default:
1622 /* RFC7231#6.1:
1623 * Responses with status codes that are defined as
1624 * cacheable by default (e.g., 200, 203, 204, 206,
1625 * 300, 301, 404, 405, 410, 414, and 501 in this
1626 * specification) can be reused by a cache with
1627 * heuristic expiration unless otherwise indicated
1628 * by the method definition or explicit cache
1629 * controls [RFC7234]; all other status codes are
1630 * not cacheable by default.
1631 */
1632 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1633 break;
1634 }
1635
1636 /*
1637 * 3: we may need to capture headers
1638 */
1639 s->logs.logwait &= ~LW_RESP;
1640 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001641 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001642
Christopher Faulet9768c262018-10-22 09:34:31 +02001643 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001644 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001645 txn->status == 101)) {
1646 /* Either we've established an explicit tunnel, or we're
1647 * switching the protocol. In both cases, we're very unlikely
1648 * to understand the next protocols. We have to switch to tunnel
1649 * mode, so that we transfer the request and responses then let
1650 * this protocol pass unmodified. When we later implement specific
1651 * parsers for such protocols, we'll want to check the Upgrade
1652 * header which contains information about that protocol for
1653 * responses with status 101 (eg: see RFC2817 about TLS).
1654 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001655 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001656 }
1657
Christopher Faulet61608322018-11-23 16:23:45 +01001658 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1659 * 407 (Proxy-Authenticate) responses and set the connection to private
1660 */
Christopher Faulet95a61e82021-12-22 14:22:03 +01001661 srv_conn = cs_conn(s->csb);
Christopher Faulet61608322018-11-23 16:23:45 +01001662 if (srv_conn) {
1663 struct ist hdr;
1664 struct http_hdr_ctx ctx;
1665
1666 if (txn->status == 401)
1667 hdr = ist("WWW-Authenticate");
1668 else if (txn->status == 407)
1669 hdr = ist("Proxy-Authenticate");
1670 else
1671 goto end;
1672
1673 ctx.blk = NULL;
1674 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001675 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1676 * possibly followed by blanks and a base64 string, the connection
1677 * is private. Since it's a mess to deal with, we only check for
1678 * values starting with "NTLM" or "Nego". Note that often multiple
1679 * headers are sent by the server there.
1680 */
1681 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001682 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001683 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001684 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001685 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001686 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001687 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001688 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001689 }
Christopher Faulet61608322018-11-23 16:23:45 +01001690 }
1691 }
1692
1693 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001694 /* we want to have the response time before we start processing it */
1695 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1696
1697 /* end of job, return OK */
1698 rep->analysers &= ~an_bit;
1699 rep->analyse_exp = TICK_ETERNITY;
1700 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001701 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001702 return 1;
1703
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001704 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001705 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1706 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001707 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001708 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001709 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001710 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001711 txn->status = 500;
1712 if (!(s->flags & SF_ERR_MASK))
1713 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001714 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001715
1716 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001717 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001718 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001719 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001720 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001721 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001722 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001723 (txn->flags & TX_L7_RETRY) &&
Christopher Faulet77397992022-04-04 11:07:08 +02001724 do_l7_retry(s, s->csb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001725 DBG_TRACE_DEVEL("leaving on L7 retry",
1726 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001727 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001728 }
Christopher Faulet47365272018-10-31 17:40:50 +01001729 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001730 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001731 /* fall through */
1732
Christopher Fauletb8a53712019-12-16 11:29:38 +01001733 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001734 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001735
1736 if (!(s->flags & SF_ERR_MASK))
1737 s->flags |= SF_ERR_PRXCOND;
1738 if (!(s->flags & SF_FINST_MASK))
1739 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001740
Christopher Faulet8abe7122022-03-30 15:10:18 +02001741 s->csb->flags |= CS_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001742 DBG_TRACE_DEVEL("leaving on error",
1743 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001744 return 0;
1745
Christopher Faulete0768eb2018-10-03 16:38:02 +02001746 abort_keep_alive:
1747 /* A keep-alive request to the server failed on a network error.
1748 * The client is required to retry. We need to close without returning
1749 * any other information so that the client retries.
1750 */
1751 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001752 s->logs.logwait = 0;
1753 s->logs.level = 0;
1754 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001755 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001756 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1757 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001758 return 0;
1759}
1760
1761/* This function performs all the processing enabled for the current response.
1762 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1763 * and updates s->res.analysers. It might make sense to explode it into several
1764 * other functions. It works like process_request (see indications above).
1765 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001766int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001767{
1768 struct session *sess = s->sess;
1769 struct http_txn *txn = s->txn;
1770 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001771 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001772 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001773 enum rule_result ret = HTTP_RULE_RES_CONT;
1774
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001775 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1776 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001777
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001778 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001779
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001780 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001781
1782 /* The stats applet needs to adjust the Connection header but we don't
1783 * apply any filter there.
1784 */
1785 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1786 rep->analysers &= ~an_bit;
1787 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001788 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001789 }
1790
1791 /*
1792 * We will have to evaluate the filters.
1793 * As opposed to version 1.2, now they will be evaluated in the
1794 * filters order and not in the header order. This means that
1795 * each filter has to be validated among all headers.
1796 *
1797 * Filters are tried with ->be first, then with ->fe if it is
1798 * different from ->be.
1799 *
1800 * Maybe we are in resume condiion. In this case I choose the
1801 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001802 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001803 * the process with the first one.
1804 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001805 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001806 * pointer and the ->fe rule list. If it doesn't match, I initialize
1807 * the loop with the ->be.
1808 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001809 if (s->current_rule_list == &sess->fe->http_res_rules ||
1810 (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001811 cur_proxy = sess->fe;
1812 else
1813 cur_proxy = s->be;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001814
Christopher Faulete0768eb2018-10-03 16:38:02 +02001815 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001816 /* evaluate http-response rules */
Christopher Faulet46f46df2021-11-09 16:33:25 +01001817 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001818 struct list *def_rules, *rules;
1819
1820 def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL);
1821 rules = &cur_proxy->http_res_rules;
1822
1823 ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001824
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001825 switch (ret) {
1826 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1827 goto return_prx_yield;
1828
1829 case HTTP_RULE_RES_CONT:
1830 case HTTP_RULE_RES_STOP: /* nothing to do */
1831 break;
1832
1833 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1834 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001835
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001836 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1837 goto return_prx_cond;
1838
1839 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001840 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001841
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001842 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1843 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001844
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001845 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1846 goto return_int_err;
1847 }
1848
1849 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001850
Christopher Faulete0768eb2018-10-03 16:38:02 +02001851 /* check whether we're already working on the frontend */
1852 if (cur_proxy == sess->fe)
1853 break;
1854 cur_proxy = sess->fe;
1855 }
1856
Christopher Faulete0768eb2018-10-03 16:38:02 +02001857 /* OK that's all we can do for 1xx responses */
1858 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001859 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001860
1861 /*
1862 * Now check for a server cookie.
1863 */
1864 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001865 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001866
1867 /*
1868 * Check for cache-control or pragma headers if required.
1869 */
1870 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001871 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001872
1873 /*
1874 * Add server cookie in the response if needed
1875 */
1876 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1877 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1878 (!(s->flags & SF_DIRECT) ||
1879 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1880 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1881 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1882 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1883 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1884 !(s->flags & SF_IGNORE_PRST)) {
1885 /* the server is known, it's not the one the client requested, or the
1886 * cookie's last seen date needs to be refreshed. We have to
1887 * insert a set-cookie here, except if we want to insert only on POST
1888 * requests and this one isn't. Note that servers which don't have cookies
1889 * (eg: some backup servers) will return a full cookie removal request.
1890 */
Willy Tarreau88bc8002021-12-06 07:01:02 +00001891 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001892 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001893 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001894 s->be->cookie_name);
1895 }
1896 else {
Willy Tarreau88bc8002021-12-06 07:01:02 +00001897 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001898
1899 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1900 /* emit last_date, which is mandatory */
1901 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1902 s30tob64((date.tv_sec+3) >> 2,
1903 trash.area + trash.data);
1904 trash.data += 5;
1905
1906 if (s->be->cookie_maxlife) {
1907 /* emit first_date, which is either the original one or
1908 * the current date.
1909 */
1910 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1911 s30tob64(txn->cookie_first_date ?
1912 txn->cookie_first_date >> 2 :
1913 (date.tv_sec+3) >> 2,
1914 trash.area + trash.data);
1915 trash.data += 5;
1916 }
1917 }
1918 chunk_appendf(&trash, "; path=/");
1919 }
1920
1921 if (s->be->cookie_domain)
1922 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1923
1924 if (s->be->ck_opts & PR_CK_HTTPONLY)
1925 chunk_appendf(&trash, "; HttpOnly");
1926
1927 if (s->be->ck_opts & PR_CK_SECURE)
1928 chunk_appendf(&trash, "; Secure");
1929
Christopher Faulet2f533902020-01-21 11:06:48 +01001930 if (s->be->cookie_attrs)
1931 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1932
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001933 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001934 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001935
1936 txn->flags &= ~TX_SCK_MASK;
1937 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1938 /* the server did not change, only the date was updated */
1939 txn->flags |= TX_SCK_UPDATED;
1940 else
1941 txn->flags |= TX_SCK_INSERTED;
1942
1943 /* Here, we will tell an eventual cache on the client side that we don't
1944 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1945 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1946 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1947 */
1948 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1949
1950 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1951
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001952 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001953 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001954 }
1955 }
1956
1957 /*
1958 * Check if result will be cacheable with a cookie.
1959 * We'll block the response if security checks have caught
1960 * nasty things such as a cacheable cookie.
1961 */
1962 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1963 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1964 (s->be->options & PR_O_CHK_CACHE)) {
1965 /* we're in presence of a cacheable response containing
1966 * a set-cookie header. We'll block it as requested by
1967 * the 'checkcache' option, and send an alert.
1968 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001969 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001970 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02001971 send_log(s->be, LOG_ALERT,
1972 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001973 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001974 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001975 }
1976
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001977 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001978 /*
1979 * Evaluate after-response rules before forwarding the response. rules
1980 * from the backend are evaluated first, then one from the frontend if
1981 * it differs.
1982 */
1983 if (!http_eval_after_res_rules(s))
1984 goto return_int_err;
1985
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001986 /* Filter the response headers if there are filters attached to the
1987 * stream.
1988 */
1989 if (HAS_FILTERS(s))
1990 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1991
Christopher Faulete0768eb2018-10-03 16:38:02 +02001992 /* Always enter in the body analyzer */
1993 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1994 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1995
1996 /* if the user wants to log as soon as possible, without counting
1997 * bytes from the server, then this is the right moment. We have
1998 * to temporarily assign bytes_out to log what we currently have.
1999 */
2000 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2001 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002002 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002003 s->do_log(s);
2004 s->logs.bytes_out = 0;
2005 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002006
Christopher Fauletb8a53712019-12-16 11:29:38 +01002007 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002008 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002009 rep->analysers &= ~an_bit;
2010 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002011 s->current_rule = s->current_rule_list = NULL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002012 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002013
Christopher Fauletb8a53712019-12-16 11:29:38 +01002014 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02002015 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
2016 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01002017 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002018 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002019 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002020 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002021 goto return_prx_err;
2022
2023 return_int_err:
2024 txn->status = 500;
2025 if (!(s->flags & SF_ERR_MASK))
2026 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02002027 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2028 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02002029 if (sess->listener && sess->listener->counters)
2030 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002031 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002032 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002033 goto return_prx_err;
2034
2035 return_bad_res:
2036 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002037 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02002038 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002039 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002040 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01002041 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2042 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002043 /* fall through */
2044
2045 return_prx_err:
2046 http_reply_and_close(s, txn->status, http_error_message(s));
2047 /* fall through */
2048
2049 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002050 s->logs.t_data = -1; /* was not a valid response */
Christopher Faulet8abe7122022-03-30 15:10:18 +02002051 s->csb->flags |= CS_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002052
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002053 if (!(s->flags & SF_ERR_MASK))
2054 s->flags |= SF_ERR_PRXCOND;
2055 if (!(s->flags & SF_FINST_MASK))
2056 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002057
Christopher Faulete58c0002020-03-02 16:21:01 +01002058 rep->analysers &= AN_RES_FLT_END;
2059 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002060 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002061 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002062 DBG_TRACE_DEVEL("leaving on error",
2063 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002064 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002065
2066 return_prx_yield:
2067 channel_dont_close(rep);
2068 DBG_TRACE_DEVEL("waiting for more data",
2069 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2070 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002071}
2072
2073/* This function is an analyser which forwards response body (including chunk
2074 * sizes if any). It is called as soon as we must forward, even if we forward
2075 * zero byte. The only situation where it must not be called is when we're in
2076 * tunnel mode and we want to forward till the close. It's used both to forward
2077 * remaining data and to resync after end of body. It expects the msg_state to
2078 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2079 * read more data, or 1 once we can go on with next request or end the stream.
2080 *
2081 * It is capable of compressing response data both in content-length mode and
2082 * in chunked mode. The state machines follows different flows depending on
2083 * whether content-length and chunked modes are used, since there are no
2084 * trailers in content-length :
2085 *
2086 * chk-mode cl-mode
2087 * ,----- BODY -----.
2088 * / \
2089 * V size > 0 V chk-mode
2090 * .--> SIZE -------------> DATA -------------> CRLF
2091 * | | size == 0 | last byte |
2092 * | v final crlf v inspected |
2093 * | TRAILERS -----------> DONE |
2094 * | |
2095 * `----------------------------------------------'
2096 *
2097 * Compression only happens in the DATA state, and must be flushed in final
2098 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2099 * is performed at once on final states for all bytes parsed, or when leaving
2100 * on missing data.
2101 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002102int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002103{
2104 struct session *sess = s->sess;
2105 struct http_txn *txn = s->txn;
2106 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002107 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002108 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002109
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002110 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002111
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002112 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002113
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002114 if (htx->flags & HTX_FL_PARSING_ERROR)
2115 goto return_bad_res;
2116 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2117 goto return_int_err;
2118
Christopher Faulete0768eb2018-10-03 16:38:02 +02002119 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002120 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002121 /* Output closed while we were sending data. We must abort and
2122 * wake the other side up.
2123 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002124 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002125 http_end_response(s);
2126 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002127 DBG_TRACE_DEVEL("leaving on error",
2128 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002129 return 1;
2130 }
2131
Christopher Faulet9768c262018-10-22 09:34:31 +02002132 if (msg->msg_state == HTTP_MSG_BODY)
2133 msg->msg_state = HTTP_MSG_DATA;
2134
Christopher Faulete0768eb2018-10-03 16:38:02 +02002135 /* in most states, we should abort in case of early close */
2136 channel_auto_close(res);
2137
Christopher Faulete0768eb2018-10-03 16:38:02 +02002138 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002139 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002140 if (res->flags & CF_EOI)
2141 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002142 }
2143 else {
2144 /* We can't process the buffer's contents yet */
2145 res->flags |= CF_WAKE_WRITE;
2146 goto missing_data_or_waiting;
2147 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002148 }
2149
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002150 if (msg->msg_state >= HTTP_MSG_ENDING)
2151 goto ending;
2152
Christopher Fauletc75668e2020-12-07 18:10:32 +01002153 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002154 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2155 msg->msg_state = HTTP_MSG_ENDING;
2156 goto ending;
2157 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002158
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002159 /* Forward input data. We get it by removing all outgoing data not
2160 * forwarded yet from HTX data size. If there are some data filters, we
2161 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002162 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002163 if (HAS_RSP_DATA_FILTERS(s)) {
2164 ret = flt_http_payload(s, msg, htx->data);
2165 if (ret < 0)
2166 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002167 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002168 }
2169 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002170 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002171 if (msg->flags & HTTP_MSGF_XFER_LEN)
2172 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002173 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002174
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002175 if (htx->data != co_data(res))
2176 goto missing_data_or_waiting;
2177
2178 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2179 msg->msg_state = HTTP_MSG_ENDING;
2180 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002181 }
2182
Christopher Faulet9768c262018-10-22 09:34:31 +02002183 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002184 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2185 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002186 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002187 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002188 goto missing_data_or_waiting;
2189
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002190 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002191
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002192 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002193 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2194
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002195 /* other states, ENDING...TUNNEL */
2196 if (msg->msg_state >= HTTP_MSG_DONE)
2197 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002198
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002199 if (HAS_RSP_DATA_FILTERS(s)) {
2200 ret = flt_http_end(s, msg);
2201 if (ret <= 0) {
2202 if (!ret)
2203 goto missing_data_or_waiting;
2204 goto return_bad_res;
2205 }
2206 }
2207
Christopher Fauletc75668e2020-12-07 18:10:32 +01002208 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002209 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2210 msg->msg_state = HTTP_MSG_TUNNEL;
2211 goto ending;
2212 }
2213 else {
2214 msg->msg_state = HTTP_MSG_DONE;
2215 res->to_forward = 0;
2216 }
2217
2218 done:
2219
2220 channel_dont_close(res);
2221
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002222 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002223 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002224 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002225 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2226 if (res->flags & CF_SHUTW) {
2227 /* response errors are most likely due to the
2228 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002229 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002230 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002231 goto return_bad_res;
2232 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002233 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002234 return 1;
2235 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002236 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2237 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002238 return 0;
2239
2240 missing_data_or_waiting:
2241 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002242 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002243
2244 /* stop waiting for data if the input is closed before the end. If the
2245 * client side was already closed, it means that the client has aborted,
2246 * so we don't want to count this as a server abort. Otherwise it's a
2247 * server abort.
2248 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002249 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002250 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002251 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002252 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002253 if (htx_is_empty(htx))
2254 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002255 }
2256
Christopher Faulete0768eb2018-10-03 16:38:02 +02002257 /* When TE: chunked is used, we need to get there again to parse
2258 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002259 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2260 * are filters registered on the stream, we don't want to forward a
2261 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002262 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002263 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002264 channel_dont_close(res);
2265
2266 /* We know that more data are expected, but we couldn't send more that
2267 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2268 * system knows it must not set a PUSH on this first part. Interactive
2269 * modes are already handled by the stream sock layer. We must not do
2270 * this in content-length mode because it could present the MSG_MORE
2271 * flag with the last block of forwarded data, which would cause an
2272 * additional delay to be observed by the receiver.
2273 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002274 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002275 res->flags |= CF_EXPECT_MORE;
2276
2277 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002278 DBG_TRACE_DEVEL("waiting for more data to forward",
2279 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002280 return 0;
2281
Christopher Faulet93e02d82019-03-08 14:18:50 +01002282 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002283 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2284 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002285 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002286 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002287 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002288 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002289 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002290 if (!(s->flags & SF_ERR_MASK))
2291 s->flags |= SF_ERR_SRVCL;
2292 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002293
Christopher Faulet93e02d82019-03-08 14:18:50 +01002294 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002295 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2296 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002297 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002298 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002299 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002300 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002301 if (!(s->flags & SF_ERR_MASK))
2302 s->flags |= SF_ERR_CLICL;
2303 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002304
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002305 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002306 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2307 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002308 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002309 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002310 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002311 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002312 if (!(s->flags & SF_ERR_MASK))
2313 s->flags |= SF_ERR_INTERNAL;
2314 goto return_error;
2315
Christopher Faulet93e02d82019-03-08 14:18:50 +01002316 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002317 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002318 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002319 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002320 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2321 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002322 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002323 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002324 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002325 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002326
Christopher Faulet93e02d82019-03-08 14:18:50 +01002327 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002328 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002329 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002330 if (!(s->flags & SF_FINST_MASK))
2331 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002332 DBG_TRACE_DEVEL("leaving on error",
2333 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002334 return 0;
2335}
2336
Christopher Fauletf2824e62018-10-01 12:12:37 +02002337/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002338 * returns zero in case of an irrecoverable error such as too large a request
2339 * to build a valid response, 1 in case of successful redirect (hence the rule
2340 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002341 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002342int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002343{
Christopher Faulet99daf282018-11-28 22:58:13 +01002344 struct channel *req = &s->req;
2345 struct channel *res = &s->res;
2346 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002347 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002348 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002349 struct ist status, reason, location;
2350 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002351 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002352
2353 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002354 if (!chunk) {
2355 if (!(s->flags & SF_ERR_MASK))
2356 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002357 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002358 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002359
Christopher Faulet99daf282018-11-28 22:58:13 +01002360 /*
2361 * Create the location
2362 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002363 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002364 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002365 case REDIRECT_TYPE_SCHEME: {
2366 struct http_hdr_ctx ctx;
2367 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002368 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002369
Christopher Faulet99daf282018-11-28 22:58:13 +01002370 host = ist("");
2371 ctx.blk = NULL;
2372 if (http_find_header(htx, ist("Host"), &ctx, 0))
2373 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002374
Christopher Faulet297fbb42019-05-13 14:41:27 +02002375 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002376 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2377 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002378 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002379 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002380 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2381 int qs = 0;
2382 while (qs < path.len) {
2383 if (*(path.ptr + qs) == '?') {
2384 path.len = qs;
2385 break;
2386 }
2387 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002388 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002389 }
2390 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002391 else
2392 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002393
Christopher Faulet99daf282018-11-28 22:58:13 +01002394 if (rule->rdr_str) { /* this is an old "redirect" rule */
2395 /* add scheme */
2396 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2397 goto fail;
2398 }
2399 else {
2400 /* add scheme with executing log format */
2401 chunk->data += build_logline(s, chunk->area + chunk->data,
2402 chunk->size - chunk->data,
2403 &rule->rdr_fmt);
2404 }
2405 /* add "://" + host + path */
2406 if (!chunk_memcat(chunk, "://", 3) ||
2407 !chunk_memcat(chunk, host.ptr, host.len) ||
2408 !chunk_memcat(chunk, path.ptr, path.len))
2409 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002410
Christopher Faulet99daf282018-11-28 22:58:13 +01002411 /* append a slash at the end of the location if needed and missing */
2412 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2413 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2414 if (chunk->data + 1 >= chunk->size)
2415 goto fail;
2416 chunk->area[chunk->data++] = '/';
2417 }
2418 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002419 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002420
Christopher Faulet99daf282018-11-28 22:58:13 +01002421 case REDIRECT_TYPE_PREFIX: {
2422 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002423 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002424
Christopher Faulet297fbb42019-05-13 14:41:27 +02002425 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002426 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2427 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002428 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002429 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002430 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2431 int qs = 0;
2432 while (qs < path.len) {
2433 if (*(path.ptr + qs) == '?') {
2434 path.len = qs;
2435 break;
2436 }
2437 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002438 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002439 }
2440 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002441 else
2442 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002443
Christopher Faulet99daf282018-11-28 22:58:13 +01002444 if (rule->rdr_str) { /* this is an old "redirect" rule */
2445 /* add prefix. Note that if prefix == "/", we don't want to
2446 * add anything, otherwise it makes it hard for the user to
2447 * configure a self-redirection.
2448 */
2449 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2450 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2451 goto fail;
2452 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002453 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002454 else {
2455 /* add prefix with executing log format */
2456 chunk->data += build_logline(s, chunk->area + chunk->data,
2457 chunk->size - chunk->data,
2458 &rule->rdr_fmt);
2459 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002460
Christopher Faulet99daf282018-11-28 22:58:13 +01002461 /* add path */
2462 if (!chunk_memcat(chunk, path.ptr, path.len))
2463 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002464
Christopher Faulet99daf282018-11-28 22:58:13 +01002465 /* append a slash at the end of the location if needed and missing */
2466 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2467 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2468 if (chunk->data + 1 >= chunk->size)
2469 goto fail;
2470 chunk->area[chunk->data++] = '/';
2471 }
2472 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002473 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002474 case REDIRECT_TYPE_LOCATION:
2475 default:
2476 if (rule->rdr_str) { /* this is an old "redirect" rule */
2477 /* add location */
2478 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2479 goto fail;
2480 }
2481 else {
2482 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002483 int len = build_logline(s, chunk->area + chunk->data,
2484 chunk->size - chunk->data,
2485 &rule->rdr_fmt);
2486 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY)
2487 return 2;
2488
2489 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002490 }
2491 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002492 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002493 location = ist2(chunk->area, chunk->data);
2494
2495 /*
2496 * Create the 30x response
2497 */
2498 switch (rule->code) {
2499 case 308:
2500 status = ist("308");
2501 reason = ist("Permanent Redirect");
2502 break;
2503 case 307:
2504 status = ist("307");
2505 reason = ist("Temporary Redirect");
2506 break;
2507 case 303:
2508 status = ist("303");
2509 reason = ist("See Other");
2510 break;
2511 case 301:
2512 status = ist("301");
2513 reason = ist("Moved Permanently");
2514 break;
2515 case 302:
2516 default:
2517 status = ist("302");
2518 reason = ist("Found");
2519 break;
2520 }
2521
Christopher Faulet08e66462019-05-23 16:44:59 +02002522 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2523 close = 1;
2524
Christopher Faulet99daf282018-11-28 22:58:13 +01002525 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002526 /* Trim any possible response */
2527 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002528 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2529 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2530 if (!sl)
2531 goto fail;
2532 sl->info.res.status = rule->code;
2533 s->txn->status = rule->code;
2534
Christopher Faulet08e66462019-05-23 16:44:59 +02002535 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2536 goto fail;
2537
2538 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002539 !htx_add_header(htx, ist("Location"), location))
2540 goto fail;
2541
2542 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2543 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2544 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002545 }
2546
2547 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002548 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2549 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002550 }
2551
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002552 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002553 goto fail;
2554
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002555 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002556 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002557 if (!http_forward_proxy_resp(s, 1))
2558 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002559
Christopher Faulet60b33a52020-01-28 09:18:10 +01002560 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2561 /* let's log the request time */
2562 s->logs.tv_request = now;
Christopher Fauletd3475882021-10-04 14:16:46 +02002563 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002564
Christopher Faulet60b33a52020-01-28 09:18:10 +01002565 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002566 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002567 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002568
2569 if (!(s->flags & SF_ERR_MASK))
2570 s->flags |= SF_ERR_LOCAL;
2571 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002572 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002573
Christopher Faulet99daf282018-11-28 22:58:13 +01002574 free_trash_chunk(chunk);
2575 return 1;
2576
2577 fail:
2578 /* If an error occurred, remove the incomplete HTTP response from the
2579 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002580 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002581 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002582 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002583}
2584
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002585/* Replace all headers matching the name <name>. The header value is replaced if
2586 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2587 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2588 * values are evaluated one by one. It returns 0 on success and -1 on error.
2589 */
2590int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2591 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002592{
2593 struct http_hdr_ctx ctx;
2594 struct buffer *output = get_trash_chunk();
2595
Christopher Faulet72333522018-10-24 11:25:02 +02002596 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002597 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002598 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2599 continue;
2600
2601 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2602 if (output->data == -1)
2603 return -1;
2604 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2605 return -1;
2606 }
2607 return 0;
2608}
2609
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002610/* This function executes one of the set-{method,path,query,uri} actions. It
2611 * takes the string from the variable 'replace' with length 'len', then modifies
2612 * the relevant part of the request line accordingly. Then it updates various
2613 * pointers to the next elements which were moved, and the total buffer length.
2614 * It finds the action to be performed in p[2], previously filled by function
2615 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2616 * error, though this can be revisited when this code is finally exploited.
2617 *
2618 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002619 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002620 *
2621 * In query string case, the mark question '?' must be set at the start of the
2622 * string by the caller, event if the replacement query string is empty.
2623 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002624int http_req_replace_stline(int action, const char *replace, int len,
2625 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002626{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002627 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002628
2629 switch (action) {
2630 case 0: // method
2631 if (!http_replace_req_meth(htx, ist2(replace, len)))
2632 return -1;
2633 break;
2634
2635 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002636 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002637 return -1;
2638 break;
2639
2640 case 2: // query
2641 if (!http_replace_req_query(htx, ist2(replace, len)))
2642 return -1;
2643 break;
2644
2645 case 3: // uri
2646 if (!http_replace_req_uri(htx, ist2(replace, len)))
2647 return -1;
2648 break;
2649
Christopher Faulet312294f2020-09-02 17:17:44 +02002650 case 4: // path + query
2651 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2652 return -1;
2653 break;
2654
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002655 default:
2656 return -1;
2657 }
2658 return 0;
2659}
2660
2661/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002662 * variable <status> contains the new status code. This function never fails. It
2663 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002664 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002665int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002666{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002667 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002668 char *res;
2669
2670 chunk_reset(&trash);
2671 res = ultoa_o(status, trash.area, trash.size);
2672 trash.data = res - trash.area;
2673
2674 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002675 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002676 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002677 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002678 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002679
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002680 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002681 return -1;
2682 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002683}
2684
Christopher Faulet3e964192018-10-24 11:39:23 +02002685/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2686 * transaction <txn>. Returns the verdict of the first rule that prevents
2687 * further processing of the request (auth, deny, ...), and defaults to
2688 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2689 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2690 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2691 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2692 * status.
2693 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002694static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2695 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002696{
2697 struct session *sess = strm_sess(s);
2698 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002699 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002700 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002701 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002702
Christopher Faulet3e964192018-10-24 11:39:23 +02002703 /* If "the current_rule_list" match the executed rule list, we are in
2704 * resume condition. If a resume is needed it is always in the action
2705 * and never in the ACL or converters. In this case, we initialise the
2706 * current rule, and go to the action execution point.
2707 */
2708 if (s->current_rule) {
2709 rule = s->current_rule;
2710 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002711 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002712 goto resume_execution;
2713 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002714 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002715
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002716 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002717 /* start the ruleset evaluation in strict mode */
2718 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002719
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002720 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002721 /* check optional condition */
2722 if (rule->cond) {
2723 int ret;
2724
2725 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2726 ret = acl_pass(ret);
2727
2728 if (rule->cond->pol == ACL_COND_UNLESS)
2729 ret = !ret;
2730
2731 if (!ret) /* condition not matched */
2732 continue;
2733 }
2734
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002735 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002736 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002737 if (rule->kw->flags & KWF_EXPERIMENTAL)
2738 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2739
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002740 /* Always call the action function if defined */
2741 if (rule->action_ptr) {
2742 if ((s->req.flags & CF_READ_ERROR) ||
2743 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2744 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002745 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002746
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002747 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002748 case ACT_RET_CONT:
2749 break;
2750 case ACT_RET_STOP:
2751 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002752 s->last_rule_file = rule->conf.file;
2753 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002754 goto end;
2755 case ACT_RET_YIELD:
2756 s->current_rule = rule;
2757 rule_ret = HTTP_RULE_RES_YIELD;
2758 goto end;
2759 case ACT_RET_ERR:
2760 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002761 s->last_rule_file = rule->conf.file;
2762 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002763 goto end;
2764 case ACT_RET_DONE:
2765 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002766 s->last_rule_file = rule->conf.file;
2767 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002768 goto end;
2769 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002770 if (txn->status == -1)
2771 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002772 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002773 s->last_rule_file = rule->conf.file;
2774 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002775 goto end;
2776 case ACT_RET_ABRT:
2777 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002778 s->last_rule_file = rule->conf.file;
2779 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002780 goto end;
2781 case ACT_RET_INV:
2782 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002783 s->last_rule_file = rule->conf.file;
2784 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002785 goto end;
2786 }
2787 continue; /* eval the next rule */
2788 }
2789
2790 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002791 switch (rule->action) {
2792 case ACT_ACTION_ALLOW:
2793 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002794 s->last_rule_file = rule->conf.file;
2795 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002796 goto end;
2797
2798 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002799 txn->status = rule->arg.http_reply->status;
2800 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002801 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002802 s->last_rule_file = rule->conf.file;
2803 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002804 goto end;
2805
2806 case ACT_HTTP_REQ_TARPIT:
2807 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002808 txn->status = rule->arg.http_reply->status;
2809 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002810 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002811 s->last_rule_file = rule->conf.file;
2812 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002813 goto end;
2814
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002815 case ACT_HTTP_REDIR: {
2816 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2817
2818 if (ret == 2) // 2 == skip
2819 break;
2820
2821 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002822 s->last_rule_file = rule->conf.file;
2823 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002824 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002825 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002826
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002827 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002828 default:
2829 break;
2830 }
2831 }
2832
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002833 if (def_rules && s->current_rule_list == def_rules) {
2834 s->current_rule_list = rules;
2835 goto restart;
2836 }
2837
Christopher Faulet3e964192018-10-24 11:39:23 +02002838 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002839 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002840 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002841 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002842
Christopher Faulet3e964192018-10-24 11:39:23 +02002843 /* we reached the end of the rules, nothing to report */
2844 return rule_ret;
2845}
2846
2847/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2848 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2849 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2850 * is returned, the process can continue the evaluation of next rule list. If
2851 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2852 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002853 * must be returned. If *YIELD is returned, the caller must call again the
2854 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002855 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002856static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2857 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002858{
2859 struct session *sess = strm_sess(s);
2860 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002861 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002862 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002863 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002864
Christopher Faulet3e964192018-10-24 11:39:23 +02002865 /* If "the current_rule_list" match the executed rule list, we are in
2866 * resume condition. If a resume is needed it is always in the action
2867 * and never in the ACL or converters. In this case, we initialise the
2868 * current rule, and go to the action execution point.
2869 */
2870 if (s->current_rule) {
2871 rule = s->current_rule;
2872 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002873 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002874 goto resume_execution;
2875 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002876 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2877
2878 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002879
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002880 /* start the ruleset evaluation in strict mode */
2881 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002882
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002883 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002884 /* check optional condition */
2885 if (rule->cond) {
2886 int ret;
2887
2888 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2889 ret = acl_pass(ret);
2890
2891 if (rule->cond->pol == ACL_COND_UNLESS)
2892 ret = !ret;
2893
2894 if (!ret) /* condition not matched */
2895 continue;
2896 }
2897
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002898 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002899resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002900 if (rule->kw->flags & KWF_EXPERIMENTAL)
2901 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002902
2903 /* Always call the action function if defined */
2904 if (rule->action_ptr) {
2905 if ((s->req.flags & CF_READ_ERROR) ||
2906 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2907 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002908 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002909
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002910 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002911 case ACT_RET_CONT:
2912 break;
2913 case ACT_RET_STOP:
2914 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002915 s->last_rule_file = rule->conf.file;
2916 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002917 goto end;
2918 case ACT_RET_YIELD:
2919 s->current_rule = rule;
2920 rule_ret = HTTP_RULE_RES_YIELD;
2921 goto end;
2922 case ACT_RET_ERR:
2923 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002924 s->last_rule_file = rule->conf.file;
2925 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002926 goto end;
2927 case ACT_RET_DONE:
2928 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002929 s->last_rule_file = rule->conf.file;
2930 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002931 goto end;
2932 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002933 if (txn->status == -1)
2934 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002935 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002936 s->last_rule_file = rule->conf.file;
2937 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002938 goto end;
2939 case ACT_RET_ABRT:
2940 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002941 s->last_rule_file = rule->conf.file;
2942 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002943 goto end;
2944 case ACT_RET_INV:
2945 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002946 s->last_rule_file = rule->conf.file;
2947 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002948 goto end;
2949 }
2950 continue; /* eval the next rule */
2951 }
2952
2953 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002954 switch (rule->action) {
2955 case ACT_ACTION_ALLOW:
2956 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01002957 s->last_rule_file = rule->conf.file;
2958 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002959 goto end;
2960
2961 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002962 txn->status = rule->arg.http_reply->status;
2963 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002964 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002965 s->last_rule_file = rule->conf.file;
2966 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002967 goto end;
2968
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002969 case ACT_HTTP_REDIR: {
2970 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02002971
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002972 if (ret == 2) // 2 == skip
2973 break;
2974
2975 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002976 s->last_rule_file = rule->conf.file;
2977 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002978 goto end;
2979 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002980 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002981 default:
2982 break;
2983 }
2984 }
2985
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002986 if (def_rules && s->current_rule_list == def_rules) {
2987 s->current_rule_list = rules;
2988 goto restart;
2989 }
2990
Christopher Faulet3e964192018-10-24 11:39:23 +02002991 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002992 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002993 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002994 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002995
Christopher Faulet3e964192018-10-24 11:39:23 +02002996 /* we reached the end of the rules, nothing to report */
2997 return rule_ret;
2998}
2999
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003000/* Executes backend and frontend http-after-response rules for the stream <s>,
3001 * in that order. it return 1 on success and 0 on error. It is the caller
3002 * responsibility to catch error or ignore it. If it catches it, this function
3003 * may be called a second time, for the internal error.
3004 */
3005int http_eval_after_res_rules(struct stream *s)
3006{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003007 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003008 struct session *sess = s->sess;
3009 enum rule_result ret = HTTP_RULE_RES_CONT;
3010
Christopher Faulet507479b2020-05-15 12:29:46 +02003011 /* Eval after-response ruleset only if the reply is not const */
3012 if (s->txn->flags & TX_CONST_REPLY)
3013 goto end;
3014
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003015 /* prune the request variables if not already done and swap to the response variables. */
3016 if (s->vars_reqres.scope != SCOPE_RES) {
3017 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3018 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02003019 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003020 }
3021
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003022 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
3023 rules = &s->be->http_after_res_rules;
3024
3025 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01003026 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003027 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
3028 rules = &sess->fe->http_after_res_rules;
3029 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
3030 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003031
Christopher Faulet507479b2020-05-15 12:29:46 +02003032 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003033 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3034 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3035}
3036
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003037/*
3038 * Manage client-side cookie. It can impact performance by about 2% so it is
3039 * desirable to call it only when needed. This code is quite complex because
3040 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3041 * highly recommended not to touch this part without a good reason !
3042 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003043static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003044{
3045 struct session *sess = s->sess;
3046 struct http_txn *txn = s->txn;
3047 struct htx *htx;
3048 struct http_hdr_ctx ctx;
3049 char *hdr_beg, *hdr_end, *del_from;
3050 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3051 int preserve_hdr;
3052
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003053 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003054 ctx.blk = NULL;
3055 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003056 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003057 del_from = NULL; /* nothing to be deleted */
3058 preserve_hdr = 0; /* assume we may kill the whole header */
3059
3060 /* Now look for cookies. Conforming to RFC2109, we have to support
3061 * attributes whose name begin with a '$', and associate them with
3062 * the right cookie, if we want to delete this cookie.
3063 * So there are 3 cases for each cookie read :
3064 * 1) it's a special attribute, beginning with a '$' : ignore it.
3065 * 2) it's a server id cookie that we *MAY* want to delete : save
3066 * some pointers on it (last semi-colon, beginning of cookie...)
3067 * 3) it's an application cookie : we *MAY* have to delete a previous
3068 * "special" cookie.
3069 * At the end of loop, if a "special" cookie remains, we may have to
3070 * remove it. If no application cookie persists in the header, we
3071 * *MUST* delete it.
3072 *
3073 * Note: RFC2965 is unclear about the processing of spaces around
3074 * the equal sign in the ATTR=VALUE form. A careful inspection of
3075 * the RFC explicitly allows spaces before it, and not within the
3076 * tokens (attrs or values). An inspection of RFC2109 allows that
3077 * too but section 10.1.3 lets one think that spaces may be allowed
3078 * after the equal sign too, resulting in some (rare) buggy
3079 * implementations trying to do that. So let's do what servers do.
3080 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3081 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003082 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003083 * causes parsing to become ambiguous. Browsers also allow spaces
3084 * within values even without quotes.
3085 *
3086 * We have to keep multiple pointers in order to support cookie
3087 * removal at the beginning, middle or end of header without
3088 * corrupting the header. All of these headers are valid :
3089 *
3090 * hdr_beg hdr_end
3091 * | |
3092 * v |
3093 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3094 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3095 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3096 * | | | | | | |
3097 * | | | | | | |
3098 * | | | | | | +--> next
3099 * | | | | | +----> val_end
3100 * | | | | +-----------> val_beg
3101 * | | | +--------------> equal
3102 * | | +----------------> att_end
3103 * | +---------------------> att_beg
3104 * +--------------------------> prev
3105 *
3106 */
3107 hdr_beg = ctx.value.ptr;
3108 hdr_end = hdr_beg + ctx.value.len;
3109 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3110 /* Iterate through all cookies on this line */
3111
3112 /* find att_beg */
3113 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003114 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003115 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003116 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003117
3118 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3119 att_beg++;
3120
3121 /* find att_end : this is the first character after the last non
3122 * space before the equal. It may be equal to hdr_end.
3123 */
3124 equal = att_end = att_beg;
3125 while (equal < hdr_end) {
3126 if (*equal == '=' || *equal == ',' || *equal == ';')
3127 break;
3128 if (HTTP_IS_SPHT(*equal++))
3129 continue;
3130 att_end = equal;
3131 }
3132
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003133 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003134 * is between <att_beg> and <equal>, both may be identical.
3135 */
3136 /* look for end of cookie if there is an equal sign */
3137 if (equal < hdr_end && *equal == '=') {
3138 /* look for the beginning of the value */
3139 val_beg = equal + 1;
3140 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3141 val_beg++;
3142
3143 /* find the end of the value, respecting quotes */
3144 next = http_find_cookie_value_end(val_beg, hdr_end);
3145
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003146 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003147 val_end = next;
3148 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3149 val_end--;
3150 }
3151 else
3152 val_beg = val_end = next = equal;
3153
3154 /* We have nothing to do with attributes beginning with
3155 * '$'. However, they will automatically be removed if a
3156 * header before them is removed, since they're supposed
3157 * to be linked together.
3158 */
3159 if (*att_beg == '$')
3160 continue;
3161
3162 /* Ignore cookies with no equal sign */
3163 if (equal == next) {
3164 /* This is not our cookie, so we must preserve it. But if we already
3165 * scheduled another cookie for removal, we cannot remove the
3166 * complete header, but we can remove the previous block itself.
3167 */
3168 preserve_hdr = 1;
3169 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003170 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003171 val_end += delta;
3172 next += delta;
3173 hdr_end += delta;
3174 prev = del_from;
3175 del_from = NULL;
3176 }
3177 continue;
3178 }
3179
3180 /* if there are spaces around the equal sign, we need to
3181 * strip them otherwise we'll get trouble for cookie captures,
3182 * or even for rewrites. Since this happens extremely rarely,
3183 * it does not hurt performance.
3184 */
3185 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3186 int stripped_before = 0;
3187 int stripped_after = 0;
3188
3189 if (att_end != equal) {
3190 memmove(att_end, equal, hdr_end - equal);
3191 stripped_before = (att_end - equal);
3192 equal += stripped_before;
3193 val_beg += stripped_before;
3194 }
3195
3196 if (val_beg > equal + 1) {
3197 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3198 stripped_after = (equal + 1) - val_beg;
3199 val_beg += stripped_after;
3200 stripped_before += stripped_after;
3201 }
3202
3203 val_end += stripped_before;
3204 next += stripped_before;
3205 hdr_end += stripped_before;
3206 }
3207 /* now everything is as on the diagram above */
3208
3209 /* First, let's see if we want to capture this cookie. We check
3210 * that we don't already have a client side cookie, because we
3211 * can only capture one. Also as an optimisation, we ignore
3212 * cookies shorter than the declared name.
3213 */
3214 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3215 (val_end - att_beg >= sess->fe->capture_namelen) &&
3216 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3217 int log_len = val_end - att_beg;
3218
3219 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3220 ha_alert("HTTP logging : out of memory.\n");
3221 } else {
3222 if (log_len > sess->fe->capture_len)
3223 log_len = sess->fe->capture_len;
3224 memcpy(txn->cli_cookie, att_beg, log_len);
3225 txn->cli_cookie[log_len] = 0;
3226 }
3227 }
3228
3229 /* Persistence cookies in passive, rewrite or insert mode have the
3230 * following form :
3231 *
3232 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3233 *
3234 * For cookies in prefix mode, the form is :
3235 *
3236 * Cookie: NAME=SRV~VALUE
3237 */
3238 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3239 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3240 struct server *srv = s->be->srv;
3241 char *delim;
3242
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003243 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003244 * have the server ID between val_beg and delim, and the original cookie between
3245 * delim+1 and val_end. Otherwise, delim==val_end :
3246 *
3247 * hdr_beg
3248 * |
3249 * v
3250 * NAME=SRV; # in all but prefix modes
3251 * NAME=SRV~OPAQUE ; # in prefix mode
3252 * || || | |+-> next
3253 * || || | +--> val_end
3254 * || || +---------> delim
3255 * || |+------------> val_beg
3256 * || +-------------> att_end = equal
3257 * |+-----------------> att_beg
3258 * +------------------> prev
3259 *
3260 */
3261 if (s->be->ck_opts & PR_CK_PFX) {
3262 for (delim = val_beg; delim < val_end; delim++)
3263 if (*delim == COOKIE_DELIM)
3264 break;
3265 }
3266 else {
3267 char *vbar1;
3268 delim = val_end;
3269 /* Now check if the cookie contains a date field, which would
3270 * appear after a vertical bar ('|') just after the server name
3271 * and before the delimiter.
3272 */
3273 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3274 if (vbar1) {
3275 /* OK, so left of the bar is the server's cookie and
3276 * right is the last seen date. It is a base64 encoded
3277 * 30-bit value representing the UNIX date since the
3278 * epoch in 4-second quantities.
3279 */
3280 int val;
3281 delim = vbar1++;
3282 if (val_end - vbar1 >= 5) {
3283 val = b64tos30(vbar1);
3284 if (val > 0)
3285 txn->cookie_last_date = val << 2;
3286 }
3287 /* look for a second vertical bar */
3288 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3289 if (vbar1 && (val_end - vbar1 > 5)) {
3290 val = b64tos30(vbar1 + 1);
3291 if (val > 0)
3292 txn->cookie_first_date = val << 2;
3293 }
3294 }
3295 }
3296
3297 /* if the cookie has an expiration date and the proxy wants to check
3298 * it, then we do that now. We first check if the cookie is too old,
3299 * then only if it has expired. We detect strict overflow because the
3300 * time resolution here is not great (4 seconds). Cookies with dates
3301 * in the future are ignored if their offset is beyond one day. This
3302 * allows an admin to fix timezone issues without expiring everyone
3303 * and at the same time avoids keeping unwanted side effects for too
3304 * long.
3305 */
3306 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3307 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3308 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3309 txn->flags &= ~TX_CK_MASK;
3310 txn->flags |= TX_CK_OLD;
3311 delim = val_beg; // let's pretend we have not found the cookie
3312 txn->cookie_first_date = 0;
3313 txn->cookie_last_date = 0;
3314 }
3315 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3316 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3317 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3318 txn->flags &= ~TX_CK_MASK;
3319 txn->flags |= TX_CK_EXPIRED;
3320 delim = val_beg; // let's pretend we have not found the cookie
3321 txn->cookie_first_date = 0;
3322 txn->cookie_last_date = 0;
3323 }
3324
3325 /* Here, we'll look for the first running server which supports the cookie.
3326 * This allows to share a same cookie between several servers, for example
3327 * to dedicate backup servers to specific servers only.
3328 * However, to prevent clients from sticking to cookie-less backup server
3329 * when they have incidentely learned an empty cookie, we simply ignore
3330 * empty cookies and mark them as invalid.
3331 * The same behaviour is applied when persistence must be ignored.
3332 */
3333 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3334 srv = NULL;
3335
3336 while (srv) {
3337 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3338 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3339 if ((srv->cur_state != SRV_ST_STOPPED) ||
3340 (s->be->options & PR_O_PERSIST) ||
3341 (s->flags & SF_FORCE_PRST)) {
3342 /* we found the server and we can use it */
3343 txn->flags &= ~TX_CK_MASK;
3344 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3345 s->flags |= SF_DIRECT | SF_ASSIGNED;
3346 s->target = &srv->obj_type;
3347 break;
3348 } else {
3349 /* we found a server, but it's down,
3350 * mark it as such and go on in case
3351 * another one is available.
3352 */
3353 txn->flags &= ~TX_CK_MASK;
3354 txn->flags |= TX_CK_DOWN;
3355 }
3356 }
3357 srv = srv->next;
3358 }
3359
3360 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3361 /* no server matched this cookie or we deliberately skipped it */
3362 txn->flags &= ~TX_CK_MASK;
3363 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3364 txn->flags |= TX_CK_UNUSED;
3365 else
3366 txn->flags |= TX_CK_INVALID;
3367 }
3368
3369 /* depending on the cookie mode, we may have to either :
3370 * - delete the complete cookie if we're in insert+indirect mode, so that
3371 * the server never sees it ;
3372 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003373 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003374 * if we're in cookie prefix mode
3375 */
3376 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3377 int delta; /* negative */
3378
3379 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3380 delta = val_beg - (delim + 1);
3381 val_end += delta;
3382 next += delta;
3383 hdr_end += delta;
3384 del_from = NULL;
3385 preserve_hdr = 1; /* we want to keep this cookie */
3386 }
3387 else if (del_from == NULL &&
3388 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3389 del_from = prev;
3390 }
3391 }
3392 else {
3393 /* This is not our cookie, so we must preserve it. But if we already
3394 * scheduled another cookie for removal, we cannot remove the
3395 * complete header, but we can remove the previous block itself.
3396 */
3397 preserve_hdr = 1;
3398
3399 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003400 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003401 if (att_beg >= del_from)
3402 att_beg += delta;
3403 if (att_end >= del_from)
3404 att_end += delta;
3405 val_beg += delta;
3406 val_end += delta;
3407 next += delta;
3408 hdr_end += delta;
3409 prev = del_from;
3410 del_from = NULL;
3411 }
3412 }
3413
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003414 } /* for each cookie */
3415
3416
3417 /* There are no more cookies on this line.
3418 * We may still have one (or several) marked for deletion at the
3419 * end of the line. We must do this now in two ways :
3420 * - if some cookies must be preserved, we only delete from the
3421 * mark to the end of line ;
3422 * - if nothing needs to be preserved, simply delete the whole header
3423 */
3424 if (del_from) {
3425 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3426 }
3427 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003428 if (hdr_beg != hdr_end)
3429 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003430 else
3431 http_remove_header(htx, &ctx);
3432 }
3433 } /* for each "Cookie header */
3434}
3435
3436/*
3437 * Manage server-side cookies. It can impact performance by about 2% so it is
3438 * desirable to call it only when needed. This function is also used when we
3439 * just need to know if there is a cookie (eg: for check-cache).
3440 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003441static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003442{
3443 struct session *sess = s->sess;
3444 struct http_txn *txn = s->txn;
3445 struct htx *htx;
3446 struct http_hdr_ctx ctx;
3447 struct server *srv;
3448 char *hdr_beg, *hdr_end;
3449 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003450 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003451
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003452 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003453
3454 ctx.blk = NULL;
3455 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003456 int is_first = 1;
3457
Andrew McDermottbfb15ab2022-02-11 18:26:49 +00003458 if (is_cookie2 || !http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003459 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3460 break;
3461 is_cookie2 = 1;
3462 }
3463
3464 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3465 * <prev> points to the colon.
3466 */
3467 txn->flags |= TX_SCK_PRESENT;
3468
3469 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3470 * check-cache is enabled) and we are not interested in checking
3471 * them. Warning, the cookie capture is declared in the frontend.
3472 */
3473 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3474 break;
3475
3476 /* OK so now we know we have to process this response cookie.
3477 * The format of the Set-Cookie header is slightly different
3478 * from the format of the Cookie header in that it does not
3479 * support the comma as a cookie delimiter (thus the header
3480 * cannot be folded) because the Expires attribute described in
3481 * the original Netscape's spec may contain an unquoted date
3482 * with a comma inside. We have to live with this because
3483 * many browsers don't support Max-Age and some browsers don't
3484 * support quoted strings. However the Set-Cookie2 header is
3485 * clean.
3486 *
3487 * We have to keep multiple pointers in order to support cookie
3488 * removal at the beginning, middle or end of header without
3489 * corrupting the header (in case of set-cookie2). A special
3490 * pointer, <scav> points to the beginning of the set-cookie-av
3491 * fields after the first semi-colon. The <next> pointer points
3492 * either to the end of line (set-cookie) or next unquoted comma
3493 * (set-cookie2). All of these headers are valid :
3494 *
3495 * hdr_beg hdr_end
3496 * | |
3497 * v |
3498 * NAME1 = VALUE 1 ; Secure; Path="/" |
3499 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3500 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3501 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3502 * | | | | | | | |
3503 * | | | | | | | +-> next
3504 * | | | | | | +------------> scav
3505 * | | | | | +--------------> val_end
3506 * | | | | +--------------------> val_beg
3507 * | | | +----------------------> equal
3508 * | | +------------------------> att_end
3509 * | +----------------------------> att_beg
3510 * +------------------------------> prev
3511 * -------------------------------> hdr_beg
3512 */
3513 hdr_beg = ctx.value.ptr;
3514 hdr_end = hdr_beg + ctx.value.len;
3515 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3516
3517 /* Iterate through all cookies on this line */
3518
3519 /* find att_beg */
3520 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003521 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003522 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003523 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003524
3525 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3526 att_beg++;
3527
3528 /* find att_end : this is the first character after the last non
3529 * space before the equal. It may be equal to hdr_end.
3530 */
3531 equal = att_end = att_beg;
3532
3533 while (equal < hdr_end) {
3534 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3535 break;
3536 if (HTTP_IS_SPHT(*equal++))
3537 continue;
3538 att_end = equal;
3539 }
3540
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003541 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003542 * is between <att_beg> and <equal>, both may be identical.
3543 */
3544
3545 /* look for end of cookie if there is an equal sign */
3546 if (equal < hdr_end && *equal == '=') {
3547 /* look for the beginning of the value */
3548 val_beg = equal + 1;
3549 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3550 val_beg++;
3551
3552 /* find the end of the value, respecting quotes */
3553 next = http_find_cookie_value_end(val_beg, hdr_end);
3554
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003555 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003556 val_end = next;
3557 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3558 val_end--;
3559 }
3560 else {
3561 /* <equal> points to next comma, semi-colon or EOL */
3562 val_beg = val_end = next = equal;
3563 }
3564
3565 if (next < hdr_end) {
3566 /* Set-Cookie2 supports multiple cookies, and <next> points to
3567 * a colon or semi-colon before the end. So skip all attr-value
3568 * pairs and look for the next comma. For Set-Cookie, since
3569 * commas are permitted in values, skip to the end.
3570 */
3571 if (is_cookie2)
3572 next = http_find_hdr_value_end(next, hdr_end);
3573 else
3574 next = hdr_end;
3575 }
3576
3577 /* Now everything is as on the diagram above */
3578
3579 /* Ignore cookies with no equal sign */
3580 if (equal == val_end)
3581 continue;
3582
3583 /* If there are spaces around the equal sign, we need to
3584 * strip them otherwise we'll get trouble for cookie captures,
3585 * or even for rewrites. Since this happens extremely rarely,
3586 * it does not hurt performance.
3587 */
3588 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3589 int stripped_before = 0;
3590 int stripped_after = 0;
3591
3592 if (att_end != equal) {
3593 memmove(att_end, equal, hdr_end - equal);
3594 stripped_before = (att_end - equal);
3595 equal += stripped_before;
3596 val_beg += stripped_before;
3597 }
3598
3599 if (val_beg > equal + 1) {
3600 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3601 stripped_after = (equal + 1) - val_beg;
3602 val_beg += stripped_after;
3603 stripped_before += stripped_after;
3604 }
3605
3606 val_end += stripped_before;
3607 next += stripped_before;
3608 hdr_end += stripped_before;
3609
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003610 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003611 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003612 }
3613
3614 /* First, let's see if we want to capture this cookie. We check
3615 * that we don't already have a server side cookie, because we
3616 * can only capture one. Also as an optimisation, we ignore
3617 * cookies shorter than the declared name.
3618 */
3619 if (sess->fe->capture_name != NULL &&
3620 txn->srv_cookie == NULL &&
3621 (val_end - att_beg >= sess->fe->capture_namelen) &&
3622 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3623 int log_len = val_end - att_beg;
3624 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3625 ha_alert("HTTP logging : out of memory.\n");
3626 }
3627 else {
3628 if (log_len > sess->fe->capture_len)
3629 log_len = sess->fe->capture_len;
3630 memcpy(txn->srv_cookie, att_beg, log_len);
3631 txn->srv_cookie[log_len] = 0;
3632 }
3633 }
3634
3635 srv = objt_server(s->target);
3636 /* now check if we need to process it for persistence */
3637 if (!(s->flags & SF_IGNORE_PRST) &&
3638 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3639 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3640 /* assume passive cookie by default */
3641 txn->flags &= ~TX_SCK_MASK;
3642 txn->flags |= TX_SCK_FOUND;
3643
3644 /* If the cookie is in insert mode on a known server, we'll delete
3645 * this occurrence because we'll insert another one later.
3646 * We'll delete it too if the "indirect" option is set and we're in
3647 * a direct access.
3648 */
3649 if (s->be->ck_opts & PR_CK_PSV) {
3650 /* The "preserve" flag was set, we don't want to touch the
3651 * server's cookie.
3652 */
3653 }
3654 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3655 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3656 /* this cookie must be deleted */
3657 if (prev == hdr_beg && next == hdr_end) {
3658 /* whole header */
3659 http_remove_header(htx, &ctx);
3660 /* note: while both invalid now, <next> and <hdr_end>
3661 * are still equal, so the for() will stop as expected.
3662 */
3663 } else {
3664 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003665 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003666 next = prev;
3667 hdr_end += delta;
3668 }
3669 txn->flags &= ~TX_SCK_MASK;
3670 txn->flags |= TX_SCK_DELETED;
3671 /* and go on with next cookie */
3672 }
3673 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3674 /* replace bytes val_beg->val_end with the cookie name associated
3675 * with this server since we know it.
3676 */
3677 int sliding, delta;
3678
3679 ctx.value = ist2(val_beg, val_end - val_beg);
3680 ctx.lws_before = ctx.lws_after = 0;
3681 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3682 delta = srv->cklen - (val_end - val_beg);
3683 sliding = (ctx.value.ptr - val_beg);
3684 hdr_beg += sliding;
3685 val_beg += sliding;
3686 next += sliding + delta;
3687 hdr_end += sliding + delta;
3688
3689 txn->flags &= ~TX_SCK_MASK;
3690 txn->flags |= TX_SCK_REPLACED;
3691 }
3692 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3693 /* insert the cookie name associated with this server
3694 * before existing cookie, and insert a delimiter between them..
3695 */
3696 int sliding, delta;
3697 ctx.value = ist2(val_beg, 0);
3698 ctx.lws_before = ctx.lws_after = 0;
3699 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3700 delta = srv->cklen + 1;
3701 sliding = (ctx.value.ptr - val_beg);
3702 hdr_beg += sliding;
3703 val_beg += sliding;
3704 next += sliding + delta;
3705 hdr_end += sliding + delta;
3706
3707 val_beg[srv->cklen] = COOKIE_DELIM;
3708 txn->flags &= ~TX_SCK_MASK;
3709 txn->flags |= TX_SCK_REPLACED;
3710 }
3711 }
3712 /* that's done for this cookie, check the next one on the same
3713 * line when next != hdr_end (only if is_cookie2).
3714 */
3715 }
3716 }
3717}
3718
Christopher Faulet25a02f62018-10-24 12:00:25 +02003719/*
3720 * Parses the Cache-Control and Pragma request header fields to determine if
3721 * the request may be served from the cache and/or if it is cacheable. Updates
3722 * s->txn->flags.
3723 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003724void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003725{
3726 struct http_txn *txn = s->txn;
3727 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003728 struct http_hdr_ctx ctx = { .blk = NULL };
3729 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003730
3731 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3732 return; /* nothing more to do here */
3733
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003734 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003735 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003736
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003737 /* Check "pragma" header for HTTP/1.0 compatibility. */
3738 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3739 if (isteqi(ctx.value, ist("no-cache"))) {
3740 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003741 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003742 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003743
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003744 ctx.blk = NULL;
3745 /* Don't use the cache and don't try to store if we found the
3746 * Authorization header */
3747 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3748 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3749 txn->flags |= TX_CACHE_IGNORE;
3750 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003751
Christopher Faulet25a02f62018-10-24 12:00:25 +02003752
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003753 /* Look for "cache-control" header and iterate over all the values
3754 * until we find one that specifies that caching is possible or not. */
3755 ctx.blk = NULL;
3756 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003757 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003758 /* We don't check the values after max-age, max-stale nor min-fresh,
3759 * we simply don't use the cache when they're specified. */
3760 if (istmatchi(ctx.value, ist("max-age")) ||
3761 istmatchi(ctx.value, ist("no-cache")) ||
3762 istmatchi(ctx.value, ist("max-stale")) ||
3763 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003764 txn->flags |= TX_CACHE_IGNORE;
3765 continue;
3766 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003767 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003768 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3769 continue;
3770 }
3771 }
3772
3773 /* RFC7234#5.4:
3774 * When the Cache-Control header field is also present and
3775 * understood in a request, Pragma is ignored.
3776 * When the Cache-Control header field is not present in a
3777 * request, caches MUST consider the no-cache request
3778 * pragma-directive as having the same effect as if
3779 * "Cache-Control: no-cache" were present.
3780 */
3781 if (!cc_found && pragma_found)
3782 txn->flags |= TX_CACHE_IGNORE;
3783}
3784
3785/*
3786 * Check if response is cacheable or not. Updates s->txn->flags.
3787 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003788void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003789{
3790 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003791 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003792 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003793 int has_freshness_info = 0;
3794 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003795
3796 if (txn->status < 200) {
3797 /* do not try to cache interim responses! */
3798 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3799 return;
3800 }
3801
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003802 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003803 /* Check "pragma" header for HTTP/1.0 compatibility. */
3804 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3805 if (isteqi(ctx.value, ist("no-cache"))) {
3806 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3807 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003808 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003809 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003810
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003811 /* Look for "cache-control" header and iterate over all the values
3812 * until we find one that specifies that caching is possible or not. */
3813 ctx.blk = NULL;
3814 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3815 if (isteqi(ctx.value, ist("public"))) {
3816 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003817 continue;
3818 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003819 if (isteqi(ctx.value, ist("private")) ||
3820 isteqi(ctx.value, ist("no-cache")) ||
3821 isteqi(ctx.value, ist("no-store")) ||
3822 isteqi(ctx.value, ist("max-age=0")) ||
3823 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003824 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003825 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003826 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003827 /* We might have a no-cache="set-cookie" form. */
3828 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3829 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003830 continue;
3831 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003832
3833 if (istmatchi(ctx.value, ist("s-maxage")) ||
3834 istmatchi(ctx.value, ist("max-age"))) {
3835 has_freshness_info = 1;
3836 continue;
3837 }
3838 }
3839
3840 /* If no freshness information could be found in Cache-Control values,
3841 * look for an Expires header. */
3842 if (!has_freshness_info) {
3843 ctx.blk = NULL;
3844 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003845 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003846
3847 /* If no freshness information could be found in Cache-Control or Expires
3848 * values, look for an explicit validator. */
3849 if (!has_freshness_info) {
3850 ctx.blk = NULL;
3851 has_validator = 1;
3852 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3853 ctx.blk = NULL;
3854 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3855 has_validator = 0;
3856 }
3857 }
3858
3859 /* We won't store an entry that has neither a cache validator nor an
3860 * explicit expiration time, as suggested in RFC 7234#3. */
3861 if (!has_freshness_info && !has_validator)
3862 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003863}
3864
Christopher Faulet377c5a52018-10-24 21:21:30 +02003865/*
3866 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3867 * for the current backend.
3868 *
3869 * It is assumed that the request is either a HEAD, GET, or POST and that the
3870 * uri_auth field is valid.
3871 *
3872 * Returns 1 if stats should be provided, otherwise 0.
3873 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003874static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003875{
3876 struct uri_auth *uri_auth = backend->uri_auth;
3877 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003878 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003879 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003880
3881 if (!uri_auth)
3882 return 0;
3883
3884 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3885 return 0;
3886
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003887 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003888 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003889 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003890 if (*uri_auth->uri_prefix == '/') {
3891 struct http_uri_parser parser = http_uri_parser_init(uri);
3892 uri = http_parse_path(&parser);
3893 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003894
3895 /* check URI size */
3896 if (uri_auth->uri_len > uri.len)
3897 return 0;
3898
3899 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3900 return 0;
3901
3902 return 1;
3903}
3904
3905/* This function prepares an applet to handle the stats. It can deal with the
3906 * "100-continue" expectation, check that admin rules are met for POST requests,
3907 * and program a response message if something was unexpected. It cannot fail
3908 * and always relies on the stats applet to complete the job. It does not touch
3909 * analysers nor counters, which are left to the caller. It does not touch
3910 * s->target which is supposed to already point to the stats applet. The caller
3911 * is expected to have already assigned an appctx to the stream.
3912 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003913static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003914{
3915 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003916 struct session *sess = s->sess;
3917 struct http_txn *txn = s->txn;
3918 struct http_msg *msg = &txn->req;
3919 struct uri_auth *uri_auth = s->be->uri_auth;
3920 const char *h, *lookup, *end;
Christopher Faulet693b23b2022-02-28 09:09:05 +01003921 struct appctx *appctx = __cs_appctx(s->csb);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003922 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003923 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003924
Christopher Faulet377c5a52018-10-24 21:21:30 +02003925 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3926 appctx->st1 = appctx->st2 = 0;
3927 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003928 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003929 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3930 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3931 appctx->ctx.stats.flags |= STAT_CHUNKED;
3932
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003933 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003934 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003935 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3936 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003937
3938 for (h = lookup; h <= end - 3; h++) {
3939 if (memcmp(h, ";up", 3) == 0) {
3940 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3941 break;
3942 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003943 }
3944
3945 for (h = lookup; h <= end - 9; h++) {
3946 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003947 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3948 break;
3949 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003950 }
3951
3952 if (uri_auth->refresh) {
3953 for (h = lookup; h <= end - 10; h++) {
3954 if (memcmp(h, ";norefresh", 10) == 0) {
3955 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3956 break;
3957 }
3958 }
3959 }
3960
3961 for (h = lookup; h <= end - 4; h++) {
3962 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003963 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003964 break;
3965 }
3966 }
3967
3968 for (h = lookup; h <= end - 6; h++) {
3969 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003970 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003971 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3972 break;
3973 }
3974 }
3975
Christopher Faulet6338a082019-09-09 15:50:54 +02003976 for (h = lookup; h <= end - 5; h++) {
3977 if (memcmp(h, ";json", 5) == 0) {
3978 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3979 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3980 break;
3981 }
3982 }
3983
3984 for (h = lookup; h <= end - 12; h++) {
3985 if (memcmp(h, ";json-schema", 12) == 0) {
3986 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
3987 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
3988 break;
3989 }
3990 }
3991
Christopher Faulet377c5a52018-10-24 21:21:30 +02003992 for (h = lookup; h <= end - 8; h++) {
3993 if (memcmp(h, ";st=", 4) == 0) {
3994 int i;
3995 h += 4;
3996 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
3997 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3998 if (strncmp(stat_status_codes[i], h, 4) == 0) {
3999 appctx->ctx.stats.st_code = i;
4000 break;
4001 }
4002 }
4003 break;
4004 }
4005 }
4006
4007 appctx->ctx.stats.scope_str = 0;
4008 appctx->ctx.stats.scope_len = 0;
4009 for (h = lookup; h <= end - 8; h++) {
4010 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4011 int itx = 0;
4012 const char *h2;
4013 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4014 const char *err;
4015
4016 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4017 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004018 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4019 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004020 if (*h == ';' || *h == '&' || *h == ' ')
4021 break;
4022 itx++;
4023 h++;
4024 }
4025
4026 if (itx > STAT_SCOPE_TXT_MAXLEN)
4027 itx = STAT_SCOPE_TXT_MAXLEN;
4028 appctx->ctx.stats.scope_len = itx;
4029
4030 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4031 memcpy(scope_txt, h2, itx);
4032 scope_txt[itx] = '\0';
4033 err = invalid_char(scope_txt);
4034 if (err) {
4035 /* bad char in search text => clear scope */
4036 appctx->ctx.stats.scope_str = 0;
4037 appctx->ctx.stats.scope_len = 0;
4038 }
4039 break;
4040 }
4041 }
4042
4043 /* now check whether we have some admin rules for this request */
4044 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4045 int ret = 1;
4046
4047 if (stats_admin_rule->cond) {
4048 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4049 ret = acl_pass(ret);
4050 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4051 ret = !ret;
4052 }
4053
4054 if (ret) {
4055 /* no rule, or the rule matches */
4056 appctx->ctx.stats.flags |= STAT_ADMIN;
4057 break;
4058 }
4059 }
4060
Christopher Faulet5d45e382019-02-27 15:15:23 +01004061 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4062 appctx->st0 = STAT_HTTP_HEAD;
4063 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004064 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004065 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004066 if (msg->msg_state < HTTP_MSG_DATA)
4067 req->analysers |= AN_REQ_HTTP_BODY;
4068 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004069 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004070 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004071 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4072 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4073 appctx->st0 = STAT_HTTP_LAST;
4074 }
4075 }
4076 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004077 /* Unsupported method */
4078 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4079 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4080 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004081 }
4082
4083 s->task->nice = -32; /* small boost for HTTP statistics */
4084 return 1;
4085}
4086
Christopher Faulet021a8e42021-03-29 10:46:38 +02004087/* This function waits for the message payload at most <time> milliseconds (may
4088 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4089 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4090 * the result:
4091 *
4092 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4093 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004094 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004095 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004096 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004097 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004098 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004099 * to the client, depending on the channel (408 on request side, 504 on response
4100 * side). All other errors must be handled by the caller.
4101 */
4102enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4103 unsigned int time, unsigned int bytes)
4104{
4105 struct session *sess = s->sess;
4106 struct http_txn *txn = s->txn;
4107 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4108 struct htx *htx;
4109 enum rule_result ret = HTTP_RULE_RES_CONT;
4110
4111 htx = htxbuf(&chn->buf);
4112
4113 if (htx->flags & HTX_FL_PARSING_ERROR) {
4114 ret = HTTP_RULE_RES_BADREQ;
4115 goto end;
4116 }
4117 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4118 ret = HTTP_RULE_RES_ERROR;
4119 goto end;
4120 }
4121
4122 /* Do nothing for bodyless and CONNECT requests */
4123 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4124 goto end;
4125
4126 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4127 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4128 ret = HTTP_RULE_RES_ERROR;
4129 goto end;
4130 }
4131 }
4132
4133 msg->msg_state = HTTP_MSG_DATA;
4134
4135 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4136 * been received or if the buffer is full.
4137 */
Christopher Faulet78335962021-09-23 14:46:32 +02004138 if ((htx->flags & HTX_FL_EOM) ||
4139 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4140 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004141 cs_rx_blocked_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004142 goto end;
4143
4144 if (bytes) {
4145 struct htx_blk *blk;
4146 unsigned int len = 0;
4147
4148 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4149 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4150 continue;
4151 len += htx_get_blksz(blk);
4152 if (len >= bytes)
4153 goto end;
4154 }
4155 }
4156
4157 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4158 if (!(chn->flags & CF_ISRESP))
4159 goto abort_req;
4160 goto abort_res;
4161 }
4162
4163 /* we get here if we need to wait for more data */
4164 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4165 if (!tick_isset(chn->analyse_exp))
4166 chn->analyse_exp = tick_add_ifset(now_ms, time);
4167 ret = HTTP_RULE_RES_YIELD;
4168 }
4169
4170 end:
4171 return ret;
4172
4173 abort_req:
4174 txn->status = 408;
4175 if (!(s->flags & SF_ERR_MASK))
4176 s->flags |= SF_ERR_CLITO;
4177 if (!(s->flags & SF_FINST_MASK))
4178 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004179 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004180 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004181 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004182 http_reply_and_close(s, txn->status, http_error_message(s));
4183 ret = HTTP_RULE_RES_ABRT;
4184 goto end;
4185
4186 abort_res:
4187 txn->status = 504;
4188 if (!(s->flags & SF_ERR_MASK))
4189 s->flags |= SF_ERR_SRVTO;
4190 if (!(s->flags & SF_FINST_MASK))
4191 s->flags |= SF_FINST_D;
4192 stream_inc_http_fail_ctr(s);
4193 http_reply_and_close(s, txn->status, http_error_message(s));
4194 ret = HTTP_RULE_RES_ABRT;
4195 goto end;
4196}
4197
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004198void http_perform_server_redirect(struct stream *s, struct conn_stream *cs)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004199{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004200 struct channel *req = &s->req;
4201 struct channel *res = &s->res;
4202 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004203 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004204 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004205 struct ist path, location;
4206 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004207 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004208
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004209 /*
4210 * Create the location
4211 */
4212 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004213
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004214 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004215 /* special prefix "/" means don't change URL */
4216 srv = __objt_server(s->target);
4217 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4218 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4219 return;
4220 }
4221
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004222 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004223 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004224 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004225 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4226 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004227 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004228 return;
4229
4230 if (!chunk_memcat(&trash, path.ptr, path.len))
4231 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004232 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004233
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004234 /*
4235 * Create the 302 respone
4236 */
4237 htx = htx_from_buf(&res->buf);
4238 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4239 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4240 ist("HTTP/1.1"), ist("302"), ist("Found"));
4241 if (!sl)
4242 goto fail;
4243 sl->info.res.status = 302;
4244 s->txn->status = 302;
4245
4246 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4247 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4248 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4249 !htx_add_header(htx, ist("Location"), location))
4250 goto fail;
4251
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004252 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004253 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004254
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004255 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004256 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004257 if (!http_forward_proxy_resp(s, 1))
4258 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004259
4260 /* return without error. */
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004261 cs_shutr(cs);
4262 cs_shutw(cs);
Christopher Faulet50264b42022-03-30 19:39:30 +02004263 s->conn_err_type = STRM_ET_NONE;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004264 cs->state = CS_ST_CLO;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004265
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004266 if (!(s->flags & SF_ERR_MASK))
4267 s->flags |= SF_ERR_LOCAL;
4268 if (!(s->flags & SF_FINST_MASK))
4269 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004270
4271 /* FIXME: we should increase a counter of redirects per server and per backend. */
4272 srv_inc_sess_ctr(srv);
4273 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004274 return;
4275
4276 fail:
4277 /* If an error occurred, remove the incomplete HTTP response from the
4278 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004279 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004280}
4281
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004282/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004283 * because an error was triggered during the body forwarding.
4284 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004285static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004286{
4287 struct channel *chn = &s->req;
4288 struct http_txn *txn = s->txn;
4289
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004290 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004291
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004292 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4293 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004294 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004295 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004296 goto end;
4297 }
4298
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004299 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4300 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004301 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004302 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004303
4304 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004305 /* No need to read anymore, the request was completely parsed.
4306 * We can shut the read side unless we want to abort_on_close,
4307 * or we have a POST request. The issue with POST requests is
4308 * that some browsers still send a CRLF after the request, and
4309 * this CRLF must be read so that it does not remain in the kernel
4310 * buffers, otherwise a close could cause an RST on some systems
4311 * (eg: Linux).
4312 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004313 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004314 channel_dont_read(chn);
4315
4316 /* if the server closes the connection, we want to immediately react
4317 * and close the socket to save packets and syscalls.
4318 */
Christopher Faulet8abe7122022-03-30 15:10:18 +02004319 s->csb->flags |= CS_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004320
4321 /* In any case we've finished parsing the request so we must
4322 * disable Nagle when sending data because 1) we're not going
4323 * to shut this side, and 2) the server is waiting for us to
4324 * send pending data.
4325 */
4326 chn->flags |= CF_NEVER_WAIT;
4327
Christopher Fauletd01ce402019-01-02 17:44:13 +01004328 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4329 /* The server has not finished to respond, so we
4330 * don't want to move in order not to upset it.
4331 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004332 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004333 return;
4334 }
4335
Christopher Fauletf2824e62018-10-01 12:12:37 +02004336 /* When we get here, it means that both the request and the
4337 * response have finished receiving. Depending on the connection
4338 * mode, we'll have to wait for the last bytes to leave in either
4339 * direction, and sometimes for a close to be effective.
4340 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004341 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004342 /* Tunnel mode will not have any analyser so it needs to
4343 * poll for reads.
4344 */
4345 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004346 if (b_data(&chn->buf)) {
4347 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004348 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004349 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004350 txn->req.msg_state = HTTP_MSG_TUNNEL;
4351 }
4352 else {
4353 /* we're not expecting any new data to come for this
4354 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004355 *
4356 * However, there is an exception if the response
4357 * length is undefined. In this case, we need to wait
4358 * the close from the server. The response will be
4359 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004360 */
4361 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4362 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004363 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004364
4365 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4366 channel_shutr_now(chn);
4367 channel_shutw_now(chn);
4368 }
4369 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004370 goto check_channel_flags;
4371 }
4372
4373 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4374 http_msg_closing:
4375 /* nothing else to forward, just waiting for the output buffer
4376 * to be empty and for the shutw_now to take effect.
4377 */
4378 if (channel_is_empty(chn)) {
4379 txn->req.msg_state = HTTP_MSG_CLOSED;
4380 goto http_msg_closed;
4381 }
4382 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004383 txn->req.msg_state = HTTP_MSG_ERROR;
4384 goto end;
4385 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004386 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004387 return;
4388 }
4389
4390 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4391 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004392 /* if we don't know whether the server will close, we need to hard close */
4393 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
Christopher Faulet8abe7122022-03-30 15:10:18 +02004394 s->csb->flags |= CS_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004395 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004396 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004397 channel_dont_read(chn);
4398 goto end;
4399 }
4400
4401 check_channel_flags:
4402 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4403 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4404 /* if we've just closed an output, let's switch */
4405 txn->req.msg_state = HTTP_MSG_CLOSING;
4406 goto http_msg_closing;
4407 }
4408
4409 end:
4410 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004411 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4412 chn->flags |= CF_NEVER_WAIT;
4413 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004414 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004415 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004416 channel_auto_close(chn);
4417 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004418 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004419}
4420
4421
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004422/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004423 * because an error was triggered during the body forwarding.
4424 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004425static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004426{
4427 struct channel *chn = &s->res;
4428 struct http_txn *txn = s->txn;
4429
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004430 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004431
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004432 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4433 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004434 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004435 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004436 goto end;
4437 }
4438
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004439 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4440 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004441 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004442 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004443
4444 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4445 /* In theory, we don't need to read anymore, but we must
4446 * still monitor the server connection for a possible close
4447 * while the request is being uploaded, so we don't disable
4448 * reading.
4449 */
4450 /* channel_dont_read(chn); */
4451
4452 if (txn->req.msg_state < HTTP_MSG_DONE) {
4453 /* The client seems to still be sending data, probably
4454 * because we got an error response during an upload.
4455 * We have the choice of either breaking the connection
4456 * or letting it pass through. Let's do the later.
4457 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004458 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004459 return;
4460 }
4461
4462 /* When we get here, it means that both the request and the
4463 * response have finished receiving. Depending on the connection
4464 * mode, we'll have to wait for the last bytes to leave in either
4465 * direction, and sometimes for a close to be effective.
4466 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004467 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004468 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004469 if (b_data(&chn->buf)) {
4470 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004471 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004472 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004473 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4474 }
4475 else {
4476 /* we're not expecting any new data to come for this
4477 * transaction, so we can close it.
4478 */
4479 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4480 channel_shutr_now(chn);
4481 channel_shutw_now(chn);
4482 }
4483 }
4484 goto check_channel_flags;
4485 }
4486
4487 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4488 http_msg_closing:
4489 /* nothing else to forward, just waiting for the output buffer
4490 * to be empty and for the shutw_now to take effect.
4491 */
4492 if (channel_is_empty(chn)) {
4493 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4494 goto http_msg_closed;
4495 }
4496 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004497 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004498 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4499 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004500 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004501 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004502 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004503 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004504 goto end;
4505 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004506 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004507 return;
4508 }
4509
4510 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4511 http_msg_closed:
4512 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004513 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004514 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004515 goto end;
4516 }
4517
4518 check_channel_flags:
4519 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4520 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4521 /* if we've just closed an output, let's switch */
4522 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4523 goto http_msg_closing;
4524 }
4525
4526 end:
4527 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004528 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4529 chn->flags |= CF_NEVER_WAIT;
4530 if (HAS_RSP_DATA_FILTERS(s))
4531 chn->analysers |= AN_RES_FLT_XFER_DATA;
4532 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004533 channel_auto_close(chn);
4534 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004535 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004536}
4537
Christopher Fauletef70e252020-01-28 09:26:19 +01004538/* Forward a response generated by HAProxy (error/redirect/return). This
4539 * function forwards all pending incoming data. If <final> is set to 0, nothing
4540 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004541 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004542 * returned. If an error occurred, 0 is returned. If it fails, this function
4543 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004544 */
4545int http_forward_proxy_resp(struct stream *s, int final)
4546{
4547 struct channel *req = &s->req;
4548 struct channel *res = &s->res;
4549 struct htx *htx = htxbuf(&res->buf);
4550 size_t data;
4551
4552 if (final) {
4553 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004554
Christopher Fauletaab1b672020-11-18 16:44:02 +01004555 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004556 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004557
Christopher Fauletd6c48362020-10-19 18:01:38 +02004558 if (s->txn->meth == HTTP_METH_HEAD)
4559 htx_skip_msg_payload(htx);
4560
Christopher Fauletef70e252020-01-28 09:26:19 +01004561 channel_auto_read(req);
4562 channel_abort(req);
4563 channel_auto_close(req);
4564 channel_htx_erase(req, htxbuf(&req->buf));
4565
4566 res->wex = tick_add_ifset(now_ms, res->wto);
4567 channel_auto_read(res);
4568 channel_auto_close(res);
4569 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004570 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004571 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004572 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004573 else {
4574 /* Send ASAP informational messages. Rely on CF_EOI for final
4575 * response.
4576 */
4577 res->flags |= CF_SEND_DONTWAIT;
4578 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004579
4580 data = htx->data - co_data(res);
4581 c_adv(res, data);
4582 htx->first = -1;
4583 res->total += data;
4584 return 1;
4585}
4586
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004587void http_server_error(struct stream *s, struct conn_stream *cs, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004588 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004589{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004590 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004591 if (!(s->flags & SF_ERR_MASK))
4592 s->flags |= err;
4593 if (!(s->flags & SF_FINST_MASK))
4594 s->flags |= finst;
4595}
4596
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004597void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004598{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004599 if (!msg) {
4600 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4601 goto end;
4602 }
4603
4604 if (http_reply_message(s, msg) == -1) {
4605 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004606 * it is already an internal error. If it was already a "const"
4607 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004608 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004609 if (s->txn->status == 500) {
4610 if (s->txn->flags & TX_CONST_REPLY)
4611 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004612 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004613 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004614 s->txn->status = 500;
4615 s->txn->http_reply = NULL;
4616 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4617 }
4618
4619end:
4620 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004621
Christopher Faulet2d565002021-09-10 09:17:50 +02004622 /* At this staged, HTTP analysis is finished */
4623 s->req.analysers &= AN_REQ_FLT_END;
4624 s->req.analyse_exp = TICK_ETERNITY;
4625
4626 s->res.analysers &= AN_RES_FLT_END;
4627 s->res.analyse_exp = TICK_ETERNITY;
4628
Christopher Faulet0f226952018-10-22 09:29:56 +02004629 channel_auto_read(&s->req);
4630 channel_abort(&s->req);
4631 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004632 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004633 channel_auto_read(&s->res);
4634 channel_auto_close(&s->res);
4635 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004636}
4637
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004638struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004639{
4640 const int msgnum = http_get_status_idx(s->txn->status);
4641
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004642 if (s->txn->http_reply)
4643 return s->txn->http_reply;
4644 else if (s->be->replies[msgnum])
4645 return s->be->replies[msgnum];
4646 else if (strm_fe(s)->replies[msgnum])
4647 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004648 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004649 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004650}
4651
Christopher Faulet40e6b552020-06-25 16:04:50 +02004652/* Produces an HTX message from an http reply. Depending on the http reply type,
4653 * a, errorfile, an raw file or a log-format string is used. On success, it
4654 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4655 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004656 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004657int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004658{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004659 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004660 struct htx_sl *sl;
4661 struct buffer *body = NULL;
4662 const char *status, *reason, *clen, *ctype;
4663 unsigned int slflags;
4664 int ret = 0;
4665
Christopher Faulete29a97e2020-05-14 14:49:25 +02004666 /*
4667 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4668 *
4669 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4670 * as no payload if NULL. the TXN status code is set with the status
4671 * of the original reply.
4672 */
4673
4674 if (reply->type == HTTP_REPLY_INDIRECT) {
4675 if (reply->body.reply)
4676 reply = reply->body.reply;
4677 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004678 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4679 /* get default error message */
4680 if (reply == s->txn->http_reply)
4681 s->txn->http_reply = NULL;
4682 reply = http_error_message(s);
4683 if (reply->type == HTTP_REPLY_INDIRECT) {
4684 if (reply->body.reply)
4685 reply = reply->body.reply;
4686 }
4687 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004688
4689 if (reply->type == HTTP_REPLY_ERRMSG) {
4690 /* implicit or explicit error message*/
4691 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004692 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004693 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004694 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004695 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004696 }
4697 else {
4698 /* no payload, file or log-format string */
4699 if (reply->type == HTTP_REPLY_RAW) {
4700 /* file */
4701 body = &reply->body.obj;
4702 }
4703 else if (reply->type == HTTP_REPLY_LOGFMT) {
4704 /* log-format string */
4705 body = alloc_trash_chunk();
4706 if (!body)
4707 goto fail_alloc;
4708 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4709 }
4710 /* else no payload */
4711
4712 status = ultoa(reply->status);
4713 reason = http_get_reason(reply->status);
4714 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4715 if (!body || !b_data(body))
4716 slflags |= HTX_SL_F_BODYLESS;
4717 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4718 if (!sl)
4719 goto fail;
4720 sl->info.res.status = reply->status;
4721
4722 clen = (body ? ultoa(b_data(body)) : "0");
4723 ctype = reply->ctype;
4724
4725 if (!LIST_ISEMPTY(&reply->hdrs)) {
4726 struct http_reply_hdr *hdr;
4727 struct buffer *value = alloc_trash_chunk();
4728
4729 if (!value)
4730 goto fail;
4731
4732 list_for_each_entry(hdr, &reply->hdrs, list) {
4733 chunk_reset(value);
4734 value->data = build_logline(s, value->area, value->size, &hdr->value);
4735 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4736 free_trash_chunk(value);
4737 goto fail;
4738 }
4739 chunk_reset(value);
4740 }
4741 free_trash_chunk(value);
4742 }
4743
4744 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4745 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4746 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004747 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004748 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004749
4750 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004751 }
4752
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004753 leave:
4754 if (reply->type == HTTP_REPLY_LOGFMT)
4755 free_trash_chunk(body);
4756 return ret;
4757
4758 fail_alloc:
4759 if (!(s->flags & SF_ERR_MASK))
4760 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004761 /* fall through */
4762 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004763 ret = -1;
4764 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004765}
4766
4767/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004768 * occurs -1 is returned and the response channel is truncated, removing this
4769 * way the faulty reply. This function may fail when the reply is formatted
4770 * (http_reply_to_htx) or when the reply is forwarded
4771 * (http_forward_proxy_resp). On the last case, it is because a
4772 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004773 */
4774int http_reply_message(struct stream *s, struct http_reply *reply)
4775{
4776 struct channel *res = &s->res;
4777 struct htx *htx = htx_from_buf(&res->buf);
4778
4779 if (s->txn->status == -1)
4780 s->txn->status = reply->status;
4781 channel_htx_truncate(res, htx);
4782
4783 if (http_reply_to_htx(s, htx, reply) == -1)
4784 goto fail;
4785
4786 htx_to_buf(htx, &s->res.buf);
4787 if (!http_forward_proxy_resp(s, 1))
4788 goto fail;
4789 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004790
4791 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004792 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004793 if (!(s->flags & SF_ERR_MASK))
4794 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004795 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004796}
4797
Christopher Faulet50264b42022-03-30 19:39:30 +02004798/* Return the error message corresponding to s->conn_err_type. It is assumed
Christopher Faulet304cc402019-07-15 15:46:28 +02004799 * that the server side is closed. Note that err_type is actually a
4800 * bitmask, where almost only aborts may be cumulated with other
4801 * values. We consider that aborted operations are more important
4802 * than timeouts or errors due to the fact that nobody else in the
4803 * logs might explain incomplete retries. All others should avoid
4804 * being cumulated. It should normally not be possible to have multiple
4805 * aborts at once, but just in case, the first one in sequence is reported.
4806 * Note that connection errors appearing on the second request of a keep-alive
4807 * connection are not reported since this allows the client to retry.
4808 */
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004809void http_return_srv_error(struct stream *s, struct conn_stream *cs)
Christopher Faulet304cc402019-07-15 15:46:28 +02004810{
Christopher Faulet50264b42022-03-30 19:39:30 +02004811 int err_type = s->conn_err_type;
Christopher Faulet304cc402019-07-15 15:46:28 +02004812
4813 /* set s->txn->status for http_error_message(s) */
Christopher Faulet50264b42022-03-30 19:39:30 +02004814 if (err_type & STRM_ET_QUEUE_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004815 s->txn->status = -1;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004816 http_server_error(s, cs, SF_ERR_CLICL, SF_FINST_Q, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004817 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004818 else if (err_type & STRM_ET_CONN_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004819 s->txn->status = -1;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004820 http_server_error(s, cs, SF_ERR_CLICL, SF_FINST_C, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004821 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004822 else if (err_type & STRM_ET_QUEUE_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004823 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004824 http_server_error(s, cs, SF_ERR_SRVTO, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004825 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004826 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004827 else if (err_type & STRM_ET_QUEUE_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004828 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004829 http_server_error(s, cs, SF_ERR_SRVCL, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004830 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004831 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004832 else if (err_type & STRM_ET_CONN_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004833 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004834 http_server_error(s, cs, SF_ERR_SRVTO, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004835 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4836 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004837 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004838 else if (err_type & STRM_ET_CONN_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004839 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004840 http_server_error(s, cs, SF_ERR_SRVCL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004841 (s->flags & SF_SRV_REUSED) ? NULL :
4842 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004843 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004844 else if (err_type & STRM_ET_CONN_RES) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004845 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004846 http_server_error(s, cs, SF_ERR_RESOURCE, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004847 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4848 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004849 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004850 else { /* STRM_ET_CONN_OTHER and others */
Christopher Faulet304cc402019-07-15 15:46:28 +02004851 s->txn->status = 500;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004852 http_server_error(s, cs, SF_ERR_INTERNAL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004853 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004854 }
4855}
4856
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004857
Christopher Faulet4a28a532019-03-01 11:19:40 +01004858/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4859 * on success and -1 on error.
4860 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004861static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004862{
4863 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4864 * then we must send an HTTP/1.1 100 Continue intermediate response.
4865 */
4866 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4867 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4868 struct ist hdr = { .ptr = "Expect", .len = 6 };
4869 struct http_hdr_ctx ctx;
4870
4871 ctx.blk = NULL;
4872 /* Expect is allowed in 1.1, look for it */
4873 if (http_find_header(htx, hdr, &ctx, 0) &&
4874 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004875 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004876 return -1;
4877 http_remove_header(htx, &ctx);
4878 }
4879 }
4880 return 0;
4881}
4882
Christopher Faulet23a3c792018-11-28 10:01:23 +01004883/* Send a 100-Continue response to the client. It returns 0 on success and -1
4884 * on error. The response channel is updated accordingly.
4885 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004886static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004887{
4888 struct channel *res = &s->res;
4889 struct htx *htx = htx_from_buf(&res->buf);
4890 struct htx_sl *sl;
4891 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4892 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004893
4894 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4895 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4896 if (!sl)
4897 goto fail;
4898 sl->info.res.status = 100;
4899
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004900 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004901 goto fail;
4902
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004903 if (!http_forward_proxy_resp(s, 0))
4904 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004905 return 0;
4906
4907 fail:
4908 /* If an error occurred, remove the incomplete HTTP response from the
4909 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004910 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004911 return -1;
4912}
4913
Christopher Faulet12c51e22018-11-28 15:59:42 +01004914
Christopher Faulet0f226952018-10-22 09:29:56 +02004915/*
4916 * Capture headers from message <htx> according to header list <cap_hdr>, and
4917 * fill the <cap> pointers appropriately.
4918 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004919static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004920{
4921 struct cap_hdr *h;
4922 int32_t pos;
4923
Christopher Fauleta3f15502019-05-13 15:27:23 +02004924 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004925 struct htx_blk *blk = htx_get_blk(htx, pos);
4926 enum htx_blk_type type = htx_get_blk_type(blk);
4927 struct ist n, v;
4928
4929 if (type == HTX_BLK_EOH)
4930 break;
4931 if (type != HTX_BLK_HDR)
4932 continue;
4933
4934 n = htx_get_blk_name(htx, blk);
4935
4936 for (h = cap_hdr; h; h = h->next) {
4937 if (h->namelen && (h->namelen == n.len) &&
4938 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4939 if (cap[h->index] == NULL)
4940 cap[h->index] =
4941 pool_alloc(h->pool);
4942
4943 if (cap[h->index] == NULL) {
4944 ha_alert("HTTP capture : out of memory.\n");
4945 break;
4946 }
4947
4948 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01004949 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004950
4951 memcpy(cap[h->index], v.ptr, v.len);
4952 cap[h->index][v.len]=0;
4953 }
4954 }
4955 }
4956}
4957
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004958/* Delete a value in a header between delimiters <from> and <next>. The header
4959 * itself is delimited by <start> and <end> pointers. The number of characters
4960 * displaced is returned, and the pointer to the first delimiter is updated if
4961 * required. The function tries as much as possible to respect the following
4962 * principles :
4963 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4964 * in which case <next> is simply removed
4965 * - set exactly one space character after the new first delimiter, unless there
4966 * are not enough characters in the block being moved to do so.
4967 * - remove unneeded spaces before the previous delimiter and after the new
4968 * one.
4969 *
4970 * It is the caller's responsibility to ensure that :
4971 * - <from> points to a valid delimiter or <start> ;
4972 * - <next> points to a valid delimiter or <end> ;
4973 * - there are non-space chars before <from>.
4974 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004975static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004976{
4977 char *prev = *from;
4978
4979 if (prev == start) {
4980 /* We're removing the first value. eat the semicolon, if <next>
4981 * is lower than <end> */
4982 if (next < end)
4983 next++;
4984
4985 while (next < end && HTTP_IS_SPHT(*next))
4986 next++;
4987 }
4988 else {
4989 /* Remove useless spaces before the old delimiter. */
4990 while (HTTP_IS_SPHT(*(prev-1)))
4991 prev--;
4992 *from = prev;
4993
4994 /* copy the delimiter and if possible a space if we're
4995 * not at the end of the line.
4996 */
4997 if (next < end) {
4998 *prev++ = *next++;
4999 if (prev + 1 < next)
5000 *prev++ = ' ';
5001 while (next < end && HTTP_IS_SPHT(*next))
5002 next++;
5003 }
5004 }
5005 memmove(prev, next, end - next);
5006 return (prev - next);
5007}
5008
Christopher Faulet0f226952018-10-22 09:29:56 +02005009
5010/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005011 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005012 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005013static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005014{
5015 struct ist dst = ist2(str, 0);
5016
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005017 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005018 goto end;
5019 if (dst.len + 1 > len)
5020 goto end;
5021 dst.ptr[dst.len++] = ' ';
5022
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005023 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005024 goto end;
5025 if (dst.len + 1 > len)
5026 goto end;
5027 dst.ptr[dst.len++] = ' ';
5028
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005029 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005030 end:
5031 return dst.len;
5032}
5033
5034/*
5035 * Print a debug line with a start line.
5036 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005037static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005038{
5039 struct session *sess = strm_sess(s);
5040 int max;
5041
5042 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5043 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005044 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Christopher Faulet693b23b2022-02-28 09:09:05 +01005045 cs_conn(s->csb) ? (unsigned short)(__cs_conn(s->csb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005046
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005047 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005048 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005049 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005050 trash.area[trash.data++] = ' ';
5051
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005052 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005053 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005054 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005055 trash.area[trash.data++] = ' ';
5056
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005057 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005058 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005059 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005060 trash.area[trash.data++] = '\n';
5061
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005062 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005063}
5064
5065/*
5066 * Print a debug line with a header.
5067 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005068static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v)
Christopher Faulet0f226952018-10-22 09:29:56 +02005069{
5070 struct session *sess = strm_sess(s);
5071 int max;
5072
5073 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5074 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005075 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Christopher Faulet693b23b2022-02-28 09:09:05 +01005076 cs_conn(s->csb) ? (unsigned short)(__cs_conn(s->csb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005077
5078 max = n.len;
5079 UBOUND(max, trash.size - trash.data - 3);
5080 chunk_memcat(&trash, n.ptr, max);
5081 trash.area[trash.data++] = ':';
5082 trash.area[trash.data++] = ' ';
5083
5084 max = v.len;
5085 UBOUND(max, trash.size - trash.data - 1);
5086 chunk_memcat(&trash, v.ptr, max);
5087 trash.area[trash.data++] = '\n';
5088
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005089 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005090}
5091
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005092void http_txn_reset_req(struct http_txn *txn)
5093{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005094 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005095 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5096}
5097
5098void http_txn_reset_res(struct http_txn *txn)
5099{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005100 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005101 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5102}
5103
5104/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005105 * Create and initialize a new HTTP transaction for stream <s>. This should be
5106 * used before processing any new request. It returns the transaction or NLULL
5107 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005108 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005109struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005110{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005111 struct http_txn *txn;
Christopher Faulet95a61e82021-12-22 14:22:03 +01005112 struct conn_stream *cs = s->csf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005113
Christopher Faulet75f619a2021-03-08 19:12:58 +01005114 txn = pool_alloc(pool_head_http_txn);
5115 if (!txn)
5116 return NULL;
5117 s->txn = txn;
5118
Christopher Faulete9e48202022-03-22 18:13:29 +01005119 txn->flags = ((cs && cs->endp->flags & CS_EP_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005120 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005121 txn->http_reply = NULL;
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005122 txn->l7_buffer = BUF_NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005123 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005124
5125 txn->cookie_first_date = 0;
5126 txn->cookie_last_date = 0;
5127
5128 txn->srv_cookie = NULL;
5129 txn->cli_cookie = NULL;
5130 txn->uri = NULL;
5131
5132 http_txn_reset_req(txn);
5133 http_txn_reset_res(txn);
5134
5135 txn->req.chn = &s->req;
5136 txn->rsp.chn = &s->res;
5137
5138 txn->auth.method = HTTP_AUTH_UNKNOWN;
5139
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02005140 vars_init_head(&s->vars_txn, SCOPE_TXN);
5141 vars_init_head(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005142
5143 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005144}
5145
5146/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005147void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005148{
5149 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005150
5151 /* these ones will have been dynamically allocated */
5152 pool_free(pool_head_requri, txn->uri);
5153 pool_free(pool_head_capture, txn->cli_cookie);
5154 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005155 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005156
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005157 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005158 txn->uri = NULL;
5159 txn->srv_cookie = NULL;
5160 txn->cli_cookie = NULL;
5161
Christopher Faulet59399252019-11-07 14:27:52 +01005162 if (!LIST_ISEMPTY(&s->vars_txn.head))
5163 vars_prune(&s->vars_txn, s->sess, s);
5164 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5165 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005166
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005167 b_free(&txn->l7_buffer);
5168
Christopher Faulet75f619a2021-03-08 19:12:58 +01005169 pool_free(pool_head_http_txn, txn);
5170 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005171}
5172
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005173
5174DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005175
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005176__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005177static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005178{
5179}
5180
5181
5182/*
5183 * Local variables:
5184 * c-indent-level: 8
5185 * c-basic-offset: 8
5186 * End:
5187 */