blob: 7a987a5cf603b662d43bca04177995cba1a03aad [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 Fauleteab17572022-04-26 20:34:38 +02002351 int ret = 1, 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);
Christopher Fauleteab17572022-04-26 20:34:38 +02002486 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) {
2487 ret = 2;
2488 goto out;
2489 }
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002490
2491 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002492 }
2493 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002494 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002495 location = ist2(chunk->area, chunk->data);
2496
2497 /*
2498 * Create the 30x response
2499 */
2500 switch (rule->code) {
2501 case 308:
2502 status = ist("308");
2503 reason = ist("Permanent Redirect");
2504 break;
2505 case 307:
2506 status = ist("307");
2507 reason = ist("Temporary Redirect");
2508 break;
2509 case 303:
2510 status = ist("303");
2511 reason = ist("See Other");
2512 break;
2513 case 301:
2514 status = ist("301");
2515 reason = ist("Moved Permanently");
2516 break;
2517 case 302:
2518 default:
2519 status = ist("302");
2520 reason = ist("Found");
2521 break;
2522 }
2523
Christopher Faulet08e66462019-05-23 16:44:59 +02002524 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2525 close = 1;
2526
Christopher Faulet99daf282018-11-28 22:58:13 +01002527 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002528 /* Trim any possible response */
2529 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002530 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2531 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2532 if (!sl)
2533 goto fail;
2534 sl->info.res.status = rule->code;
2535 s->txn->status = rule->code;
2536
Christopher Faulet08e66462019-05-23 16:44:59 +02002537 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2538 goto fail;
2539
2540 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002541 !htx_add_header(htx, ist("Location"), location))
2542 goto fail;
2543
2544 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2545 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2546 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002547 }
2548
2549 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002550 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2551 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002552 }
2553
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002554 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002555 goto fail;
2556
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002557 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002558 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002559 if (!http_forward_proxy_resp(s, 1))
2560 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002561
Christopher Faulet60b33a52020-01-28 09:18:10 +01002562 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2563 /* let's log the request time */
2564 s->logs.tv_request = now;
Christopher Fauletd3475882021-10-04 14:16:46 +02002565 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002566
Christopher Faulet60b33a52020-01-28 09:18:10 +01002567 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002568 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002569 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002570
2571 if (!(s->flags & SF_ERR_MASK))
2572 s->flags |= SF_ERR_LOCAL;
2573 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002574 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002575
Christopher Fauleteab17572022-04-26 20:34:38 +02002576 out:
Christopher Faulet99daf282018-11-28 22:58:13 +01002577 free_trash_chunk(chunk);
Christopher Fauleteab17572022-04-26 20:34:38 +02002578 return ret;
Christopher Faulet99daf282018-11-28 22:58:13 +01002579
2580 fail:
2581 /* If an error occurred, remove the incomplete HTTP response from the
2582 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002583 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauleteab17572022-04-26 20:34:38 +02002584 ret = 0;
2585 goto out;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002586}
2587
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002588/* Replace all headers matching the name <name>. The header value is replaced if
2589 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2590 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2591 * values are evaluated one by one. It returns 0 on success and -1 on error.
2592 */
2593int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2594 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002595{
2596 struct http_hdr_ctx ctx;
2597 struct buffer *output = get_trash_chunk();
2598
Christopher Faulet72333522018-10-24 11:25:02 +02002599 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002600 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002601 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2602 continue;
2603
2604 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2605 if (output->data == -1)
2606 return -1;
2607 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2608 return -1;
2609 }
2610 return 0;
2611}
2612
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002613/* This function executes one of the set-{method,path,query,uri} actions. It
2614 * takes the string from the variable 'replace' with length 'len', then modifies
2615 * the relevant part of the request line accordingly. Then it updates various
2616 * pointers to the next elements which were moved, and the total buffer length.
2617 * It finds the action to be performed in p[2], previously filled by function
2618 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2619 * error, though this can be revisited when this code is finally exploited.
2620 *
2621 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002622 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002623 *
2624 * In query string case, the mark question '?' must be set at the start of the
2625 * string by the caller, event if the replacement query string is empty.
2626 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002627int http_req_replace_stline(int action, const char *replace, int len,
2628 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002629{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002630 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002631
2632 switch (action) {
2633 case 0: // method
2634 if (!http_replace_req_meth(htx, ist2(replace, len)))
2635 return -1;
2636 break;
2637
2638 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002639 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002640 return -1;
2641 break;
2642
2643 case 2: // query
2644 if (!http_replace_req_query(htx, ist2(replace, len)))
2645 return -1;
2646 break;
2647
2648 case 3: // uri
2649 if (!http_replace_req_uri(htx, ist2(replace, len)))
2650 return -1;
2651 break;
2652
Christopher Faulet312294f2020-09-02 17:17:44 +02002653 case 4: // path + query
2654 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2655 return -1;
2656 break;
2657
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002658 default:
2659 return -1;
2660 }
2661 return 0;
2662}
2663
2664/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002665 * variable <status> contains the new status code. This function never fails. It
2666 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002667 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002668int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002669{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002670 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002671 char *res;
2672
2673 chunk_reset(&trash);
2674 res = ultoa_o(status, trash.area, trash.size);
2675 trash.data = res - trash.area;
2676
2677 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002678 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002679 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002680 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002681 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002682
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002683 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002684 return -1;
2685 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002686}
2687
Christopher Faulet3e964192018-10-24 11:39:23 +02002688/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2689 * transaction <txn>. Returns the verdict of the first rule that prevents
2690 * further processing of the request (auth, deny, ...), and defaults to
2691 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2692 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2693 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2694 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2695 * status.
2696 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002697static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2698 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002699{
2700 struct session *sess = strm_sess(s);
2701 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002702 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002703 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002704 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002705
Christopher Faulet3e964192018-10-24 11:39:23 +02002706 /* If "the current_rule_list" match the executed rule list, we are in
2707 * resume condition. If a resume is needed it is always in the action
2708 * and never in the ACL or converters. In this case, we initialise the
2709 * current rule, and go to the action execution point.
2710 */
2711 if (s->current_rule) {
2712 rule = s->current_rule;
2713 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002714 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002715 goto resume_execution;
2716 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002717 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002718
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002719 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002720 /* start the ruleset evaluation in strict mode */
2721 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002722
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002723 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002724 /* check optional condition */
2725 if (rule->cond) {
2726 int ret;
2727
2728 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2729 ret = acl_pass(ret);
2730
2731 if (rule->cond->pol == ACL_COND_UNLESS)
2732 ret = !ret;
2733
2734 if (!ret) /* condition not matched */
2735 continue;
2736 }
2737
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002738 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002739 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002740 if (rule->kw->flags & KWF_EXPERIMENTAL)
2741 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2742
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002743 /* Always call the action function if defined */
2744 if (rule->action_ptr) {
2745 if ((s->req.flags & CF_READ_ERROR) ||
2746 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2747 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002748 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002749
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002750 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002751 case ACT_RET_CONT:
2752 break;
2753 case ACT_RET_STOP:
2754 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002755 s->last_rule_file = rule->conf.file;
2756 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002757 goto end;
2758 case ACT_RET_YIELD:
2759 s->current_rule = rule;
2760 rule_ret = HTTP_RULE_RES_YIELD;
2761 goto end;
2762 case ACT_RET_ERR:
2763 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002764 s->last_rule_file = rule->conf.file;
2765 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002766 goto end;
2767 case ACT_RET_DONE:
2768 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002769 s->last_rule_file = rule->conf.file;
2770 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002771 goto end;
2772 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002773 if (txn->status == -1)
2774 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002775 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002776 s->last_rule_file = rule->conf.file;
2777 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002778 goto end;
2779 case ACT_RET_ABRT:
2780 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002781 s->last_rule_file = rule->conf.file;
2782 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002783 goto end;
2784 case ACT_RET_INV:
2785 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002786 s->last_rule_file = rule->conf.file;
2787 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002788 goto end;
2789 }
2790 continue; /* eval the next rule */
2791 }
2792
2793 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002794 switch (rule->action) {
2795 case ACT_ACTION_ALLOW:
2796 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002797 s->last_rule_file = rule->conf.file;
2798 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002799 goto end;
2800
2801 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002802 txn->status = rule->arg.http_reply->status;
2803 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002804 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002805 s->last_rule_file = rule->conf.file;
2806 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002807 goto end;
2808
2809 case ACT_HTTP_REQ_TARPIT:
2810 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002811 txn->status = rule->arg.http_reply->status;
2812 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002813 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002814 s->last_rule_file = rule->conf.file;
2815 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002816 goto end;
2817
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002818 case ACT_HTTP_REDIR: {
2819 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2820
2821 if (ret == 2) // 2 == skip
2822 break;
2823
2824 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002825 s->last_rule_file = rule->conf.file;
2826 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002827 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002828 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002829
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002830 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002831 default:
2832 break;
2833 }
2834 }
2835
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002836 if (def_rules && s->current_rule_list == def_rules) {
2837 s->current_rule_list = rules;
2838 goto restart;
2839 }
2840
Christopher Faulet3e964192018-10-24 11:39:23 +02002841 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002842 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002843 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002844 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002845
Christopher Faulet3e964192018-10-24 11:39:23 +02002846 /* we reached the end of the rules, nothing to report */
2847 return rule_ret;
2848}
2849
2850/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2851 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2852 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2853 * is returned, the process can continue the evaluation of next rule list. If
2854 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2855 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002856 * must be returned. If *YIELD is returned, the caller must call again the
2857 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002858 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002859static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2860 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002861{
2862 struct session *sess = strm_sess(s);
2863 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002864 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002865 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002866 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002867
Christopher Faulet3e964192018-10-24 11:39:23 +02002868 /* If "the current_rule_list" match the executed rule list, we are in
2869 * resume condition. If a resume is needed it is always in the action
2870 * and never in the ACL or converters. In this case, we initialise the
2871 * current rule, and go to the action execution point.
2872 */
2873 if (s->current_rule) {
2874 rule = s->current_rule;
2875 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002876 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002877 goto resume_execution;
2878 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002879 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2880
2881 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002882
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002883 /* start the ruleset evaluation in strict mode */
2884 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002885
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002886 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002887 /* check optional condition */
2888 if (rule->cond) {
2889 int ret;
2890
2891 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2892 ret = acl_pass(ret);
2893
2894 if (rule->cond->pol == ACL_COND_UNLESS)
2895 ret = !ret;
2896
2897 if (!ret) /* condition not matched */
2898 continue;
2899 }
2900
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002901 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002902resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002903 if (rule->kw->flags & KWF_EXPERIMENTAL)
2904 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002905
2906 /* Always call the action function if defined */
2907 if (rule->action_ptr) {
2908 if ((s->req.flags & CF_READ_ERROR) ||
2909 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2910 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002911 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002912
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002913 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002914 case ACT_RET_CONT:
2915 break;
2916 case ACT_RET_STOP:
2917 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002918 s->last_rule_file = rule->conf.file;
2919 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002920 goto end;
2921 case ACT_RET_YIELD:
2922 s->current_rule = rule;
2923 rule_ret = HTTP_RULE_RES_YIELD;
2924 goto end;
2925 case ACT_RET_ERR:
2926 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002927 s->last_rule_file = rule->conf.file;
2928 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002929 goto end;
2930 case ACT_RET_DONE:
2931 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002932 s->last_rule_file = rule->conf.file;
2933 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002934 goto end;
2935 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002936 if (txn->status == -1)
2937 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002938 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002939 s->last_rule_file = rule->conf.file;
2940 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002941 goto end;
2942 case ACT_RET_ABRT:
2943 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002944 s->last_rule_file = rule->conf.file;
2945 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002946 goto end;
2947 case ACT_RET_INV:
2948 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002949 s->last_rule_file = rule->conf.file;
2950 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002951 goto end;
2952 }
2953 continue; /* eval the next rule */
2954 }
2955
2956 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002957 switch (rule->action) {
2958 case ACT_ACTION_ALLOW:
2959 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01002960 s->last_rule_file = rule->conf.file;
2961 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002962 goto end;
2963
2964 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002965 txn->status = rule->arg.http_reply->status;
2966 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002967 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002968 s->last_rule_file = rule->conf.file;
2969 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002970 goto end;
2971
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002972 case ACT_HTTP_REDIR: {
2973 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02002974
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002975 if (ret == 2) // 2 == skip
2976 break;
2977
2978 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002979 s->last_rule_file = rule->conf.file;
2980 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002981 goto end;
2982 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002983 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002984 default:
2985 break;
2986 }
2987 }
2988
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002989 if (def_rules && s->current_rule_list == def_rules) {
2990 s->current_rule_list = rules;
2991 goto restart;
2992 }
2993
Christopher Faulet3e964192018-10-24 11:39:23 +02002994 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002995 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002996 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002997 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002998
Christopher Faulet3e964192018-10-24 11:39:23 +02002999 /* we reached the end of the rules, nothing to report */
3000 return rule_ret;
3001}
3002
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003003/* Executes backend and frontend http-after-response rules for the stream <s>,
3004 * in that order. it return 1 on success and 0 on error. It is the caller
3005 * responsibility to catch error or ignore it. If it catches it, this function
3006 * may be called a second time, for the internal error.
3007 */
3008int http_eval_after_res_rules(struct stream *s)
3009{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003010 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003011 struct session *sess = s->sess;
3012 enum rule_result ret = HTTP_RULE_RES_CONT;
3013
Christopher Faulet507479b2020-05-15 12:29:46 +02003014 /* Eval after-response ruleset only if the reply is not const */
3015 if (s->txn->flags & TX_CONST_REPLY)
3016 goto end;
3017
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003018 /* prune the request variables if not already done and swap to the response variables. */
3019 if (s->vars_reqres.scope != SCOPE_RES) {
3020 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3021 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02003022 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003023 }
3024
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003025 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
3026 rules = &s->be->http_after_res_rules;
3027
3028 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01003029 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02003030 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
3031 rules = &sess->fe->http_after_res_rules;
3032 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
3033 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003034
Christopher Faulet507479b2020-05-15 12:29:46 +02003035 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003036 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3037 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3038}
3039
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003040/*
3041 * Manage client-side cookie. It can impact performance by about 2% so it is
3042 * desirable to call it only when needed. This code is quite complex because
3043 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3044 * highly recommended not to touch this part without a good reason !
3045 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003046static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003047{
3048 struct session *sess = s->sess;
3049 struct http_txn *txn = s->txn;
3050 struct htx *htx;
3051 struct http_hdr_ctx ctx;
3052 char *hdr_beg, *hdr_end, *del_from;
3053 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3054 int preserve_hdr;
3055
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003056 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003057 ctx.blk = NULL;
3058 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003059 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003060 del_from = NULL; /* nothing to be deleted */
3061 preserve_hdr = 0; /* assume we may kill the whole header */
3062
3063 /* Now look for cookies. Conforming to RFC2109, we have to support
3064 * attributes whose name begin with a '$', and associate them with
3065 * the right cookie, if we want to delete this cookie.
3066 * So there are 3 cases for each cookie read :
3067 * 1) it's a special attribute, beginning with a '$' : ignore it.
3068 * 2) it's a server id cookie that we *MAY* want to delete : save
3069 * some pointers on it (last semi-colon, beginning of cookie...)
3070 * 3) it's an application cookie : we *MAY* have to delete a previous
3071 * "special" cookie.
3072 * At the end of loop, if a "special" cookie remains, we may have to
3073 * remove it. If no application cookie persists in the header, we
3074 * *MUST* delete it.
3075 *
3076 * Note: RFC2965 is unclear about the processing of spaces around
3077 * the equal sign in the ATTR=VALUE form. A careful inspection of
3078 * the RFC explicitly allows spaces before it, and not within the
3079 * tokens (attrs or values). An inspection of RFC2109 allows that
3080 * too but section 10.1.3 lets one think that spaces may be allowed
3081 * after the equal sign too, resulting in some (rare) buggy
3082 * implementations trying to do that. So let's do what servers do.
3083 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3084 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003085 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003086 * causes parsing to become ambiguous. Browsers also allow spaces
3087 * within values even without quotes.
3088 *
3089 * We have to keep multiple pointers in order to support cookie
3090 * removal at the beginning, middle or end of header without
3091 * corrupting the header. All of these headers are valid :
3092 *
3093 * hdr_beg hdr_end
3094 * | |
3095 * v |
3096 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3097 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3098 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3099 * | | | | | | |
3100 * | | | | | | |
3101 * | | | | | | +--> next
3102 * | | | | | +----> val_end
3103 * | | | | +-----------> val_beg
3104 * | | | +--------------> equal
3105 * | | +----------------> att_end
3106 * | +---------------------> att_beg
3107 * +--------------------------> prev
3108 *
3109 */
3110 hdr_beg = ctx.value.ptr;
3111 hdr_end = hdr_beg + ctx.value.len;
3112 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3113 /* Iterate through all cookies on this line */
3114
3115 /* find att_beg */
3116 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003117 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003118 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003119 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003120
3121 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3122 att_beg++;
3123
3124 /* find att_end : this is the first character after the last non
3125 * space before the equal. It may be equal to hdr_end.
3126 */
3127 equal = att_end = att_beg;
3128 while (equal < hdr_end) {
3129 if (*equal == '=' || *equal == ',' || *equal == ';')
3130 break;
3131 if (HTTP_IS_SPHT(*equal++))
3132 continue;
3133 att_end = equal;
3134 }
3135
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003136 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003137 * is between <att_beg> and <equal>, both may be identical.
3138 */
3139 /* look for end of cookie if there is an equal sign */
3140 if (equal < hdr_end && *equal == '=') {
3141 /* look for the beginning of the value */
3142 val_beg = equal + 1;
3143 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3144 val_beg++;
3145
3146 /* find the end of the value, respecting quotes */
3147 next = http_find_cookie_value_end(val_beg, hdr_end);
3148
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003149 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003150 val_end = next;
3151 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3152 val_end--;
3153 }
3154 else
3155 val_beg = val_end = next = equal;
3156
3157 /* We have nothing to do with attributes beginning with
3158 * '$'. However, they will automatically be removed if a
3159 * header before them is removed, since they're supposed
3160 * to be linked together.
3161 */
3162 if (*att_beg == '$')
3163 continue;
3164
3165 /* Ignore cookies with no equal sign */
3166 if (equal == next) {
3167 /* This is not our cookie, so we must preserve it. But if we already
3168 * scheduled another cookie for removal, we cannot remove the
3169 * complete header, but we can remove the previous block itself.
3170 */
3171 preserve_hdr = 1;
3172 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003173 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003174 val_end += delta;
3175 next += delta;
3176 hdr_end += delta;
3177 prev = del_from;
3178 del_from = NULL;
3179 }
3180 continue;
3181 }
3182
3183 /* if there are spaces around the equal sign, we need to
3184 * strip them otherwise we'll get trouble for cookie captures,
3185 * or even for rewrites. Since this happens extremely rarely,
3186 * it does not hurt performance.
3187 */
3188 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3189 int stripped_before = 0;
3190 int stripped_after = 0;
3191
3192 if (att_end != equal) {
3193 memmove(att_end, equal, hdr_end - equal);
3194 stripped_before = (att_end - equal);
3195 equal += stripped_before;
3196 val_beg += stripped_before;
3197 }
3198
3199 if (val_beg > equal + 1) {
3200 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3201 stripped_after = (equal + 1) - val_beg;
3202 val_beg += stripped_after;
3203 stripped_before += stripped_after;
3204 }
3205
3206 val_end += stripped_before;
3207 next += stripped_before;
3208 hdr_end += stripped_before;
3209 }
3210 /* now everything is as on the diagram above */
3211
3212 /* First, let's see if we want to capture this cookie. We check
3213 * that we don't already have a client side cookie, because we
3214 * can only capture one. Also as an optimisation, we ignore
3215 * cookies shorter than the declared name.
3216 */
3217 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3218 (val_end - att_beg >= sess->fe->capture_namelen) &&
3219 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3220 int log_len = val_end - att_beg;
3221
3222 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3223 ha_alert("HTTP logging : out of memory.\n");
3224 } else {
3225 if (log_len > sess->fe->capture_len)
3226 log_len = sess->fe->capture_len;
3227 memcpy(txn->cli_cookie, att_beg, log_len);
3228 txn->cli_cookie[log_len] = 0;
3229 }
3230 }
3231
3232 /* Persistence cookies in passive, rewrite or insert mode have the
3233 * following form :
3234 *
3235 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3236 *
3237 * For cookies in prefix mode, the form is :
3238 *
3239 * Cookie: NAME=SRV~VALUE
3240 */
3241 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3242 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3243 struct server *srv = s->be->srv;
3244 char *delim;
3245
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003246 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003247 * have the server ID between val_beg and delim, and the original cookie between
3248 * delim+1 and val_end. Otherwise, delim==val_end :
3249 *
3250 * hdr_beg
3251 * |
3252 * v
3253 * NAME=SRV; # in all but prefix modes
3254 * NAME=SRV~OPAQUE ; # in prefix mode
3255 * || || | |+-> next
3256 * || || | +--> val_end
3257 * || || +---------> delim
3258 * || |+------------> val_beg
3259 * || +-------------> att_end = equal
3260 * |+-----------------> att_beg
3261 * +------------------> prev
3262 *
3263 */
3264 if (s->be->ck_opts & PR_CK_PFX) {
3265 for (delim = val_beg; delim < val_end; delim++)
3266 if (*delim == COOKIE_DELIM)
3267 break;
3268 }
3269 else {
3270 char *vbar1;
3271 delim = val_end;
3272 /* Now check if the cookie contains a date field, which would
3273 * appear after a vertical bar ('|') just after the server name
3274 * and before the delimiter.
3275 */
3276 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3277 if (vbar1) {
3278 /* OK, so left of the bar is the server's cookie and
3279 * right is the last seen date. It is a base64 encoded
3280 * 30-bit value representing the UNIX date since the
3281 * epoch in 4-second quantities.
3282 */
3283 int val;
3284 delim = vbar1++;
3285 if (val_end - vbar1 >= 5) {
3286 val = b64tos30(vbar1);
3287 if (val > 0)
3288 txn->cookie_last_date = val << 2;
3289 }
3290 /* look for a second vertical bar */
3291 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3292 if (vbar1 && (val_end - vbar1 > 5)) {
3293 val = b64tos30(vbar1 + 1);
3294 if (val > 0)
3295 txn->cookie_first_date = val << 2;
3296 }
3297 }
3298 }
3299
3300 /* if the cookie has an expiration date and the proxy wants to check
3301 * it, then we do that now. We first check if the cookie is too old,
3302 * then only if it has expired. We detect strict overflow because the
3303 * time resolution here is not great (4 seconds). Cookies with dates
3304 * in the future are ignored if their offset is beyond one day. This
3305 * allows an admin to fix timezone issues without expiring everyone
3306 * and at the same time avoids keeping unwanted side effects for too
3307 * long.
3308 */
3309 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3310 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3311 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3312 txn->flags &= ~TX_CK_MASK;
3313 txn->flags |= TX_CK_OLD;
3314 delim = val_beg; // let's pretend we have not found the cookie
3315 txn->cookie_first_date = 0;
3316 txn->cookie_last_date = 0;
3317 }
3318 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3319 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3320 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3321 txn->flags &= ~TX_CK_MASK;
3322 txn->flags |= TX_CK_EXPIRED;
3323 delim = val_beg; // let's pretend we have not found the cookie
3324 txn->cookie_first_date = 0;
3325 txn->cookie_last_date = 0;
3326 }
3327
3328 /* Here, we'll look for the first running server which supports the cookie.
3329 * This allows to share a same cookie between several servers, for example
3330 * to dedicate backup servers to specific servers only.
3331 * However, to prevent clients from sticking to cookie-less backup server
3332 * when they have incidentely learned an empty cookie, we simply ignore
3333 * empty cookies and mark them as invalid.
3334 * The same behaviour is applied when persistence must be ignored.
3335 */
3336 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3337 srv = NULL;
3338
3339 while (srv) {
3340 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3341 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3342 if ((srv->cur_state != SRV_ST_STOPPED) ||
3343 (s->be->options & PR_O_PERSIST) ||
3344 (s->flags & SF_FORCE_PRST)) {
3345 /* we found the server and we can use it */
3346 txn->flags &= ~TX_CK_MASK;
3347 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3348 s->flags |= SF_DIRECT | SF_ASSIGNED;
3349 s->target = &srv->obj_type;
3350 break;
3351 } else {
3352 /* we found a server, but it's down,
3353 * mark it as such and go on in case
3354 * another one is available.
3355 */
3356 txn->flags &= ~TX_CK_MASK;
3357 txn->flags |= TX_CK_DOWN;
3358 }
3359 }
3360 srv = srv->next;
3361 }
3362
3363 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3364 /* no server matched this cookie or we deliberately skipped it */
3365 txn->flags &= ~TX_CK_MASK;
3366 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3367 txn->flags |= TX_CK_UNUSED;
3368 else
3369 txn->flags |= TX_CK_INVALID;
3370 }
3371
3372 /* depending on the cookie mode, we may have to either :
3373 * - delete the complete cookie if we're in insert+indirect mode, so that
3374 * the server never sees it ;
3375 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003376 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003377 * if we're in cookie prefix mode
3378 */
3379 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3380 int delta; /* negative */
3381
3382 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3383 delta = val_beg - (delim + 1);
3384 val_end += delta;
3385 next += delta;
3386 hdr_end += delta;
3387 del_from = NULL;
3388 preserve_hdr = 1; /* we want to keep this cookie */
3389 }
3390 else if (del_from == NULL &&
3391 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3392 del_from = prev;
3393 }
3394 }
3395 else {
3396 /* This is not our cookie, so we must preserve it. But if we already
3397 * scheduled another cookie for removal, we cannot remove the
3398 * complete header, but we can remove the previous block itself.
3399 */
3400 preserve_hdr = 1;
3401
3402 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003403 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003404 if (att_beg >= del_from)
3405 att_beg += delta;
3406 if (att_end >= del_from)
3407 att_end += delta;
3408 val_beg += delta;
3409 val_end += delta;
3410 next += delta;
3411 hdr_end += delta;
3412 prev = del_from;
3413 del_from = NULL;
3414 }
3415 }
3416
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003417 } /* for each cookie */
3418
3419
3420 /* There are no more cookies on this line.
3421 * We may still have one (or several) marked for deletion at the
3422 * end of the line. We must do this now in two ways :
3423 * - if some cookies must be preserved, we only delete from the
3424 * mark to the end of line ;
3425 * - if nothing needs to be preserved, simply delete the whole header
3426 */
3427 if (del_from) {
3428 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3429 }
3430 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003431 if (hdr_beg != hdr_end)
3432 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003433 else
3434 http_remove_header(htx, &ctx);
3435 }
3436 } /* for each "Cookie header */
3437}
3438
3439/*
3440 * Manage server-side cookies. It can impact performance by about 2% so it is
3441 * desirable to call it only when needed. This function is also used when we
3442 * just need to know if there is a cookie (eg: for check-cache).
3443 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003444static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003445{
3446 struct session *sess = s->sess;
3447 struct http_txn *txn = s->txn;
3448 struct htx *htx;
3449 struct http_hdr_ctx ctx;
3450 struct server *srv;
3451 char *hdr_beg, *hdr_end;
3452 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003453 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003454
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003455 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003456
3457 ctx.blk = NULL;
3458 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003459 int is_first = 1;
3460
Andrew McDermottbfb15ab2022-02-11 18:26:49 +00003461 if (is_cookie2 || !http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003462 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3463 break;
3464 is_cookie2 = 1;
3465 }
3466
3467 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3468 * <prev> points to the colon.
3469 */
3470 txn->flags |= TX_SCK_PRESENT;
3471
3472 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3473 * check-cache is enabled) and we are not interested in checking
3474 * them. Warning, the cookie capture is declared in the frontend.
3475 */
3476 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3477 break;
3478
3479 /* OK so now we know we have to process this response cookie.
3480 * The format of the Set-Cookie header is slightly different
3481 * from the format of the Cookie header in that it does not
3482 * support the comma as a cookie delimiter (thus the header
3483 * cannot be folded) because the Expires attribute described in
3484 * the original Netscape's spec may contain an unquoted date
3485 * with a comma inside. We have to live with this because
3486 * many browsers don't support Max-Age and some browsers don't
3487 * support quoted strings. However the Set-Cookie2 header is
3488 * clean.
3489 *
3490 * We have to keep multiple pointers in order to support cookie
3491 * removal at the beginning, middle or end of header without
3492 * corrupting the header (in case of set-cookie2). A special
3493 * pointer, <scav> points to the beginning of the set-cookie-av
3494 * fields after the first semi-colon. The <next> pointer points
3495 * either to the end of line (set-cookie) or next unquoted comma
3496 * (set-cookie2). All of these headers are valid :
3497 *
3498 * hdr_beg hdr_end
3499 * | |
3500 * v |
3501 * NAME1 = VALUE 1 ; Secure; Path="/" |
3502 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3503 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3504 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3505 * | | | | | | | |
3506 * | | | | | | | +-> next
3507 * | | | | | | +------------> scav
3508 * | | | | | +--------------> val_end
3509 * | | | | +--------------------> val_beg
3510 * | | | +----------------------> equal
3511 * | | +------------------------> att_end
3512 * | +----------------------------> att_beg
3513 * +------------------------------> prev
3514 * -------------------------------> hdr_beg
3515 */
3516 hdr_beg = ctx.value.ptr;
3517 hdr_end = hdr_beg + ctx.value.len;
3518 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3519
3520 /* Iterate through all cookies on this line */
3521
3522 /* find att_beg */
3523 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003524 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003525 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003526 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003527
3528 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3529 att_beg++;
3530
3531 /* find att_end : this is the first character after the last non
3532 * space before the equal. It may be equal to hdr_end.
3533 */
3534 equal = att_end = att_beg;
3535
3536 while (equal < hdr_end) {
3537 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3538 break;
3539 if (HTTP_IS_SPHT(*equal++))
3540 continue;
3541 att_end = equal;
3542 }
3543
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003544 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003545 * is between <att_beg> and <equal>, both may be identical.
3546 */
3547
3548 /* look for end of cookie if there is an equal sign */
3549 if (equal < hdr_end && *equal == '=') {
3550 /* look for the beginning of the value */
3551 val_beg = equal + 1;
3552 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3553 val_beg++;
3554
3555 /* find the end of the value, respecting quotes */
3556 next = http_find_cookie_value_end(val_beg, hdr_end);
3557
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003558 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003559 val_end = next;
3560 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3561 val_end--;
3562 }
3563 else {
3564 /* <equal> points to next comma, semi-colon or EOL */
3565 val_beg = val_end = next = equal;
3566 }
3567
3568 if (next < hdr_end) {
3569 /* Set-Cookie2 supports multiple cookies, and <next> points to
3570 * a colon or semi-colon before the end. So skip all attr-value
3571 * pairs and look for the next comma. For Set-Cookie, since
3572 * commas are permitted in values, skip to the end.
3573 */
3574 if (is_cookie2)
3575 next = http_find_hdr_value_end(next, hdr_end);
3576 else
3577 next = hdr_end;
3578 }
3579
3580 /* Now everything is as on the diagram above */
3581
3582 /* Ignore cookies with no equal sign */
3583 if (equal == val_end)
3584 continue;
3585
3586 /* If there are spaces around the equal sign, we need to
3587 * strip them otherwise we'll get trouble for cookie captures,
3588 * or even for rewrites. Since this happens extremely rarely,
3589 * it does not hurt performance.
3590 */
3591 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3592 int stripped_before = 0;
3593 int stripped_after = 0;
3594
3595 if (att_end != equal) {
3596 memmove(att_end, equal, hdr_end - equal);
3597 stripped_before = (att_end - equal);
3598 equal += stripped_before;
3599 val_beg += stripped_before;
3600 }
3601
3602 if (val_beg > equal + 1) {
3603 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3604 stripped_after = (equal + 1) - val_beg;
3605 val_beg += stripped_after;
3606 stripped_before += stripped_after;
3607 }
3608
3609 val_end += stripped_before;
3610 next += stripped_before;
3611 hdr_end += stripped_before;
3612
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003613 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003614 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003615 }
3616
3617 /* First, let's see if we want to capture this cookie. We check
3618 * that we don't already have a server side cookie, because we
3619 * can only capture one. Also as an optimisation, we ignore
3620 * cookies shorter than the declared name.
3621 */
3622 if (sess->fe->capture_name != NULL &&
3623 txn->srv_cookie == NULL &&
3624 (val_end - att_beg >= sess->fe->capture_namelen) &&
3625 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3626 int log_len = val_end - att_beg;
3627 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3628 ha_alert("HTTP logging : out of memory.\n");
3629 }
3630 else {
3631 if (log_len > sess->fe->capture_len)
3632 log_len = sess->fe->capture_len;
3633 memcpy(txn->srv_cookie, att_beg, log_len);
3634 txn->srv_cookie[log_len] = 0;
3635 }
3636 }
3637
3638 srv = objt_server(s->target);
3639 /* now check if we need to process it for persistence */
3640 if (!(s->flags & SF_IGNORE_PRST) &&
3641 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3642 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3643 /* assume passive cookie by default */
3644 txn->flags &= ~TX_SCK_MASK;
3645 txn->flags |= TX_SCK_FOUND;
3646
3647 /* If the cookie is in insert mode on a known server, we'll delete
3648 * this occurrence because we'll insert another one later.
3649 * We'll delete it too if the "indirect" option is set and we're in
3650 * a direct access.
3651 */
3652 if (s->be->ck_opts & PR_CK_PSV) {
3653 /* The "preserve" flag was set, we don't want to touch the
3654 * server's cookie.
3655 */
3656 }
3657 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3658 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3659 /* this cookie must be deleted */
3660 if (prev == hdr_beg && next == hdr_end) {
3661 /* whole header */
3662 http_remove_header(htx, &ctx);
3663 /* note: while both invalid now, <next> and <hdr_end>
3664 * are still equal, so the for() will stop as expected.
3665 */
3666 } else {
3667 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003668 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003669 next = prev;
3670 hdr_end += delta;
3671 }
3672 txn->flags &= ~TX_SCK_MASK;
3673 txn->flags |= TX_SCK_DELETED;
3674 /* and go on with next cookie */
3675 }
3676 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3677 /* replace bytes val_beg->val_end with the cookie name associated
3678 * with this server since we know it.
3679 */
3680 int sliding, delta;
3681
3682 ctx.value = ist2(val_beg, val_end - val_beg);
3683 ctx.lws_before = ctx.lws_after = 0;
3684 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3685 delta = srv->cklen - (val_end - val_beg);
3686 sliding = (ctx.value.ptr - val_beg);
3687 hdr_beg += sliding;
3688 val_beg += sliding;
3689 next += sliding + delta;
3690 hdr_end += sliding + delta;
3691
3692 txn->flags &= ~TX_SCK_MASK;
3693 txn->flags |= TX_SCK_REPLACED;
3694 }
3695 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3696 /* insert the cookie name associated with this server
3697 * before existing cookie, and insert a delimiter between them..
3698 */
3699 int sliding, delta;
3700 ctx.value = ist2(val_beg, 0);
3701 ctx.lws_before = ctx.lws_after = 0;
3702 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3703 delta = srv->cklen + 1;
3704 sliding = (ctx.value.ptr - val_beg);
3705 hdr_beg += sliding;
3706 val_beg += sliding;
3707 next += sliding + delta;
3708 hdr_end += sliding + delta;
3709
3710 val_beg[srv->cklen] = COOKIE_DELIM;
3711 txn->flags &= ~TX_SCK_MASK;
3712 txn->flags |= TX_SCK_REPLACED;
3713 }
3714 }
3715 /* that's done for this cookie, check the next one on the same
3716 * line when next != hdr_end (only if is_cookie2).
3717 */
3718 }
3719 }
3720}
3721
Christopher Faulet25a02f62018-10-24 12:00:25 +02003722/*
3723 * Parses the Cache-Control and Pragma request header fields to determine if
3724 * the request may be served from the cache and/or if it is cacheable. Updates
3725 * s->txn->flags.
3726 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003727void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003728{
3729 struct http_txn *txn = s->txn;
3730 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003731 struct http_hdr_ctx ctx = { .blk = NULL };
3732 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003733
3734 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3735 return; /* nothing more to do here */
3736
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003737 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003738 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003739
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003740 /* Check "pragma" header for HTTP/1.0 compatibility. */
3741 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3742 if (isteqi(ctx.value, ist("no-cache"))) {
3743 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003744 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003745 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003746
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003747 ctx.blk = NULL;
3748 /* Don't use the cache and don't try to store if we found the
3749 * Authorization header */
3750 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3751 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3752 txn->flags |= TX_CACHE_IGNORE;
3753 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003754
Christopher Faulet25a02f62018-10-24 12:00:25 +02003755
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003756 /* Look for "cache-control" header and iterate over all the values
3757 * until we find one that specifies that caching is possible or not. */
3758 ctx.blk = NULL;
3759 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003760 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003761 /* We don't check the values after max-age, max-stale nor min-fresh,
3762 * we simply don't use the cache when they're specified. */
3763 if (istmatchi(ctx.value, ist("max-age")) ||
3764 istmatchi(ctx.value, ist("no-cache")) ||
3765 istmatchi(ctx.value, ist("max-stale")) ||
3766 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003767 txn->flags |= TX_CACHE_IGNORE;
3768 continue;
3769 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003770 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003771 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3772 continue;
3773 }
3774 }
3775
3776 /* RFC7234#5.4:
3777 * When the Cache-Control header field is also present and
3778 * understood in a request, Pragma is ignored.
3779 * When the Cache-Control header field is not present in a
3780 * request, caches MUST consider the no-cache request
3781 * pragma-directive as having the same effect as if
3782 * "Cache-Control: no-cache" were present.
3783 */
3784 if (!cc_found && pragma_found)
3785 txn->flags |= TX_CACHE_IGNORE;
3786}
3787
3788/*
3789 * Check if response is cacheable or not. Updates s->txn->flags.
3790 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003791void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003792{
3793 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003794 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003795 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003796 int has_freshness_info = 0;
3797 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003798
3799 if (txn->status < 200) {
3800 /* do not try to cache interim responses! */
3801 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3802 return;
3803 }
3804
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003805 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003806 /* Check "pragma" header for HTTP/1.0 compatibility. */
3807 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3808 if (isteqi(ctx.value, ist("no-cache"))) {
3809 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3810 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003811 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003812 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003813
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003814 /* Look for "cache-control" header and iterate over all the values
3815 * until we find one that specifies that caching is possible or not. */
3816 ctx.blk = NULL;
3817 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3818 if (isteqi(ctx.value, ist("public"))) {
3819 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003820 continue;
3821 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003822 if (isteqi(ctx.value, ist("private")) ||
3823 isteqi(ctx.value, ist("no-cache")) ||
3824 isteqi(ctx.value, ist("no-store")) ||
3825 isteqi(ctx.value, ist("max-age=0")) ||
3826 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003827 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003828 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003829 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003830 /* We might have a no-cache="set-cookie" form. */
3831 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3832 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003833 continue;
3834 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003835
3836 if (istmatchi(ctx.value, ist("s-maxage")) ||
3837 istmatchi(ctx.value, ist("max-age"))) {
3838 has_freshness_info = 1;
3839 continue;
3840 }
3841 }
3842
3843 /* If no freshness information could be found in Cache-Control values,
3844 * look for an Expires header. */
3845 if (!has_freshness_info) {
3846 ctx.blk = NULL;
3847 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003848 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003849
3850 /* If no freshness information could be found in Cache-Control or Expires
3851 * values, look for an explicit validator. */
3852 if (!has_freshness_info) {
3853 ctx.blk = NULL;
3854 has_validator = 1;
3855 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3856 ctx.blk = NULL;
3857 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3858 has_validator = 0;
3859 }
3860 }
3861
3862 /* We won't store an entry that has neither a cache validator nor an
3863 * explicit expiration time, as suggested in RFC 7234#3. */
3864 if (!has_freshness_info && !has_validator)
3865 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003866}
3867
Christopher Faulet377c5a52018-10-24 21:21:30 +02003868/*
3869 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3870 * for the current backend.
3871 *
3872 * It is assumed that the request is either a HEAD, GET, or POST and that the
3873 * uri_auth field is valid.
3874 *
3875 * Returns 1 if stats should be provided, otherwise 0.
3876 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003877static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003878{
3879 struct uri_auth *uri_auth = backend->uri_auth;
3880 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003881 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003882 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003883
3884 if (!uri_auth)
3885 return 0;
3886
3887 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3888 return 0;
3889
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003890 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003891 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003892 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003893 if (*uri_auth->uri_prefix == '/') {
3894 struct http_uri_parser parser = http_uri_parser_init(uri);
3895 uri = http_parse_path(&parser);
3896 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003897
3898 /* check URI size */
3899 if (uri_auth->uri_len > uri.len)
3900 return 0;
3901
3902 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3903 return 0;
3904
3905 return 1;
3906}
3907
3908/* This function prepares an applet to handle the stats. It can deal with the
3909 * "100-continue" expectation, check that admin rules are met for POST requests,
3910 * and program a response message if something was unexpected. It cannot fail
3911 * and always relies on the stats applet to complete the job. It does not touch
3912 * analysers nor counters, which are left to the caller. It does not touch
3913 * s->target which is supposed to already point to the stats applet. The caller
3914 * is expected to have already assigned an appctx to the stream.
3915 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003916static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003917{
3918 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003919 struct session *sess = s->sess;
3920 struct http_txn *txn = s->txn;
3921 struct http_msg *msg = &txn->req;
3922 struct uri_auth *uri_auth = s->be->uri_auth;
3923 const char *h, *lookup, *end;
Christopher Faulet693b23b2022-02-28 09:09:05 +01003924 struct appctx *appctx = __cs_appctx(s->csb);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003925 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003926 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003927
Christopher Faulet377c5a52018-10-24 21:21:30 +02003928 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3929 appctx->st1 = appctx->st2 = 0;
3930 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02003931 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003932 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
3933 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
3934 appctx->ctx.stats.flags |= STAT_CHUNKED;
3935
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003936 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003937 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003938 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3939 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003940
3941 for (h = lookup; h <= end - 3; h++) {
3942 if (memcmp(h, ";up", 3) == 0) {
3943 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
3944 break;
3945 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003946 }
3947
3948 for (h = lookup; h <= end - 9; h++) {
3949 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02003950 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
3951 break;
3952 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003953 }
3954
3955 if (uri_auth->refresh) {
3956 for (h = lookup; h <= end - 10; h++) {
3957 if (memcmp(h, ";norefresh", 10) == 0) {
3958 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
3959 break;
3960 }
3961 }
3962 }
3963
3964 for (h = lookup; h <= end - 4; h++) {
3965 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003966 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003967 break;
3968 }
3969 }
3970
3971 for (h = lookup; h <= end - 6; h++) {
3972 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02003973 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003974 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3975 break;
3976 }
3977 }
3978
Christopher Faulet6338a082019-09-09 15:50:54 +02003979 for (h = lookup; h <= end - 5; h++) {
3980 if (memcmp(h, ";json", 5) == 0) {
3981 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3982 appctx->ctx.stats.flags |= STAT_FMT_JSON;
3983 break;
3984 }
3985 }
3986
3987 for (h = lookup; h <= end - 12; h++) {
3988 if (memcmp(h, ";json-schema", 12) == 0) {
3989 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
3990 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
3991 break;
3992 }
3993 }
3994
Christopher Faulet377c5a52018-10-24 21:21:30 +02003995 for (h = lookup; h <= end - 8; h++) {
3996 if (memcmp(h, ";st=", 4) == 0) {
3997 int i;
3998 h += 4;
3999 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4000 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4001 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4002 appctx->ctx.stats.st_code = i;
4003 break;
4004 }
4005 }
4006 break;
4007 }
4008 }
4009
4010 appctx->ctx.stats.scope_str = 0;
4011 appctx->ctx.stats.scope_len = 0;
4012 for (h = lookup; h <= end - 8; h++) {
4013 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4014 int itx = 0;
4015 const char *h2;
4016 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4017 const char *err;
4018
4019 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4020 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004021 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4022 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004023 if (*h == ';' || *h == '&' || *h == ' ')
4024 break;
4025 itx++;
4026 h++;
4027 }
4028
4029 if (itx > STAT_SCOPE_TXT_MAXLEN)
4030 itx = STAT_SCOPE_TXT_MAXLEN;
4031 appctx->ctx.stats.scope_len = itx;
4032
4033 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4034 memcpy(scope_txt, h2, itx);
4035 scope_txt[itx] = '\0';
4036 err = invalid_char(scope_txt);
4037 if (err) {
4038 /* bad char in search text => clear scope */
4039 appctx->ctx.stats.scope_str = 0;
4040 appctx->ctx.stats.scope_len = 0;
4041 }
4042 break;
4043 }
4044 }
4045
4046 /* now check whether we have some admin rules for this request */
4047 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4048 int ret = 1;
4049
4050 if (stats_admin_rule->cond) {
4051 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4052 ret = acl_pass(ret);
4053 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4054 ret = !ret;
4055 }
4056
4057 if (ret) {
4058 /* no rule, or the rule matches */
4059 appctx->ctx.stats.flags |= STAT_ADMIN;
4060 break;
4061 }
4062 }
4063
Christopher Faulet5d45e382019-02-27 15:15:23 +01004064 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4065 appctx->st0 = STAT_HTTP_HEAD;
4066 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004067 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004068 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004069 if (msg->msg_state < HTTP_MSG_DATA)
4070 req->analysers |= AN_REQ_HTTP_BODY;
4071 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004072 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004073 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004074 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4075 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4076 appctx->st0 = STAT_HTTP_LAST;
4077 }
4078 }
4079 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004080 /* Unsupported method */
4081 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4082 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4083 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004084 }
4085
4086 s->task->nice = -32; /* small boost for HTTP statistics */
4087 return 1;
4088}
4089
Christopher Faulet021a8e42021-03-29 10:46:38 +02004090/* This function waits for the message payload at most <time> milliseconds (may
4091 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4092 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4093 * the result:
4094 *
4095 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4096 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004097 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004098 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004099 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004100 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004101 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004102 * to the client, depending on the channel (408 on request side, 504 on response
4103 * side). All other errors must be handled by the caller.
4104 */
4105enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4106 unsigned int time, unsigned int bytes)
4107{
4108 struct session *sess = s->sess;
4109 struct http_txn *txn = s->txn;
4110 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4111 struct htx *htx;
4112 enum rule_result ret = HTTP_RULE_RES_CONT;
4113
4114 htx = htxbuf(&chn->buf);
4115
4116 if (htx->flags & HTX_FL_PARSING_ERROR) {
4117 ret = HTTP_RULE_RES_BADREQ;
4118 goto end;
4119 }
4120 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4121 ret = HTTP_RULE_RES_ERROR;
4122 goto end;
4123 }
4124
4125 /* Do nothing for bodyless and CONNECT requests */
4126 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4127 goto end;
4128
4129 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4130 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4131 ret = HTTP_RULE_RES_ERROR;
4132 goto end;
4133 }
4134 }
4135
4136 msg->msg_state = HTTP_MSG_DATA;
4137
4138 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4139 * been received or if the buffer is full.
4140 */
Christopher Faulet78335962021-09-23 14:46:32 +02004141 if ((htx->flags & HTX_FL_EOM) ||
4142 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4143 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
Christopher Fauleta0bdec32022-04-04 07:51:21 +02004144 cs_rx_blocked_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004145 goto end;
4146
4147 if (bytes) {
4148 struct htx_blk *blk;
4149 unsigned int len = 0;
4150
4151 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4152 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4153 continue;
4154 len += htx_get_blksz(blk);
4155 if (len >= bytes)
4156 goto end;
4157 }
4158 }
4159
4160 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4161 if (!(chn->flags & CF_ISRESP))
4162 goto abort_req;
4163 goto abort_res;
4164 }
4165
4166 /* we get here if we need to wait for more data */
4167 if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) {
4168 if (!tick_isset(chn->analyse_exp))
4169 chn->analyse_exp = tick_add_ifset(now_ms, time);
4170 ret = HTTP_RULE_RES_YIELD;
4171 }
4172
4173 end:
4174 return ret;
4175
4176 abort_req:
4177 txn->status = 408;
4178 if (!(s->flags & SF_ERR_MASK))
4179 s->flags |= SF_ERR_CLITO;
4180 if (!(s->flags & SF_FINST_MASK))
4181 s->flags |= SF_FINST_D;
Willy Tarreau4781b152021-04-06 13:53:36 +02004182 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004183 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004184 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004185 http_reply_and_close(s, txn->status, http_error_message(s));
4186 ret = HTTP_RULE_RES_ABRT;
4187 goto end;
4188
4189 abort_res:
4190 txn->status = 504;
4191 if (!(s->flags & SF_ERR_MASK))
4192 s->flags |= SF_ERR_SRVTO;
4193 if (!(s->flags & SF_FINST_MASK))
4194 s->flags |= SF_FINST_D;
4195 stream_inc_http_fail_ctr(s);
4196 http_reply_and_close(s, txn->status, http_error_message(s));
4197 ret = HTTP_RULE_RES_ABRT;
4198 goto end;
4199}
4200
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004201void http_perform_server_redirect(struct stream *s, struct conn_stream *cs)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004202{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004203 struct channel *req = &s->req;
4204 struct channel *res = &s->res;
4205 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004206 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004207 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004208 struct ist path, location;
4209 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004210 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004211
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004212 /*
4213 * Create the location
4214 */
4215 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004216
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004217 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004218 /* special prefix "/" means don't change URL */
4219 srv = __objt_server(s->target);
4220 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4221 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4222 return;
4223 }
4224
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004225 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004226 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004227 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004228 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4229 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004230 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004231 return;
4232
4233 if (!chunk_memcat(&trash, path.ptr, path.len))
4234 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004235 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004236
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004237 /*
4238 * Create the 302 respone
4239 */
4240 htx = htx_from_buf(&res->buf);
4241 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4242 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4243 ist("HTTP/1.1"), ist("302"), ist("Found"));
4244 if (!sl)
4245 goto fail;
4246 sl->info.res.status = 302;
4247 s->txn->status = 302;
4248
4249 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4250 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4251 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4252 !htx_add_header(htx, ist("Location"), location))
4253 goto fail;
4254
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004255 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004256 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004257
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004258 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004259 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004260 if (!http_forward_proxy_resp(s, 1))
4261 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004262
4263 /* return without error. */
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004264 cs_shutr(cs);
4265 cs_shutw(cs);
Christopher Faulet50264b42022-03-30 19:39:30 +02004266 s->conn_err_type = STRM_ET_NONE;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004267 cs->state = CS_ST_CLO;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004268
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004269 if (!(s->flags & SF_ERR_MASK))
4270 s->flags |= SF_ERR_LOCAL;
4271 if (!(s->flags & SF_FINST_MASK))
4272 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004273
4274 /* FIXME: we should increase a counter of redirects per server and per backend. */
4275 srv_inc_sess_ctr(srv);
4276 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004277 return;
4278
4279 fail:
4280 /* If an error occurred, remove the incomplete HTTP response from the
4281 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004282 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004283}
4284
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004285/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004286 * because an error was triggered during the body forwarding.
4287 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004288static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004289{
4290 struct channel *chn = &s->req;
4291 struct http_txn *txn = s->txn;
4292
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004293 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004294
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004295 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4296 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004297 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004298 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004299 goto end;
4300 }
4301
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004302 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4303 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004304 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004305 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004306
4307 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004308 /* No need to read anymore, the request was completely parsed.
4309 * We can shut the read side unless we want to abort_on_close,
4310 * or we have a POST request. The issue with POST requests is
4311 * that some browsers still send a CRLF after the request, and
4312 * this CRLF must be read so that it does not remain in the kernel
4313 * buffers, otherwise a close could cause an RST on some systems
4314 * (eg: Linux).
4315 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004316 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004317 channel_dont_read(chn);
4318
4319 /* if the server closes the connection, we want to immediately react
4320 * and close the socket to save packets and syscalls.
4321 */
Christopher Faulet8abe7122022-03-30 15:10:18 +02004322 s->csb->flags |= CS_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004323
4324 /* In any case we've finished parsing the request so we must
4325 * disable Nagle when sending data because 1) we're not going
4326 * to shut this side, and 2) the server is waiting for us to
4327 * send pending data.
4328 */
4329 chn->flags |= CF_NEVER_WAIT;
4330
Christopher Fauletd01ce402019-01-02 17:44:13 +01004331 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4332 /* The server has not finished to respond, so we
4333 * don't want to move in order not to upset it.
4334 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004335 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004336 return;
4337 }
4338
Christopher Fauletf2824e62018-10-01 12:12:37 +02004339 /* When we get here, it means that both the request and the
4340 * response have finished receiving. Depending on the connection
4341 * mode, we'll have to wait for the last bytes to leave in either
4342 * direction, and sometimes for a close to be effective.
4343 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004344 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004345 /* Tunnel mode will not have any analyser so it needs to
4346 * poll for reads.
4347 */
4348 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004349 if (b_data(&chn->buf)) {
4350 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004351 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004352 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004353 txn->req.msg_state = HTTP_MSG_TUNNEL;
4354 }
4355 else {
4356 /* we're not expecting any new data to come for this
4357 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004358 *
4359 * However, there is an exception if the response
4360 * length is undefined. In this case, we need to wait
4361 * the close from the server. The response will be
4362 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004363 */
4364 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4365 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004366 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004367
4368 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4369 channel_shutr_now(chn);
4370 channel_shutw_now(chn);
4371 }
4372 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004373 goto check_channel_flags;
4374 }
4375
4376 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4377 http_msg_closing:
4378 /* nothing else to forward, just waiting for the output buffer
4379 * to be empty and for the shutw_now to take effect.
4380 */
4381 if (channel_is_empty(chn)) {
4382 txn->req.msg_state = HTTP_MSG_CLOSED;
4383 goto http_msg_closed;
4384 }
4385 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004386 txn->req.msg_state = HTTP_MSG_ERROR;
4387 goto end;
4388 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004389 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004390 return;
4391 }
4392
4393 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4394 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004395 /* if we don't know whether the server will close, we need to hard close */
4396 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
Christopher Faulet8abe7122022-03-30 15:10:18 +02004397 s->csb->flags |= CS_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004398 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004399 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004400 channel_dont_read(chn);
4401 goto end;
4402 }
4403
4404 check_channel_flags:
4405 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4406 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4407 /* if we've just closed an output, let's switch */
4408 txn->req.msg_state = HTTP_MSG_CLOSING;
4409 goto http_msg_closing;
4410 }
4411
4412 end:
4413 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004414 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4415 chn->flags |= CF_NEVER_WAIT;
4416 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004417 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004418 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004419 channel_auto_close(chn);
4420 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004421 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004422}
4423
4424
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004425/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004426 * because an error was triggered during the body forwarding.
4427 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004428static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004429{
4430 struct channel *chn = &s->res;
4431 struct http_txn *txn = s->txn;
4432
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004433 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004434
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004435 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4436 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004437 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004438 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004439 goto end;
4440 }
4441
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004442 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4443 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004444 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004445 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004446
4447 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4448 /* In theory, we don't need to read anymore, but we must
4449 * still monitor the server connection for a possible close
4450 * while the request is being uploaded, so we don't disable
4451 * reading.
4452 */
4453 /* channel_dont_read(chn); */
4454
4455 if (txn->req.msg_state < HTTP_MSG_DONE) {
4456 /* The client seems to still be sending data, probably
4457 * because we got an error response during an upload.
4458 * We have the choice of either breaking the connection
4459 * or letting it pass through. Let's do the later.
4460 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004461 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004462 return;
4463 }
4464
4465 /* When we get here, it means that both the request and the
4466 * response have finished receiving. Depending on the connection
4467 * mode, we'll have to wait for the last bytes to leave in either
4468 * direction, and sometimes for a close to be effective.
4469 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004470 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004471 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004472 if (b_data(&chn->buf)) {
4473 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004474 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004475 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004476 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4477 }
4478 else {
4479 /* we're not expecting any new data to come for this
4480 * transaction, so we can close it.
4481 */
4482 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4483 channel_shutr_now(chn);
4484 channel_shutw_now(chn);
4485 }
4486 }
4487 goto check_channel_flags;
4488 }
4489
4490 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4491 http_msg_closing:
4492 /* nothing else to forward, just waiting for the output buffer
4493 * to be empty and for the shutw_now to take effect.
4494 */
4495 if (channel_is_empty(chn)) {
4496 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4497 goto http_msg_closed;
4498 }
4499 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004500 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau4781b152021-04-06 13:53:36 +02004501 _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts);
4502 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01004503 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004504 _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004505 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02004506 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004507 goto end;
4508 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004509 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004510 return;
4511 }
4512
4513 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4514 http_msg_closed:
4515 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004516 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004517 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004518 goto end;
4519 }
4520
4521 check_channel_flags:
4522 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4523 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4524 /* if we've just closed an output, let's switch */
4525 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4526 goto http_msg_closing;
4527 }
4528
4529 end:
4530 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004531 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4532 chn->flags |= CF_NEVER_WAIT;
4533 if (HAS_RSP_DATA_FILTERS(s))
4534 chn->analysers |= AN_RES_FLT_XFER_DATA;
4535 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004536 channel_auto_close(chn);
4537 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004538 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004539}
4540
Christopher Fauletef70e252020-01-28 09:26:19 +01004541/* Forward a response generated by HAProxy (error/redirect/return). This
4542 * function forwards all pending incoming data. If <final> is set to 0, nothing
4543 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004544 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004545 * returned. If an error occurred, 0 is returned. If it fails, this function
4546 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004547 */
4548int http_forward_proxy_resp(struct stream *s, int final)
4549{
4550 struct channel *req = &s->req;
4551 struct channel *res = &s->res;
4552 struct htx *htx = htxbuf(&res->buf);
4553 size_t data;
4554
4555 if (final) {
4556 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004557
Christopher Fauletaab1b672020-11-18 16:44:02 +01004558 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004559 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004560
Christopher Fauletd6c48362020-10-19 18:01:38 +02004561 if (s->txn->meth == HTTP_METH_HEAD)
4562 htx_skip_msg_payload(htx);
4563
Christopher Fauletef70e252020-01-28 09:26:19 +01004564 channel_auto_read(req);
4565 channel_abort(req);
4566 channel_auto_close(req);
4567 channel_htx_erase(req, htxbuf(&req->buf));
4568
4569 res->wex = tick_add_ifset(now_ms, res->wto);
4570 channel_auto_read(res);
4571 channel_auto_close(res);
4572 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004573 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004574 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004575 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004576 else {
4577 /* Send ASAP informational messages. Rely on CF_EOI for final
4578 * response.
4579 */
4580 res->flags |= CF_SEND_DONTWAIT;
4581 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004582
4583 data = htx->data - co_data(res);
4584 c_adv(res, data);
4585 htx->first = -1;
4586 res->total += data;
4587 return 1;
4588}
4589
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004590void http_server_error(struct stream *s, struct conn_stream *cs, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004591 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004592{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004593 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004594 if (!(s->flags & SF_ERR_MASK))
4595 s->flags |= err;
4596 if (!(s->flags & SF_FINST_MASK))
4597 s->flags |= finst;
4598}
4599
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004600void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004601{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004602 if (!msg) {
4603 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4604 goto end;
4605 }
4606
4607 if (http_reply_message(s, msg) == -1) {
4608 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004609 * it is already an internal error. If it was already a "const"
4610 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004611 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004612 if (s->txn->status == 500) {
4613 if (s->txn->flags & TX_CONST_REPLY)
4614 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004615 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004616 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004617 s->txn->status = 500;
4618 s->txn->http_reply = NULL;
4619 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4620 }
4621
4622end:
4623 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004624
Christopher Faulet2d565002021-09-10 09:17:50 +02004625 /* At this staged, HTTP analysis is finished */
4626 s->req.analysers &= AN_REQ_FLT_END;
4627 s->req.analyse_exp = TICK_ETERNITY;
4628
4629 s->res.analysers &= AN_RES_FLT_END;
4630 s->res.analyse_exp = TICK_ETERNITY;
4631
Christopher Faulet0f226952018-10-22 09:29:56 +02004632 channel_auto_read(&s->req);
4633 channel_abort(&s->req);
4634 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004635 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004636 channel_auto_read(&s->res);
4637 channel_auto_close(&s->res);
4638 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004639}
4640
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004641struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004642{
4643 const int msgnum = http_get_status_idx(s->txn->status);
4644
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004645 if (s->txn->http_reply)
4646 return s->txn->http_reply;
4647 else if (s->be->replies[msgnum])
4648 return s->be->replies[msgnum];
4649 else if (strm_fe(s)->replies[msgnum])
4650 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004651 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004652 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004653}
4654
Christopher Faulet40e6b552020-06-25 16:04:50 +02004655/* Produces an HTX message from an http reply. Depending on the http reply type,
4656 * a, errorfile, an raw file or a log-format string is used. On success, it
4657 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4658 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004659 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004660int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004661{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004662 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004663 struct htx_sl *sl;
4664 struct buffer *body = NULL;
4665 const char *status, *reason, *clen, *ctype;
4666 unsigned int slflags;
4667 int ret = 0;
4668
Christopher Faulete29a97e2020-05-14 14:49:25 +02004669 /*
4670 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4671 *
4672 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4673 * as no payload if NULL. the TXN status code is set with the status
4674 * of the original reply.
4675 */
4676
4677 if (reply->type == HTTP_REPLY_INDIRECT) {
4678 if (reply->body.reply)
4679 reply = reply->body.reply;
4680 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004681 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4682 /* get default error message */
4683 if (reply == s->txn->http_reply)
4684 s->txn->http_reply = NULL;
4685 reply = http_error_message(s);
4686 if (reply->type == HTTP_REPLY_INDIRECT) {
4687 if (reply->body.reply)
4688 reply = reply->body.reply;
4689 }
4690 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004691
4692 if (reply->type == HTTP_REPLY_ERRMSG) {
4693 /* implicit or explicit error message*/
4694 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004695 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004696 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004697 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004698 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004699 }
4700 else {
4701 /* no payload, file or log-format string */
4702 if (reply->type == HTTP_REPLY_RAW) {
4703 /* file */
4704 body = &reply->body.obj;
4705 }
4706 else if (reply->type == HTTP_REPLY_LOGFMT) {
4707 /* log-format string */
4708 body = alloc_trash_chunk();
4709 if (!body)
4710 goto fail_alloc;
4711 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4712 }
4713 /* else no payload */
4714
4715 status = ultoa(reply->status);
4716 reason = http_get_reason(reply->status);
4717 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4718 if (!body || !b_data(body))
4719 slflags |= HTX_SL_F_BODYLESS;
4720 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4721 if (!sl)
4722 goto fail;
4723 sl->info.res.status = reply->status;
4724
4725 clen = (body ? ultoa(b_data(body)) : "0");
4726 ctype = reply->ctype;
4727
4728 if (!LIST_ISEMPTY(&reply->hdrs)) {
4729 struct http_reply_hdr *hdr;
4730 struct buffer *value = alloc_trash_chunk();
4731
4732 if (!value)
4733 goto fail;
4734
4735 list_for_each_entry(hdr, &reply->hdrs, list) {
4736 chunk_reset(value);
4737 value->data = build_logline(s, value->area, value->size, &hdr->value);
4738 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4739 free_trash_chunk(value);
4740 goto fail;
4741 }
4742 chunk_reset(value);
4743 }
4744 free_trash_chunk(value);
4745 }
4746
4747 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4748 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4749 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004750 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004751 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004752
4753 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004754 }
4755
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004756 leave:
4757 if (reply->type == HTTP_REPLY_LOGFMT)
4758 free_trash_chunk(body);
4759 return ret;
4760
4761 fail_alloc:
4762 if (!(s->flags & SF_ERR_MASK))
4763 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004764 /* fall through */
4765 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004766 ret = -1;
4767 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004768}
4769
4770/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004771 * occurs -1 is returned and the response channel is truncated, removing this
4772 * way the faulty reply. This function may fail when the reply is formatted
4773 * (http_reply_to_htx) or when the reply is forwarded
4774 * (http_forward_proxy_resp). On the last case, it is because a
4775 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004776 */
4777int http_reply_message(struct stream *s, struct http_reply *reply)
4778{
4779 struct channel *res = &s->res;
4780 struct htx *htx = htx_from_buf(&res->buf);
4781
4782 if (s->txn->status == -1)
4783 s->txn->status = reply->status;
4784 channel_htx_truncate(res, htx);
4785
4786 if (http_reply_to_htx(s, htx, reply) == -1)
4787 goto fail;
4788
4789 htx_to_buf(htx, &s->res.buf);
4790 if (!http_forward_proxy_resp(s, 1))
4791 goto fail;
4792 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004793
4794 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004795 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004796 if (!(s->flags & SF_ERR_MASK))
4797 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004798 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004799}
4800
Christopher Faulet50264b42022-03-30 19:39:30 +02004801/* Return the error message corresponding to s->conn_err_type. It is assumed
Christopher Faulet304cc402019-07-15 15:46:28 +02004802 * that the server side is closed. Note that err_type is actually a
4803 * bitmask, where almost only aborts may be cumulated with other
4804 * values. We consider that aborted operations are more important
4805 * than timeouts or errors due to the fact that nobody else in the
4806 * logs might explain incomplete retries. All others should avoid
4807 * being cumulated. It should normally not be possible to have multiple
4808 * aborts at once, but just in case, the first one in sequence is reported.
4809 * Note that connection errors appearing on the second request of a keep-alive
4810 * connection are not reported since this allows the client to retry.
4811 */
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004812void http_return_srv_error(struct stream *s, struct conn_stream *cs)
Christopher Faulet304cc402019-07-15 15:46:28 +02004813{
Christopher Faulet50264b42022-03-30 19:39:30 +02004814 int err_type = s->conn_err_type;
Christopher Faulet304cc402019-07-15 15:46:28 +02004815
4816 /* set s->txn->status for http_error_message(s) */
Christopher Faulet50264b42022-03-30 19:39:30 +02004817 if (err_type & STRM_ET_QUEUE_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004818 s->txn->status = -1;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004819 http_server_error(s, cs, SF_ERR_CLICL, SF_FINST_Q, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004820 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004821 else if (err_type & STRM_ET_CONN_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004822 s->txn->status = -1;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004823 http_server_error(s, cs, SF_ERR_CLICL, SF_FINST_C, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004824 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004825 else if (err_type & STRM_ET_QUEUE_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004826 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004827 http_server_error(s, cs, SF_ERR_SRVTO, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004828 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004829 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004830 else if (err_type & STRM_ET_QUEUE_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004831 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004832 http_server_error(s, cs, SF_ERR_SRVCL, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004833 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004834 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004835 else if (err_type & STRM_ET_CONN_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004836 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004837 http_server_error(s, cs, SF_ERR_SRVTO, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004838 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4839 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004840 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004841 else if (err_type & STRM_ET_CONN_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004842 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004843 http_server_error(s, cs, SF_ERR_SRVCL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004844 (s->flags & SF_SRV_REUSED) ? NULL :
4845 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004846 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004847 else if (err_type & STRM_ET_CONN_RES) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004848 s->txn->status = 503;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004849 http_server_error(s, cs, SF_ERR_RESOURCE, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004850 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4851 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004852 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004853 else { /* STRM_ET_CONN_OTHER and others */
Christopher Faulet304cc402019-07-15 15:46:28 +02004854 s->txn->status = 500;
Christopher Faulet0eb32c02022-04-04 11:06:31 +02004855 http_server_error(s, cs, SF_ERR_INTERNAL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004856 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004857 }
4858}
4859
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004860
Christopher Faulet4a28a532019-03-01 11:19:40 +01004861/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4862 * on success and -1 on error.
4863 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004864static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004865{
4866 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4867 * then we must send an HTTP/1.1 100 Continue intermediate response.
4868 */
4869 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4870 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4871 struct ist hdr = { .ptr = "Expect", .len = 6 };
4872 struct http_hdr_ctx ctx;
4873
4874 ctx.blk = NULL;
4875 /* Expect is allowed in 1.1, look for it */
4876 if (http_find_header(htx, hdr, &ctx, 0) &&
4877 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004878 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004879 return -1;
4880 http_remove_header(htx, &ctx);
4881 }
4882 }
4883 return 0;
4884}
4885
Christopher Faulet23a3c792018-11-28 10:01:23 +01004886/* Send a 100-Continue response to the client. It returns 0 on success and -1
4887 * on error. The response channel is updated accordingly.
4888 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004889static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004890{
4891 struct channel *res = &s->res;
4892 struct htx *htx = htx_from_buf(&res->buf);
4893 struct htx_sl *sl;
4894 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4895 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004896
4897 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4898 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4899 if (!sl)
4900 goto fail;
4901 sl->info.res.status = 100;
4902
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004903 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004904 goto fail;
4905
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004906 if (!http_forward_proxy_resp(s, 0))
4907 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004908 return 0;
4909
4910 fail:
4911 /* If an error occurred, remove the incomplete HTTP response from the
4912 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004913 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004914 return -1;
4915}
4916
Christopher Faulet12c51e22018-11-28 15:59:42 +01004917
Christopher Faulet0f226952018-10-22 09:29:56 +02004918/*
4919 * Capture headers from message <htx> according to header list <cap_hdr>, and
4920 * fill the <cap> pointers appropriately.
4921 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004922static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004923{
4924 struct cap_hdr *h;
4925 int32_t pos;
4926
Christopher Fauleta3f15502019-05-13 15:27:23 +02004927 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004928 struct htx_blk *blk = htx_get_blk(htx, pos);
4929 enum htx_blk_type type = htx_get_blk_type(blk);
4930 struct ist n, v;
4931
4932 if (type == HTX_BLK_EOH)
4933 break;
4934 if (type != HTX_BLK_HDR)
4935 continue;
4936
4937 n = htx_get_blk_name(htx, blk);
4938
4939 for (h = cap_hdr; h; h = h->next) {
4940 if (h->namelen && (h->namelen == n.len) &&
4941 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4942 if (cap[h->index] == NULL)
4943 cap[h->index] =
4944 pool_alloc(h->pool);
4945
4946 if (cap[h->index] == NULL) {
4947 ha_alert("HTTP capture : out of memory.\n");
4948 break;
4949 }
4950
4951 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01004952 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004953
4954 memcpy(cap[h->index], v.ptr, v.len);
4955 cap[h->index][v.len]=0;
4956 }
4957 }
4958 }
4959}
4960
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004961/* Delete a value in a header between delimiters <from> and <next>. The header
4962 * itself is delimited by <start> and <end> pointers. The number of characters
4963 * displaced is returned, and the pointer to the first delimiter is updated if
4964 * required. The function tries as much as possible to respect the following
4965 * principles :
4966 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4967 * in which case <next> is simply removed
4968 * - set exactly one space character after the new first delimiter, unless there
4969 * are not enough characters in the block being moved to do so.
4970 * - remove unneeded spaces before the previous delimiter and after the new
4971 * one.
4972 *
4973 * It is the caller's responsibility to ensure that :
4974 * - <from> points to a valid delimiter or <start> ;
4975 * - <next> points to a valid delimiter or <end> ;
4976 * - there are non-space chars before <from>.
4977 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004978static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004979{
4980 char *prev = *from;
4981
4982 if (prev == start) {
4983 /* We're removing the first value. eat the semicolon, if <next>
4984 * is lower than <end> */
4985 if (next < end)
4986 next++;
4987
4988 while (next < end && HTTP_IS_SPHT(*next))
4989 next++;
4990 }
4991 else {
4992 /* Remove useless spaces before the old delimiter. */
4993 while (HTTP_IS_SPHT(*(prev-1)))
4994 prev--;
4995 *from = prev;
4996
4997 /* copy the delimiter and if possible a space if we're
4998 * not at the end of the line.
4999 */
5000 if (next < end) {
5001 *prev++ = *next++;
5002 if (prev + 1 < next)
5003 *prev++ = ' ';
5004 while (next < end && HTTP_IS_SPHT(*next))
5005 next++;
5006 }
5007 }
5008 memmove(prev, next, end - next);
5009 return (prev - next);
5010}
5011
Christopher Faulet0f226952018-10-22 09:29:56 +02005012
5013/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005014 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005015 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005016static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005017{
5018 struct ist dst = ist2(str, 0);
5019
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005020 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005021 goto end;
5022 if (dst.len + 1 > len)
5023 goto end;
5024 dst.ptr[dst.len++] = ' ';
5025
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005026 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005027 goto end;
5028 if (dst.len + 1 > len)
5029 goto end;
5030 dst.ptr[dst.len++] = ' ';
5031
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005032 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005033 end:
5034 return dst.len;
5035}
5036
5037/*
5038 * Print a debug line with a start line.
5039 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005040static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005041{
5042 struct session *sess = strm_sess(s);
5043 int max;
5044
5045 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5046 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005047 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Christopher Faulet693b23b2022-02-28 09:09:05 +01005048 cs_conn(s->csb) ? (unsigned short)(__cs_conn(s->csb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005049
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005050 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005051 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005052 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005053 trash.area[trash.data++] = ' ';
5054
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005055 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005056 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005057 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005058 trash.area[trash.data++] = ' ';
5059
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005060 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005061 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005062 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005063 trash.area[trash.data++] = '\n';
5064
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005065 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005066}
5067
5068/*
5069 * Print a debug line with a header.
5070 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005071static 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 +02005072{
5073 struct session *sess = strm_sess(s);
5074 int max;
5075
5076 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5077 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00005078 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Christopher Faulet693b23b2022-02-28 09:09:05 +01005079 cs_conn(s->csb) ? (unsigned short)(__cs_conn(s->csb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02005080
5081 max = n.len;
5082 UBOUND(max, trash.size - trash.data - 3);
5083 chunk_memcat(&trash, n.ptr, max);
5084 trash.area[trash.data++] = ':';
5085 trash.area[trash.data++] = ' ';
5086
5087 max = v.len;
5088 UBOUND(max, trash.size - trash.data - 1);
5089 chunk_memcat(&trash, v.ptr, max);
5090 trash.area[trash.data++] = '\n';
5091
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005092 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005093}
5094
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005095void http_txn_reset_req(struct http_txn *txn)
5096{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005097 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005098 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5099}
5100
5101void http_txn_reset_res(struct http_txn *txn)
5102{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005103 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005104 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5105}
5106
5107/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01005108 * Create and initialize a new HTTP transaction for stream <s>. This should be
5109 * used before processing any new request. It returns the transaction or NLULL
5110 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005111 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005112struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005113{
Christopher Faulet75f619a2021-03-08 19:12:58 +01005114 struct http_txn *txn;
Christopher Faulet95a61e82021-12-22 14:22:03 +01005115 struct conn_stream *cs = s->csf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005116
Christopher Faulet75f619a2021-03-08 19:12:58 +01005117 txn = pool_alloc(pool_head_http_txn);
5118 if (!txn)
5119 return NULL;
5120 s->txn = txn;
5121
Christopher Faulete9e48202022-03-22 18:13:29 +01005122 txn->flags = ((cs && cs->endp->flags & CS_EP_NOT_FIRST) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005123 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005124 txn->http_reply = NULL;
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005125 txn->l7_buffer = BUF_NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005126 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005127
5128 txn->cookie_first_date = 0;
5129 txn->cookie_last_date = 0;
5130
5131 txn->srv_cookie = NULL;
5132 txn->cli_cookie = NULL;
5133 txn->uri = NULL;
5134
5135 http_txn_reset_req(txn);
5136 http_txn_reset_res(txn);
5137
5138 txn->req.chn = &s->req;
5139 txn->rsp.chn = &s->res;
5140
5141 txn->auth.method = HTTP_AUTH_UNKNOWN;
5142
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02005143 vars_init_head(&s->vars_txn, SCOPE_TXN);
5144 vars_init_head(&s->vars_reqres, SCOPE_REQ);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005145
5146 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005147}
5148
5149/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005150void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005151{
5152 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005153
5154 /* these ones will have been dynamically allocated */
5155 pool_free(pool_head_requri, txn->uri);
5156 pool_free(pool_head_capture, txn->cli_cookie);
5157 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005158 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005159
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005160 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005161 txn->uri = NULL;
5162 txn->srv_cookie = NULL;
5163 txn->cli_cookie = NULL;
5164
Christopher Faulet59399252019-11-07 14:27:52 +01005165 if (!LIST_ISEMPTY(&s->vars_txn.head))
5166 vars_prune(&s->vars_txn, s->sess, s);
5167 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5168 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005169
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005170 b_free(&txn->l7_buffer);
5171
Christopher Faulet75f619a2021-03-08 19:12:58 +01005172 pool_free(pool_head_http_txn, txn);
5173 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005174}
5175
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005176
5177DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005178
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005179/*
5180 * Local variables:
5181 * c-indent-level: 8
5182 * c-basic-offset: 8
5183 * End:
5184 */