blob: 80263e61a339d4fd3a375c3b03a7e0f958d0c1cf [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 Tarreau91cefca2022-05-03 17:08:29 +020016#include <haproxy/applet.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <haproxy/backend.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020018#include <haproxy/base64.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020019#include <haproxy/capture-t.h>
Amaury Denoyelle03517732021-05-07 14:25:01 +020020#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020021#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020022#include <haproxy/check.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020023#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020024#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020025#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020026#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020027#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020028#include <haproxy/http_htx.h>
Aurelien DARRAGONb2bb9252022-12-28 15:37:57 +010029#include <haproxy/http_ext.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 Tarreau5edca2f2022-05-27 09:25:10 +020035#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020036#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020037#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020038#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020039#include <haproxy/stream.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020040#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020041#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020042#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020043
Christopher Faulete0768eb2018-10-03 16:38:02 +020044
Christopher Fauleteea8fc72019-11-05 16:18:10 +010045#define TRACE_SOURCE &trace_strm
46
Christopher Faulet377c5a52018-10-24 21:21:30 +020047extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020048
Willy Tarreauff882702021-04-10 17:23:00 +020049struct pool_head *pool_head_requri __read_mostly = NULL;
50struct pool_head *pool_head_capture __read_mostly = NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +020051
52
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020053static void http_end_request(struct stream *s);
54static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020055
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020056static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
57static int http_del_hdr_value(char *start, char *end, char **from, char *next);
58static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020059static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
60static 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 +020061
Christopher Fauletd4150ad2021-10-13 15:35:55 +020062static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s);
63static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s);
Christopher Faulet18c13d32022-05-16 11:43:10 +020064static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px);
Christopher Faulet3e964192018-10-24 11:39:23 +020065
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020066static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
67static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020068
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020069static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
70static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020071
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020072static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
73static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010074
Christopher Faulete0768eb2018-10-03 16:38:02 +020075/* This stream analyser waits for a complete HTTP request. It returns 1 if the
76 * processing can continue on next analysers, or zero if it either needs more
77 * data or wants to immediately abort the request (eg: timeout, error, ...). It
78 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
79 * when it has nothing left to do, and may remove any analyser when it wants to
80 * abort.
81 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020082int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020083{
Christopher Faulet9768c262018-10-22 09:34:31 +020084
Christopher Faulete0768eb2018-10-03 16:38:02 +020085 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020086 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020087 *
Christopher Faulet9768c262018-10-22 09:34:31 +020088 * Once the start line and all headers are received, we may perform a
89 * capture of the error (if any), and we will set a few fields. We also
90 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020091 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020092 struct session *sess = s->sess;
93 struct http_txn *txn = s->txn;
94 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020095 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010096 struct htx_sl *sl;
Frédéric Lécaille9969adb2023-01-18 11:52:21 +010097 char http_ver;
98 int len;
Christopher Faulete0768eb2018-10-03 16:38:02 +020099
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100100 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200101
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100102 if (unlikely(!IS_HTX_STRM(s))) {
103 /* It is only possible when a TCP stream is upgrade to HTTP.
104 * There is a transition period during which there is no
105 * data. The stream is still in raw mode and SF_IGNORE flag is
106 * still set. When this happens, the new mux is responsible to
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500107 * handle all errors. Thus we may leave immediately.
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100108 */
109 BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req));
Christopher Faulet9768c262018-10-22 09:34:31 +0200110
Christopher Faulet97b3a612021-03-15 17:10:12 +0100111 /* Don't connect for now */
112 channel_dont_connect(req);
113
114 /* A SHUTR at this stage means we are performing a "destructive"
115 * HTTP upgrade (TCP>H2). In this case, we can leave.
116 */
117 if (req->flags & CF_SHUTR) {
118 s->logs.logwait = 0;
119 s->logs.level = 0;
120 channel_abort(&s->req);
121 channel_abort(&s->res);
122 req->analysers &= AN_REQ_FLT_END;
123 req->analyse_exp = TICK_ETERNITY;
124 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
125 return 1;
126 }
Christopher Fauletda46a0d2021-01-21 17:32:58 +0100127 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s);
128 return 0;
129 }
130
131 htx = htxbuf(&req->buf);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100132 sl = http_get_stline(htx);
133 len = HTX_SL_REQ_VLEN(sl);
134 if (len < 6) {
135 http_ver = 0;
136 }
137 else {
138 char *ptr;
139
140 ptr = HTX_SL_REQ_VPTR(sl);
141 http_ver = ptr[5] - '0';
142 }
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200143
Willy Tarreau4236f032019-03-05 10:43:32 +0100144 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200145 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +0100146 stream_inc_http_req_ctr(s);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100147 proxy_inc_fe_req_ctr(sess->listener, sess->fe, http_ver);
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100148 if (htx->flags & HTX_FL_PARSING_ERROR) {
149 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200150 goto return_bad_req;
Christopher Fauletbf7175f2021-02-10 14:58:01 +0100151 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200152 else
153 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100154 }
155
Christopher Faulete0768eb2018-10-03 16:38:02 +0200156 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200157 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200158
Christopher Faulet9768c262018-10-22 09:34:31 +0200159 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200160 stream_inc_http_req_ctr(s);
Frédéric Lécaille9969adb2023-01-18 11:52:21 +0100161 proxy_inc_fe_req_ctr(sess->listener, sess->fe, http_ver); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200162
Christopher Faulet9768c262018-10-22 09:34:31 +0200163 /* kill the pending keep-alive timeout */
Christopher Faulet9768c262018-10-22 09:34:31 +0200164 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200165
Christopher Faulet29f17582019-05-23 11:03:26 +0200166 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet03599112018-11-27 11:21:21 +0100167
Christopher Faulet9768c262018-10-22 09:34:31 +0200168 /* 0: we might have to print this header in debug mode */
169 if (unlikely((global.mode & MODE_DEBUG) &&
170 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
171 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200172
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200173 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200174
Christopher Fauleta3f15502019-05-13 15:27:23 +0200175 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200176 struct htx_blk *blk = htx_get_blk(htx, pos);
177 enum htx_blk_type type = htx_get_blk_type(blk);
178
179 if (type == HTX_BLK_EOH)
180 break;
181 if (type != HTX_BLK_HDR)
182 continue;
183
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200184 http_debug_hdr("clihdr", s,
185 htx_get_blk_name(htx, blk),
186 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200187 }
188 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200189
190 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100191 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200192 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100193 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100194 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200195 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100196 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +0100197 if (sl->flags & HTX_SL_F_CLEN)
198 msg->flags |= HTTP_MSGF_CNT_LEN;
199 else if (sl->flags & HTX_SL_F_CHNK)
200 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100201 if (sl->flags & HTX_SL_F_BODYLESS)
202 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +0100203 if (sl->flags & HTX_SL_F_CONN_UPG)
204 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200205
206 /* we can make use of server redirect on GET and HEAD */
207 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
208 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100209 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200210 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200211 goto return_bad_req;
212 }
213
214 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100215 * 2: check if the URI matches the monitor_uri. We have to do this for
216 * every request which gets in, because the monitor-uri is defined by
217 * the frontend. If the monitor-uri starts with a '/', the matching is
218 * done against the request's path. Otherwise, the request's uri is
219 * used. It is a workaround to let HTTP/2 health-checks work as
220 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200221 */
Tim Duesterhus4b1fcaa2022-03-05 00:52:40 +0100222 if (unlikely(isttest(sess->fe->monitor_uri))) {
223 const struct ist monitor_uri = sess->fe->monitor_uri;
Amaury Denoyellec453f952021-07-06 11:40:12 +0200224 struct http_uri_parser parser = http_uri_parser_init(htx_sl_req_uri(sl));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200225
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200226 if ((istptr(monitor_uri)[0] == '/' &&
Amaury Denoyellec453f952021-07-06 11:40:12 +0200227 isteq(http_parse_path(&parser), monitor_uri)) ||
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200228 isteq(htx_sl_req_uri(sl), monitor_uri)) {
229 /*
230 * We have found the monitor URI
231 */
232 struct acl_cond *cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200233
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200234 s->flags |= SF_MONITOR;
235 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200236
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200237 /* Check if we want to fail this monitor request or not */
238 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
239 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200240
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200241 ret = acl_pass(ret);
242 if (cond->pol == ACL_COND_UNLESS)
243 ret = !ret;
244
245 if (ret) {
246 /* we fail this request, let's return 503 service unavail */
247 txn->status = 503;
248 if (!(s->flags & SF_ERR_MASK))
249 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
250 goto return_prx_cond;
251 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200252 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200253
Amaury Denoyelle5a9bd372021-07-06 11:23:10 +0200254 /* nothing to fail, let's reply normally */
255 txn->status = 200;
256 if (!(s->flags & SF_ERR_MASK))
257 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
258 goto return_prx_cond;
259 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200260 }
261
262 /*
263 * 3: Maybe we have to copy the original REQURI for the logs ?
264 * Note: we cannot log anymore if the request has been
265 * classified as invalid.
266 */
267 if (unlikely(s->logs.logwait & LW_REQ)) {
268 /* we have a complete HTTP request that we must log */
269 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200270 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200271
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200272 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200273 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200274
275 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
276 s->do_log(s);
277 } else {
278 ha_alert("HTTP logging : out of memory.\n");
279 }
280 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200281
Christopher Faulete0768eb2018-10-03 16:38:02 +0200282 /* if the frontend has "option http-use-proxy-header", we'll check if
283 * we have what looks like a proxied connection instead of a connection,
284 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
285 * Note that this is *not* RFC-compliant, however browsers and proxies
286 * happen to do that despite being non-standard :-(
287 * We consider that a request not beginning with either '/' or '*' is
288 * a proxied connection, which covers both "scheme://location" and
289 * CONNECT ip:port.
290 */
291 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100292 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200293 txn->flags |= TX_USE_PX_CONN;
294
Christopher Faulete0768eb2018-10-03 16:38:02 +0200295 /* 5: we may need to capture headers */
296 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200297 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200298
Christopher Faulete0768eb2018-10-03 16:38:02 +0200299 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200300 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200301 req->analysers |= AN_REQ_HTTP_BODY;
302
303 /*
304 * RFC7234#4:
305 * A cache MUST write through requests with methods
306 * that are unsafe (Section 4.2.1 of [RFC7231]) to
307 * the origin server; i.e., a cache is not allowed
308 * to generate a reply to such a request before
309 * having forwarded the request and having received
310 * a corresponding response.
311 *
312 * RFC7231#4.2.1:
313 * Of the request methods defined by this
314 * specification, the GET, HEAD, OPTIONS, and TRACE
315 * methods are defined to be safe.
316 */
317 if (likely(txn->meth == HTTP_METH_GET ||
318 txn->meth == HTTP_METH_HEAD ||
319 txn->meth == HTTP_METH_OPTIONS ||
320 txn->meth == HTTP_METH_TRACE))
321 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
322
323 /* end of job, return OK */
324 req->analysers &= ~an_bit;
325 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200326
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100327 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200328 return 1;
329
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200330 return_int_err:
331 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200332 if (!(s->flags & SF_ERR_MASK))
333 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200334 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100335 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200336 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200337 goto return_prx_cond;
338
Christopher Faulete0768eb2018-10-03 16:38:02 +0200339 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200340 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200341 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100342 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200343 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200344 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200345
346 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200347 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100348 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200349
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100350 DBG_TRACE_DEVEL("leaving on error",
351 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200352 return 0;
353}
354
355
356/* This stream analyser runs all HTTP request processing which is common to
357 * frontends and backends, which means blocking ACLs, filters, connection-close,
358 * reqadd, stats and redirects. This is performed for the designated proxy.
359 * It returns 1 if the processing can continue on next analysers, or zero if it
360 * either needs more data or wants to immediately abort the request (eg: deny,
361 * error, ...).
362 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200363int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200364{
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200365 struct list *def_rules, *rules;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200366 struct session *sess = s->sess;
367 struct http_txn *txn = s->txn;
368 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200369 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200370 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200371 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200372 struct connection *conn = objt_conn(sess->origin);
373
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100374 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200375
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100376 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200377
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200378 /* just in case we have some per-backend tracking. Only called the first
379 * execution of the analyser. */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200380 if (!s->current_rule && !s->current_rule_list)
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200381 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200382
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200383 def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL);
384 rules = &px->http_req_rules;
385
Christopher Faulete0768eb2018-10-03 16:38:02 +0200386 /* evaluate http-request rules */
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200387 if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) {
388 verdict = http_req_get_intercept_rule(px, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200389
390 switch (verdict) {
391 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
392 goto return_prx_yield;
393
394 case HTTP_RULE_RES_CONT:
395 case HTTP_RULE_RES_STOP: /* nothing to do */
396 break;
397
398 case HTTP_RULE_RES_DENY: /* deny or tarpit */
399 if (txn->flags & TX_CLTARPIT)
400 goto tarpit;
401 goto deny;
402
403 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
404 goto return_prx_cond;
405
406 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
407 goto done;
408
409 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
410 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100411
412 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
413 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200414 }
415 }
416
Christopher Faulet18c13d32022-05-16 11:43:10 +0200417 if (px->options2 & (PR_O2_RSTRICT_REQ_HDR_NAMES_BLK|PR_O2_RSTRICT_REQ_HDR_NAMES_DEL)) {
418 verdict = http_req_restrict_header_names(s, htx, px);
419 if (verdict == HTTP_RULE_RES_DENY)
420 goto deny;
421 }
422
Christopher Faulete0768eb2018-10-03 16:38:02 +0200423 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100424 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200425 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200426
Christopher Fauletff2759f2018-10-24 11:13:16 +0200427 ctx.blk = NULL;
428 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
429 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletd649b572022-06-01 17:42:35 +0200430 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200431 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200432 }
433
434 /* OK at this stage, we know that the request was accepted according to
435 * the http-request rules, we can check for the stats. Note that the
436 * URI is detected *before* the req* rules in order not to be affected
437 * by a possible reqrep, while they are processed *after* so that a
438 * reqdeny can still block them. This clearly needs to change in 1.6!
439 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200440 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200441 s->target = &http_stats_applet.obj_type;
Willy Tarreaua0b58b52022-05-27 08:33:53 +0200442 if (unlikely(!sc_applet_create(s->scb, objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200443 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200444 if (!(s->flags & SF_ERR_MASK))
445 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100446 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200447 }
448
449 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200450 http_handle_stats(s, req);
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200451 verdict = http_req_get_intercept_rule(px, NULL, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200452 /* not all actions implemented: deny, allow, auth */
453
454 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
455 goto deny;
456
457 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
458 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100459
460 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
461 goto return_bad_req;
462
463 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
464 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200465 }
466
Christopher Faulet2571bc62019-03-01 11:44:26 +0100467 /* Proceed with the applets now. */
468 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200469 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +0200470 _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200471
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200472 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100473 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100474
Christopher Faulete0768eb2018-10-03 16:38:02 +0200475 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
476 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
Christopher Faulet4a66c942023-01-13 09:43:21 +0100477 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200478
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100479 if (HAS_FILTERS(s))
480 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
481
Christopher Faulete0768eb2018-10-03 16:38:02 +0200482 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
483 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
484 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
485 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100486
487 req->flags |= CF_SEND_DONTWAIT;
488 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200489 goto done;
490 }
491
492 /* check whether we have some ACLs set to redirect this request */
493 list_for_each_entry(rule, &px->redirect_rules, list) {
494 if (rule->cond) {
495 int ret;
496
497 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
498 ret = acl_pass(ret);
499 if (rule->cond->pol == ACL_COND_UNLESS)
500 ret = !ret;
501 if (!ret)
502 continue;
503 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200504 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100505 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200506 goto done;
507 }
508
509 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
510 * If this happens, then the data will not come immediately, so we must
511 * send all what we have without waiting. Note that due to the small gain
512 * in waiting for the body of the request, it's easier to simply put the
513 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
514 * itself once used.
515 */
516 req->flags |= CF_SEND_DONTWAIT;
517
518 done: /* done with this analyser, continue with next ones that the calling
519 * points will have set, if any.
520 */
521 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500522 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200523 req->analysers &= ~an_bit;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200524 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100525 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200526 return 1;
527
528 tarpit:
529 /* Allow cookie logging
530 */
531 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200532 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200533
534 /* When a connection is tarpitted, we use the tarpit timeout,
535 * which may be the same as the connect timeout if unspecified.
536 * If unset, then set it to zero because we really want it to
537 * eventually expire. We build the tarpit as an analyser.
538 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100539 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200540
541 /* wipe the request out so that we can drop the connection early
542 * if the client closes first.
543 */
544 channel_dont_connect(req);
545
Christopher Faulete0768eb2018-10-03 16:38:02 +0200546 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
547 req->analysers |= AN_REQ_HTTP_TARPIT;
548 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
549 if (!req->analyse_exp)
550 req->analyse_exp = tick_add(now_ms, 0);
551 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 Faulete0768eb2018-10-03 16:38:02 +0200557 goto done_without_exp;
558
559 deny: /* this request was blocked (denied) */
560
561 /* Allow cookie logging
562 */
563 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200564 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200565
Christopher Faulete0768eb2018-10-03 16:38:02 +0200566 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200567 stream_inc_http_err_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +0200568 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100569 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200570 _HA_ATOMIC_INC(&s->be->be_counters.denied_req);
William Lallemand36119de2021-03-08 15:26:48 +0100571 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200572 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100573 goto return_prx_err;
574
Christopher Fauletd649b572022-06-01 17:42:35 +0200575 return_fail_rewrite:
576 if (!(s->flags & SF_ERR_MASK))
577 s->flags |= SF_ERR_PRXCOND;
578 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
579 if (s->flags & SF_BE_ASSIGNED)
580 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
581 if (sess->listener && sess->listener->counters)
582 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
583 if (objt_server(s->target))
584 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
585 /* fall through */
586
Christopher Fauletb8a53712019-12-16 11:29:38 +0100587 return_int_err:
588 txn->status = 500;
589 if (!(s->flags & SF_ERR_MASK))
590 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200591 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100592 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200593 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100594 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200595 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100596 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200597
598 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200599 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200600 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100601 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200602 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100603 /* fall through */
604
605 return_prx_err:
606 http_reply_and_close(s, txn->status, http_error_message(s));
607 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200608
609 return_prx_cond:
Christopher Faulet4a66c942023-01-13 09:43:21 +0100610 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200611
612 req->analysers &= AN_REQ_FLT_END;
613 req->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +0200614 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100615 DBG_TRACE_DEVEL("leaving on error",
616 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200617 return 0;
618
619 return_prx_yield:
620 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100621 DBG_TRACE_DEVEL("waiting for more data",
622 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200623 return 0;
624}
625
626/* This function performs all the processing enabled for the current request.
627 * It returns 1 if the processing can continue on next analysers, or zero if it
628 * needs more data, encounters an error, or wants to immediately abort the
629 * request. It relies on buffers flags, and updates s->req.analysers.
630 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200631int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200632{
633 struct session *sess = s->sess;
634 struct http_txn *txn = s->txn;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200635 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200636 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
637
Christopher Faulet8bebd2f2020-10-06 17:54:56 +0200638 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200639
640 /*
641 * Right now, we know that we have processed the entire headers
642 * and that unwanted requests have been filtered out. We can do
643 * whatever we want with the remaining request. Also, now we
644 * may have separate values for ->fe, ->be.
645 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100646 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200647
648 /*
Christopher Faulete0768eb2018-10-03 16:38:02 +0200649 * 7: Now we can work with the cookies.
650 * Note that doing so might move headers in the request, but
651 * the fields will stay coherent and the URI will not move.
652 * This should only be performed in the backend.
653 */
654 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200655 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200656
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100657 /* 8: Generate unique ID if a "unique-id-format" is defined.
658 *
659 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
660 * fetches only available in the HTTP request processing stage.
661 */
662 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100663 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200664
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100665 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100666 if (!(s->flags & SF_ERR_MASK))
667 s->flags |= SF_ERR_RESOURCE;
668 goto return_int_err;
669 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200670
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100671 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100672 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhus7ad27d42022-05-18 00:22:15 +0200673 unlikely(!http_add_header(htx, sess->fe->header_unique_id, unique_id)))
Christopher Fauletd649b572022-06-01 17:42:35 +0200674 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200675 }
676
Aurelien DARRAGONb2e2ec52023-01-09 11:09:03 +0100677 /* handle http extensions (if configured) */
678 if (unlikely(!http_handle_7239_header(s, req)))
679 goto return_fail_rewrite;
680 if (unlikely(!http_handle_xff_header(s, req)))
681 goto return_fail_rewrite;
682 if (unlikely(!http_handle_xot_header(s, req)))
683 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200684
Christopher Fauletc2ac5e42021-03-08 18:20:09 +0100685 /* Filter the request headers if there are filters attached to the
686 * stream.
687 */
688 if (HAS_FILTERS(s))
689 req->analysers |= AN_REQ_FLT_HTTP_HDRS;
690
Christopher Faulete0768eb2018-10-03 16:38:02 +0200691 /* If we have no server assigned yet and we're balancing on url_param
692 * with a POST request, we may be interested in checking the body for
693 * that parameter. This will be done in another analyser.
694 */
695 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100696 s->txn->meth == HTTP_METH_POST &&
697 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200698 channel_dont_connect(req);
699 req->analysers |= AN_REQ_HTTP_BODY;
700 }
701
702 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
703 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100704
Christopher Faulete0768eb2018-10-03 16:38:02 +0200705 /* We expect some data from the client. Unless we know for sure
706 * we already have a full request, we have to re-enable quick-ack
707 * in case we previously disabled it, otherwise we might cause
708 * the client to delay further data.
709 */
Willy Tarreau9bdcf422023-01-12 19:40:42 +0100710 if ((sess->listener && (sess->listener->bind_conf->options & BC_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100711 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200712
713 /*************************************************************
714 * OK, that's finished for the headers. We have done what we *
715 * could. Let's switch to the DATA state. *
716 ************************************************************/
717 req->analyse_exp = TICK_ETERNITY;
718 req->analysers &= ~an_bit;
719
720 s->logs.tv_request = now;
721 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100722 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200723 return 1;
724
Christopher Fauletd649b572022-06-01 17:42:35 +0200725 return_fail_rewrite:
726 if (!(s->flags & SF_ERR_MASK))
727 s->flags |= SF_ERR_PRXCOND;
728 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
729 if (s->flags & SF_BE_ASSIGNED)
730 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
731 if (sess->listener && sess->listener->counters)
732 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
733 if (objt_server(s->target))
734 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
735 /* fall through */
736
Christopher Fauletb8a53712019-12-16 11:29:38 +0100737 return_int_err:
738 txn->status = 500;
739 if (!(s->flags & SF_ERR_MASK))
740 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200741 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100742 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200743 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100744 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200745 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100746
Christopher Fauletb8a53712019-12-16 11:29:38 +0100747 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100748 http_set_term_flags(s);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100749
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100750 DBG_TRACE_DEVEL("leaving on error",
751 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200752 return 0;
753}
754
755/* This function is an analyser which processes the HTTP tarpit. It always
756 * returns zero, at the beginning because it prevents any other processing
757 * from occurring, and at the end because it terminates the request.
758 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200759int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200760{
761 struct http_txn *txn = s->txn;
762
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100763 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200764 /* This connection is being tarpitted. The CLIENT side has
765 * already set the connect expiration date to the right
766 * timeout. We just have to check that the client is still
767 * there and that the timeout has not expired.
768 */
769 channel_dont_connect(req);
Christopher Faulet4b490b72023-01-04 11:55:15 +0100770 if (!(req->flags & CF_SHUTR) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100771 !tick_is_expired(req->analyse_exp, now_ms)) {
Christopher Fauletb0c87f12021-10-29 14:37:07 +0200772 /* Be sure to drain all data from the request channel */
773 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100774 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
775 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200776 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100777 }
778
Christopher Faulete0768eb2018-10-03 16:38:02 +0200779
780 /* We will set the queue timer to the time spent, just for
781 * logging purposes. We fake a 500 server error, so that the
782 * attacker will not suspect his connection has been tarpitted.
783 * It will not cause trouble to the logs because we can exclude
784 * the tarpitted connections by filtering on the 'PT' status flags.
785 */
786 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
787
Christopher Faulet2e56a732023-01-26 16:18:09 +0100788 http_reply_and_close(s, txn->status, (!sc_ep_test(s->scf, SE_FL_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet4a66c942023-01-13 09:43:21 +0100789 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100790
791 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200792 return 0;
793}
794
795/* This function is an analyser which waits for the HTTP request body. It waits
796 * for either the buffer to be full, or the full advertised contents to have
797 * reached the buffer. It must only be called after the standard HTTP request
798 * processing has occurred, because it expects the request to be parsed and will
799 * look for the Expect header. It may send a 100-Continue interim response. It
800 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
801 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
802 * needs to read more data, or 1 once it has completed its analysis.
803 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200804int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200805{
806 struct session *sess = s->sess;
807 struct http_txn *txn = s->txn;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200808
Christopher Fauletf4569bb2023-01-13 10:49:31 +0100809 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &s->txn->req);
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200810
Christopher Fauletf76ebe82018-10-24 11:16:22 +0200811
Christopher Faulet021a8e42021-03-29 10:46:38 +0200812 switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) {
813 case HTTP_RULE_RES_CONT:
814 goto http_end;
815 case HTTP_RULE_RES_YIELD:
816 goto missing_data_or_waiting;
817 case HTTP_RULE_RES_BADREQ:
Willy Tarreau4236f032019-03-05 10:43:32 +0100818 goto return_bad_req;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200819 case HTTP_RULE_RES_ERROR:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200820 goto return_int_err;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200821 case HTTP_RULE_RES_ABRT:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100822 goto return_prx_cond;
Christopher Faulet021a8e42021-03-29 10:46:38 +0200823 default:
824 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200825 }
826
827 http_end:
828 /* The situation will not evolve, so let's give up on the analysis. */
829 s->logs.tv_request = now; /* update the request timer to reflect full request */
830 req->analysers &= ~an_bit;
831 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100832 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200833 return 1;
834
Christopher Faulet021a8e42021-03-29 10:46:38 +0200835 missing_data_or_waiting:
836 channel_dont_connect(req);
837 DBG_TRACE_DEVEL("waiting for more data",
838 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
839 return 0;
840
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200841 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200842 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200843 if (!(s->flags & SF_ERR_MASK))
844 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +0200845 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100846 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau4781b152021-04-06 13:53:36 +0200847 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100848 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200849 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet021a8e42021-03-29 10:46:38 +0200850 goto return_prx_err;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200851
Christopher Faulete0768eb2018-10-03 16:38:02 +0200852 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853 txn->status = 400;
Willy Tarreau4781b152021-04-06 13:53:36 +0200854 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +0100855 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200856 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100857 /* fall through */
858
Christopher Faulet021a8e42021-03-29 10:46:38 +0200859 return_prx_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +0100860 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet021a8e42021-03-29 10:46:38 +0200861 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200862
Christopher Faulet021a8e42021-03-29 10:46:38 +0200863 return_prx_cond:
Christopher Fauletf0d80df2023-01-13 10:20:20 +0100864 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200865
Christopher Faulete0768eb2018-10-03 16:38:02 +0200866 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100867 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100868 DBG_TRACE_DEVEL("leaving on error",
869 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200870 return 0;
871}
872
873/* This function is an analyser which forwards request body (including chunk
874 * sizes if any). It is called as soon as we must forward, even if we forward
875 * zero byte. The only situation where it must not be called is when we're in
876 * tunnel mode and we want to forward till the close. It's used both to forward
877 * remaining data and to resync after end of body. It expects the msg_state to
878 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
879 * read more data, or 1 once we can go on with next request or end the stream.
880 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
881 * bytes of pending data + the headers if not already done.
882 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200883int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200884{
885 struct session *sess = s->sess;
886 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +0200887 struct http_msg *msg = &txn->req;
888 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +0100889 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100890 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200891
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100892 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200893
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100894 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200895
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200896 if (htx->flags & HTX_FL_PARSING_ERROR)
897 goto return_bad_req;
898 if (htx->flags & HTX_FL_PROCESSING_ERROR)
899 goto return_int_err;
900
Christopher Faulete0768eb2018-10-03 16:38:02 +0200901 /* Note that we don't have to send 100-continue back because we don't
902 * need the data to complete our job, and it's up to the server to
903 * decide whether to return 100, 417 or anything else in return of
904 * an "Expect: 100-continue" header.
905 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200906 if (msg->msg_state == HTTP_MSG_BODY)
907 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200908
Christopher Faulete0768eb2018-10-03 16:38:02 +0200909 /* in most states, we should abort in case of early close */
910 channel_auto_close(req);
911
912 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +0100913 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100914 if (req->flags & CF_EOI)
915 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +0100916 }
917 else {
918 /* We can't process the buffer's contents yet */
919 req->flags |= CF_WAKE_WRITE;
920 goto missing_data_or_waiting;
921 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200922 }
923
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100924 if (msg->msg_state >= HTTP_MSG_ENDING)
925 goto ending;
926
927 if (txn->meth == HTTP_METH_CONNECT) {
928 msg->msg_state = HTTP_MSG_ENDING;
929 goto ending;
930 }
931
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100932 /* Forward input data. We get it by removing all outgoing data not
933 * forwarded yet from HTX data size. If there are some data filters, we
934 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +0200935 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100936 if (HAS_REQ_DATA_FILTERS(s)) {
937 ret = flt_http_payload(s, msg, htx->data);
938 if (ret < 0)
939 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +0200940 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100941 }
942 else {
Christopher Faulet421e7692019-06-13 11:16:45 +0200943 c_adv(req, htx->data - co_data(req));
Christopher Faulet2f7c82b2023-02-20 14:06:52 +0100944 if ((global.tune.options & GTUNE_USE_FAST_FWD) && (msg->flags & HTTP_MSGF_XFER_LEN))
Christopher Faulet66af0b22019-03-22 14:54:52 +0100945 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100946 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200947
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100948 if (htx->data != co_data(req))
949 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200950
Christopher Faulet9768c262018-10-22 09:34:31 +0200951 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200952 * in HTTP_MSG_ENDING state. Then if all data was marked to be
953 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +0200954 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100955 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +0200956 goto missing_data_or_waiting;
957
Christopher Fauletd20fdb02019-06-13 16:43:22 +0200958 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +0200959
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100960 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +0200961 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
962
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100963 /* other states, ENDING...TUNNEL */
964 if (msg->msg_state >= HTTP_MSG_DONE)
965 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200966
Christopher Fauletaed82cf2018-11-30 22:22:32 +0100967 if (HAS_REQ_DATA_FILTERS(s)) {
968 ret = flt_http_end(s, msg);
969 if (ret <= 0) {
970 if (!ret)
971 goto missing_data_or_waiting;
972 goto return_bad_req;
973 }
974 }
975
Christopher Faulet1a3e0272019-11-15 16:31:46 +0100976 if (txn->meth == HTTP_METH_CONNECT)
977 msg->msg_state = HTTP_MSG_TUNNEL;
978 else {
979 msg->msg_state = HTTP_MSG_DONE;
980 req->to_forward = 0;
981 }
982
983 done:
984 /* we don't want to forward closes on DONE except in tunnel mode. */
985 if (!(txn->flags & TX_CON_WANT_TUN))
986 channel_dont_close(req);
987
Christopher Fauletf2b02cf2023-01-13 11:02:28 +0100988 if ((req->flags & CF_SHUTW) && co_data(req)) {
989 /* request errors are most likely due to the server aborting the
990 * transfer. */
991 goto return_srv_abort;
992 }
993
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200994 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200995 if (!(req->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100996 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200997 return 1;
998 }
999
1000 /* If "option abortonclose" is set on the backend, we want to monitor
1001 * the client's connection and forward any shutdown notification to the
1002 * server, which will decide whether to close or to go on processing the
1003 * request. We only do that in tunnel mode, and not in other modes since
1004 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001005 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001006 channel_auto_read(req);
Christopher Faulet446d8032022-12-12 07:53:18 +01001007 if ((req->flags & CF_SHUTR) && !(txn->flags & TX_CON_WANT_TUN))
Willy Tarreaucb041662022-05-17 19:44:42 +02001008 s->scb->flags |= SC_FL_NOLINGER;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001009 channel_auto_close(req);
1010 }
1011 else if (s->txn->meth == HTTP_METH_POST) {
1012 /* POST requests may require to read extra CRLF sent by broken
1013 * browsers and which could cause an RST to be sent upon close
1014 * on some systems (eg: Linux). */
1015 channel_auto_read(req);
1016 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001017 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1018 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001019 return 0;
1020
1021 missing_data_or_waiting:
1022 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001023 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001024 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001025
1026 waiting:
1027 /* waiting for the last bits to leave the buffer */
1028 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001029 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001030
1031 /* When TE: chunked is used, we need to get there again to parse remaining
1032 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1033 * And when content-length is used, we never want to let the possible
1034 * shutdown be forwarded to the other side, as the state machine will
1035 * take care of it once the client responds. It's also important to
1036 * prevent TIME_WAITs from accumulating on the backend side, and for
1037 * HTTP/2 where the last frame comes with a shutdown.
1038 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001039 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001040 channel_dont_close(req);
1041
1042 /* We know that more data are expected, but we couldn't send more that
1043 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1044 * system knows it must not set a PUSH on this first part. Interactive
1045 * modes are already handled by the stream sock layer. We must not do
1046 * this in content-length mode because it could present the MSG_MORE
1047 * flag with the last block of forwarded data, which would cause an
1048 * additional delay to be observed by the receiver.
1049 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001050 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001051 req->flags |= CF_EXPECT_MORE;
1052
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001053 DBG_TRACE_DEVEL("waiting for more data to forward",
1054 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001055 return 0;
1056
Christopher Faulet93e02d82019-03-08 14:18:50 +01001057 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001058 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1059 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001060 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001061 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001062 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001063 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001064 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001065 s->flags |= ((req->flags & CF_READ_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001066 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001067 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001068
1069 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02001070 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
1071 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001072 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001073 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001074 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001075 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001076 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001077 s->flags |= ((req->flags & CF_WRITE_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001078 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001079 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001080
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001081 return_int_err:
1082 if (!(s->flags & SF_ERR_MASK))
1083 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001084 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1085 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001086 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001087 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001088 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001089 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001090 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001091 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001092
Christopher Faulet93e02d82019-03-08 14:18:50 +01001093 return_bad_req:
Willy Tarreau4781b152021-04-06 13:53:36 +02001094 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
William Lallemand36119de2021-03-08 15:26:48 +01001095 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001096 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001097 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001098 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001099
Christopher Fauletb8a53712019-12-16 11:29:38 +01001100 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001101 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001102 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001103 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001104 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001105 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001106 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001107 }
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01001108 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001109 DBG_TRACE_DEVEL("leaving on error ",
1110 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001111 return 0;
1112}
1113
Willy Tarreau4596fe22022-05-17 19:07:51 +02001114/* Reset the stream and the backend stream connector to a situation suitable for attemption connection */
Olivier Houcharda254a372019-04-05 15:30:12 +02001115/* Returns 0 if we can attempt to retry, -1 otherwise */
Willy Tarreaub49672d2022-05-27 10:13:37 +02001116static __inline int do_l7_retry(struct stream *s, struct stconn *sc)
Olivier Houcharda254a372019-04-05 15:30:12 +02001117{
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001118 struct channel *req, *res;
1119 int co_data;
Olivier Houcharda254a372019-04-05 15:30:12 +02001120
Christopher Faulet731c8e62022-03-29 16:08:44 +02001121 s->conn_retries++;
1122 if (s->conn_retries >= s->be->conn_retries)
Christopher Faulet552601d2021-05-26 10:31:06 +02001123 return -1;
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001124
Christopher Faulete763c8c2021-05-05 18:23:59 +02001125 if (objt_server(s->target)) {
1126 if (s->flags & SF_CURR_SESS) {
1127 s->flags &= ~SF_CURR_SESS;
1128 _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess);
1129 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001130 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries);
Christopher Faulete763c8c2021-05-05 18:23:59 +02001131 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001132 _HA_ATOMIC_INC(&s->be->be_counters.retries);
Willy Tarreau223995e2019-05-04 10:38:31 +02001133
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001134 req = &s->req;
1135 res = &s->res;
Olivier Houcharda254a372019-04-05 15:30:12 +02001136 /* Remove any write error from the request, and read error from the response */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001137 req->flags &= ~(CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1138 res->flags &= ~(CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_EVENT | CF_SHUTR_NOW);
Christopher Faulet7bf46bb2022-01-04 10:56:03 +01001139 res->analysers &= AN_RES_FLT_END;
Christopher Faulet50264b42022-03-30 19:39:30 +02001140 s->conn_err_type = STRM_ET_NONE;
Christopher Fauletae024ce2022-03-29 19:02:31 +02001141 s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK);
1142 s->conn_exp = TICK_ETERNITY;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001143 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001144 res->to_forward = 0;
1145 res->analyse_exp = TICK_ETERNITY;
1146 res->total = 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001147
Willy Tarreau19c65a92022-05-27 08:49:24 +02001148 if (sc_reset_endp(s->scb) < 0) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001149 if (!(s->flags & SF_ERR_MASK))
1150 s->flags |= SF_ERR_INTERNAL;
1151 return -1;
1152 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001153
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001154 b_free(&req->buf);
1155 /* Swap the L7 buffer with the channel buffer */
1156 /* We know we stored the co_data as b_data, so get it there */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001157 co_data = b_data(&s->txn->l7_buffer);
1158 b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer));
1159 b_xfer(&req->buf, &s->txn->l7_buffer, b_data(&s->txn->l7_buffer));
Christopher Faulet9f5382e2021-05-21 13:46:14 +02001160 co_set_data(req, co_data);
Christopher Faulet5b82cc52020-10-12 15:18:50 +02001161
Ilya Shipitsinacf84592021-02-06 22:29:08 +05001162 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 +02001163
Olivier Houcharda254a372019-04-05 15:30:12 +02001164 b_reset(&res->buf);
1165 co_set_data(res, 0);
1166 return 0;
1167}
1168
Christopher Faulete0768eb2018-10-03 16:38:02 +02001169/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1170 * processing can continue on next analysers, or zero if it either needs more
1171 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1172 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1173 * when it has nothing left to do, and may remove any analyser when it wants to
1174 * abort.
1175 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001176int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001177{
Christopher Faulet9768c262018-10-22 09:34:31 +02001178 /*
1179 * We will analyze a complete HTTP response to check the its syntax.
1180 *
1181 * Once the start line and all headers are received, we may perform a
1182 * capture of the error (if any), and we will set a few fields. We also
1183 * logging and finally headers capture.
1184 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001185 struct session *sess = s->sess;
1186 struct http_txn *txn = s->txn;
1187 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001188 struct htx *htx;
Christopher Faulet61608322018-11-23 16:23:45 +01001189 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001190 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001191 int n;
1192
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001193 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001194
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001195 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001196
Willy Tarreau4236f032019-03-05 10:43:32 +01001197 /* Parsing errors are caught here */
1198 if (htx->flags & HTX_FL_PARSING_ERROR)
1199 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001200 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1201 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001202
Christopher Faulete0768eb2018-10-03 16:38:02 +02001203 /*
1204 * Now we quickly check if we have found a full valid response.
1205 * If not so, we check the FD and buffer states before leaving.
1206 * A full response is indicated by the fact that we have seen
1207 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1208 * responses are checked first.
1209 *
1210 * Depending on whether the client is still there or not, we
1211 * may send an error response back or not. Note that normally
1212 * we should only check for HTTP status there, and check I/O
1213 * errors somewhere else.
1214 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001215 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001216 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001217 /* 1: have we encountered a read error ? */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001218 if (sc_ep_test(s->scb, SE_FL_ERROR)) {
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001219 struct connection *conn = sc_conn(s->scb);
Olivier Houchard865d8392019-05-03 22:46:27 +02001220
Christopher Fauletd9769232021-05-26 12:15:37 +02001221 /* Perform a L7 retry because server refuses the early data. */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001222 if ((txn->flags & TX_L7_RETRY) &&
Christopher Fauletd9769232021-05-26 12:15:37 +02001223 (s->be->retry_type & PR_RE_EARLY_ERROR) &&
1224 conn && conn->err_code == CO_ER_SSL_EARLY_FAILED &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001225 do_l7_retry(s, s->scb) == 0) {
Christopher Fauletd9769232021-05-26 12:15:37 +02001226 DBG_TRACE_DEVEL("leaving on L7 retry",
1227 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1228 return 0;
1229 }
1230
Olivier Houchard6db16992019-05-17 15:40:49 +02001231 if (txn->flags & TX_NOT_FIRST)
1232 goto abort_keep_alive;
1233
Willy Tarreau4781b152021-04-06 13:53:36 +02001234 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001235 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001236 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001237 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001238 }
1239
Christopher Fauletd9769232021-05-26 12:15:37 +02001240 /* if the server refused the early data, just send a 425 */
1241 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED)
Olivier Houchard865d8392019-05-03 22:46:27 +02001242 txn->status = 425;
Christopher Fauletd9769232021-05-26 12:15:37 +02001243 else {
1244 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001245 stream_inc_http_fail_ctr(s);
Christopher Fauletd9769232021-05-26 12:15:37 +02001246 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001247
Willy Tarreaucb041662022-05-17 19:44:42 +02001248 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001249 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001250
1251 if (!(s->flags & SF_ERR_MASK))
1252 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001253 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001254 DBG_TRACE_DEVEL("leaving on error",
1255 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001256 return 0;
1257 }
1258
Christopher Faulet9768c262018-10-22 09:34:31 +02001259 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001260 else if (rep->flags & CF_READ_TIMEOUT) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001261 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001262 (s->be->retry_type & PR_RE_TIMEOUT)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001263 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001264 DBG_TRACE_DEVEL("leaving on L7 retry",
1265 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001266 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001267 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001268 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001269 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001270 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001271 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001272 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001273 }
1274
Christopher Faulete0768eb2018-10-03 16:38:02 +02001275 txn->status = 504;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001276 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001277 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001278 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001279
1280 if (!(s->flags & SF_ERR_MASK))
1281 s->flags |= SF_ERR_SRVTO;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001282 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001283 DBG_TRACE_DEVEL("leaving on error",
1284 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001285 return 0;
1286 }
1287
Christopher Faulet9768c262018-10-22 09:34:31 +02001288 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001289 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001290 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
1291 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01001292 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001293 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001294 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001295 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001296
Christopher Faulete0768eb2018-10-03 16:38:02 +02001297 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001298 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001299
1300 if (!(s->flags & SF_ERR_MASK))
1301 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001302 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001303
1304 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001305 DBG_TRACE_DEVEL("leaving on error",
1306 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001307 return 0;
1308 }
1309
Christopher Faulet9768c262018-10-22 09:34:31 +02001310 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001311 else if (rep->flags & CF_SHUTR) {
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001312 if ((txn->flags & TX_L7_RETRY) &&
Olivier Houcharda254a372019-04-05 15:30:12 +02001313 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001314 if (co_data(rep) || do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001315 DBG_TRACE_DEVEL("leaving on L7 retry",
1316 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001317 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001318 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001319 }
1320
Olivier Houchard6db16992019-05-17 15:40:49 +02001321 if (txn->flags & TX_NOT_FIRST)
1322 goto abort_keep_alive;
1323
Willy Tarreau4781b152021-04-06 13:53:36 +02001324 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001325 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001326 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001327 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001328 }
1329
Christopher Faulete0768eb2018-10-03 16:38:02 +02001330 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001331 stream_inc_http_fail_ctr(s);
Willy Tarreaucb041662022-05-17 19:44:42 +02001332 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001333 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001334
1335 if (!(s->flags & SF_ERR_MASK))
1336 s->flags |= SF_ERR_SRVCL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001337 http_set_term_flags(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001338 DBG_TRACE_DEVEL("leaving on error",
1339 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001340 return 0;
1341 }
1342
Christopher Faulet9768c262018-10-22 09:34:31 +02001343 /* 5: write error to client (we don't send any message then) */
Christopher Faulet2e56a732023-01-26 16:18:09 +01001344 else if (sc_ep_test(s->scf, SE_FL_ERR_PENDING)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001345 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001346 goto abort_keep_alive;
1347
Willy Tarreau4781b152021-04-06 13:53:36 +02001348 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001349 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001350 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001351 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001352
1353 if (!(s->flags & SF_ERR_MASK))
1354 s->flags |= SF_ERR_CLICL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01001355 http_set_term_flags(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001356
1357 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001358 DBG_TRACE_DEVEL("leaving on error",
1359 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001360 return 0;
1361 }
1362
1363 channel_dont_close(rep);
1364 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001365 DBG_TRACE_DEVEL("waiting for more data",
1366 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001367 return 0;
1368 }
1369
1370 /* More interesting part now : we know that we have a complete
1371 * response which at least looks like HTTP. We have an indicator
1372 * of each header's length, so we can parse them quickly.
1373 */
Christopher Faulet29f17582019-05-23 11:03:26 +02001374 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001375 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001376
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001377 /* Perform a L7 retry because of the status code */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001378 if ((txn->flags & TX_L7_RETRY) &&
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001379 l7_status_match(s->be, sl->info.res.status) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001380 do_l7_retry(s, s->scb) == 0) {
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001381 DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1382 return 0;
1383 }
1384
1385 /* Now, L7 buffer is useless, it can be released */
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001386 b_free(&txn->l7_buffer);
Christopher Faulet1f08bff2021-05-26 13:14:39 +02001387
1388 msg->msg_state = HTTP_MSG_BODY;
1389
1390
Christopher Faulet9768c262018-10-22 09:34:31 +02001391 /* 0: we might have to print this header in debug mode */
1392 if (unlikely((global.mode & MODE_DEBUG) &&
1393 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1394 int32_t pos;
1395
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001396 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001397
Christopher Fauleta3f15502019-05-13 15:27:23 +02001398 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001399 struct htx_blk *blk = htx_get_blk(htx, pos);
1400 enum htx_blk_type type = htx_get_blk_type(blk);
1401
1402 if (type == HTX_BLK_EOH)
1403 break;
1404 if (type != HTX_BLK_HDR)
1405 continue;
1406
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001407 http_debug_hdr("srvhdr", s,
1408 htx_get_blk_name(htx, blk),
1409 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001410 }
1411 }
1412
Christopher Faulet03599112018-11-27 11:21:21 +01001413 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001414 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001415 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001416 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001417 if (sl->flags & HTX_SL_F_XFER_LEN) {
1418 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet2a408542020-11-20 14:22:37 +01001419 if (sl->flags & HTX_SL_F_CLEN)
1420 msg->flags |= HTTP_MSGF_CNT_LEN;
1421 else if (sl->flags & HTX_SL_F_CHNK)
1422 msg->flags |= HTTP_MSGF_TE_CHNK;
Christopher Faulet03599112018-11-27 11:21:21 +01001423 }
Christopher Faulet2a408542020-11-20 14:22:37 +01001424 if (sl->flags & HTX_SL_F_BODYLESS)
1425 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet576c3582021-01-08 15:53:01 +01001426 if (sl->flags & HTX_SL_F_CONN_UPG)
1427 msg->flags |= HTTP_MSGF_CONN_UPG;
Christopher Faulet9768c262018-10-22 09:34:31 +02001428
1429 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001430 if (n < 1 || n > 5)
1431 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001432
Christopher Faulete0768eb2018-10-03 16:38:02 +02001433 /* when the client triggers a 4xx from the server, it's most often due
1434 * to a missing object or permission. These events should be tracked
1435 * because if they happen often, it may indicate a brute force or a
1436 * vulnerability scan.
1437 */
1438 if (n == 4)
1439 stream_inc_http_err_ctr(s);
1440
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001441 if (n == 5 && txn->status != 501 && txn->status != 505)
1442 stream_inc_http_fail_ctr(s);
1443
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001444 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001445 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]);
1446 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001447 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001448
Christopher Faulete0768eb2018-10-03 16:38:02 +02001449 /* Adjust server's health based on status code. Note: status codes 501
1450 * and 505 are triggered on demand by client request, so we must not
1451 * count them as server failures.
1452 */
1453 if (objt_server(s->target)) {
1454 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001455 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001456 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001457 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001458 }
1459
1460 /*
1461 * We may be facing a 100-continue response, or any other informational
1462 * 1xx response which is non-final, in which case this is not the right
1463 * response, and we're waiting for the next one. Let's allow this response
1464 * to go to the client and wait for the next one. There's an exception for
1465 * 101 which is used later in the code to switch protocols.
1466 */
1467 if (txn->status < 200 &&
1468 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001469 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001470 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001471 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001472 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001473 txn->status = 0;
1474 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001475 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001476 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001477 }
1478
Christopher Faulet6e6c7b12021-01-08 16:02:05 +01001479 /* A 101-switching-protocols must contains a Connection header with the
1480 * "upgrade" option and the request too. It means both are agree to
1481 * upgrade. It is not so strict because there is no test on the Upgrade
1482 * header content. But it is probably stronger enough for now.
1483 */
1484 if (txn->status == 101 &&
1485 (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG)))
1486 goto return_bad_res;
1487
Christopher Faulete0768eb2018-10-03 16:38:02 +02001488 /*
1489 * 2: check for cacheability.
1490 */
1491
1492 switch (txn->status) {
1493 case 200:
1494 case 203:
1495 case 204:
1496 case 206:
1497 case 300:
1498 case 301:
1499 case 404:
1500 case 405:
1501 case 410:
1502 case 414:
1503 case 501:
1504 break;
1505 default:
1506 /* RFC7231#6.1:
1507 * Responses with status codes that are defined as
1508 * cacheable by default (e.g., 200, 203, 204, 206,
1509 * 300, 301, 404, 405, 410, 414, and 501 in this
1510 * specification) can be reused by a cache with
1511 * heuristic expiration unless otherwise indicated
1512 * by the method definition or explicit cache
1513 * controls [RFC7234]; all other status codes are
1514 * not cacheable by default.
1515 */
1516 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1517 break;
1518 }
1519
1520 /*
1521 * 3: we may need to capture headers
1522 */
1523 s->logs.logwait &= ~LW_RESP;
1524 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001525 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001526
Christopher Faulet9768c262018-10-22 09:34:31 +02001527 /* Skip parsing if no content length is possible. */
Christopher Fauletc75668e2020-12-07 18:10:32 +01001528 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) ||
Christopher Faulete0768eb2018-10-03 16:38:02 +02001529 txn->status == 101)) {
1530 /* Either we've established an explicit tunnel, or we're
1531 * switching the protocol. In both cases, we're very unlikely
1532 * to understand the next protocols. We have to switch to tunnel
1533 * mode, so that we transfer the request and responses then let
1534 * this protocol pass unmodified. When we later implement specific
1535 * parsers for such protocols, we'll want to check the Upgrade
1536 * header which contains information about that protocol for
1537 * responses with status 101 (eg: see RFC2817 about TLS).
1538 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001539 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001540 }
1541
Christopher Faulet61608322018-11-23 16:23:45 +01001542 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1543 * 407 (Proxy-Authenticate) responses and set the connection to private
1544 */
Willy Tarreaufd9417b2022-05-18 16:23:22 +02001545 srv_conn = sc_conn(s->scb);
Christopher Faulet61608322018-11-23 16:23:45 +01001546 if (srv_conn) {
1547 struct ist hdr;
1548 struct http_hdr_ctx ctx;
1549
1550 if (txn->status == 401)
1551 hdr = ist("WWW-Authenticate");
1552 else if (txn->status == 407)
1553 hdr = ist("Proxy-Authenticate");
1554 else
1555 goto end;
1556
1557 ctx.blk = NULL;
1558 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001559 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1560 * possibly followed by blanks and a base64 string, the connection
1561 * is private. Since it's a mess to deal with, we only check for
1562 * values starting with "NTLM" or "Nego". Note that often multiple
1563 * headers are sent by the server there.
1564 */
1565 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001566 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001567 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001568 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001569 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001570 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001571 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001572 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001573 }
Christopher Faulet61608322018-11-23 16:23:45 +01001574 }
1575 }
1576
1577 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001578 /* we want to have the response time before we start processing it */
1579 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1580
1581 /* end of job, return OK */
1582 rep->analysers &= ~an_bit;
1583 rep->analyse_exp = TICK_ETERNITY;
1584 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001585 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001586 return 1;
1587
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001588 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02001589 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1590 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01001591 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001592 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001593 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001594 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001595 txn->status = 500;
1596 if (!(s->flags & SF_ERR_MASK))
1597 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001598 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001599
1600 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02001601 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet47365272018-10-31 17:40:50 +01001602 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001603 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001604 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001605 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001606 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Christopher Faulete05bf9e2022-03-29 15:23:40 +02001607 (txn->flags & TX_L7_RETRY) &&
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +02001608 do_l7_retry(s, s->scb) == 0) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001609 DBG_TRACE_DEVEL("leaving on L7 retry",
1610 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001611 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001612 }
Christopher Faulet47365272018-10-31 17:40:50 +01001613 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001614 stream_inc_http_fail_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001615 /* fall through */
1616
Christopher Fauletb8a53712019-12-16 11:29:38 +01001617 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001618 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet4a66c942023-01-13 09:43:21 +01001619 http_set_term_flags(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001620
Willy Tarreaucb041662022-05-17 19:44:42 +02001621 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001622 DBG_TRACE_DEVEL("leaving on error",
1623 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001624 return 0;
1625
Christopher Faulete0768eb2018-10-03 16:38:02 +02001626 abort_keep_alive:
1627 /* A keep-alive request to the server failed on a network error.
1628 * The client is required to retry. We need to close without returning
1629 * any other information so that the client retries.
1630 */
1631 txn->status = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001632 s->logs.logwait = 0;
1633 s->logs.level = 0;
1634 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001635 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001636 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1637 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001638 return 0;
1639}
1640
1641/* This function performs all the processing enabled for the current response.
1642 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1643 * and updates s->res.analysers. It might make sense to explode it into several
1644 * other functions. It works like process_request (see indications above).
1645 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001646int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001647{
1648 struct session *sess = s->sess;
1649 struct http_txn *txn = s->txn;
1650 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001651 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001652 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001653 enum rule_result ret = HTTP_RULE_RES_CONT;
1654
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001655 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1656 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001657
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001658 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001659
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001660 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001661
1662 /* The stats applet needs to adjust the Connection header but we don't
1663 * apply any filter there.
1664 */
1665 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1666 rep->analysers &= ~an_bit;
1667 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001668 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001669 }
1670
1671 /*
1672 * We will have to evaluate the filters.
1673 * As opposed to version 1.2, now they will be evaluated in the
1674 * filters order and not in the header order. This means that
1675 * each filter has to be validated among all headers.
1676 *
1677 * Filters are tried with ->be first, then with ->fe if it is
1678 * different from ->be.
1679 *
1680 * Maybe we are in resume condiion. In this case I choose the
1681 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001682 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001683 * the process with the first one.
1684 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001685 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001686 * pointer and the ->fe rule list. If it doesn't match, I initialize
1687 * the loop with the ->be.
1688 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001689 if (s->current_rule_list == &sess->fe->http_res_rules ||
1690 (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001691 cur_proxy = sess->fe;
1692 else
1693 cur_proxy = s->be;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001694
Christopher Faulete0768eb2018-10-03 16:38:02 +02001695 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001696 /* evaluate http-response rules */
Christopher Faulet46f46df2021-11-09 16:33:25 +01001697 if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001698 struct list *def_rules, *rules;
1699
1700 def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL);
1701 rules = &cur_proxy->http_res_rules;
1702
1703 ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001704
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001705 switch (ret) {
1706 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1707 goto return_prx_yield;
1708
1709 case HTTP_RULE_RES_CONT:
1710 case HTTP_RULE_RES_STOP: /* nothing to do */
1711 break;
1712
1713 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1714 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001715
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001716 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1717 goto return_prx_cond;
1718
1719 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001720 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001721
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001722 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1723 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001724
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001725 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1726 goto return_int_err;
1727 }
1728
1729 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001730
Christopher Faulete0768eb2018-10-03 16:38:02 +02001731 /* check whether we're already working on the frontend */
1732 if (cur_proxy == sess->fe)
1733 break;
1734 cur_proxy = sess->fe;
1735 }
1736
Christopher Faulete0768eb2018-10-03 16:38:02 +02001737 /* OK that's all we can do for 1xx responses */
1738 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001739 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001740
1741 /*
1742 * Now check for a server cookie.
1743 */
1744 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001745 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001746
1747 /*
1748 * Check for cache-control or pragma headers if required.
1749 */
1750 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001751 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001752
1753 /*
1754 * Add server cookie in the response if needed
1755 */
1756 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1757 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1758 (!(s->flags & SF_DIRECT) ||
1759 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1760 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1761 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1762 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1763 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1764 !(s->flags & SF_IGNORE_PRST)) {
1765 /* the server is known, it's not the one the client requested, or the
1766 * cookie's last seen date needs to be refreshed. We have to
1767 * insert a set-cookie here, except if we want to insert only on POST
1768 * requests and this one isn't. Note that servers which don't have cookies
1769 * (eg: some backup servers) will return a full cookie removal request.
1770 */
Willy Tarreau88bc8002021-12-06 07:01:02 +00001771 if (!__objt_server(s->target)->cookie) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001772 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001773 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001774 s->be->cookie_name);
1775 }
1776 else {
Willy Tarreau88bc8002021-12-06 07:01:02 +00001777 chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001778
1779 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1780 /* emit last_date, which is mandatory */
1781 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1782 s30tob64((date.tv_sec+3) >> 2,
1783 trash.area + trash.data);
1784 trash.data += 5;
1785
1786 if (s->be->cookie_maxlife) {
1787 /* emit first_date, which is either the original one or
1788 * the current date.
1789 */
1790 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1791 s30tob64(txn->cookie_first_date ?
1792 txn->cookie_first_date >> 2 :
1793 (date.tv_sec+3) >> 2,
1794 trash.area + trash.data);
1795 trash.data += 5;
1796 }
1797 }
1798 chunk_appendf(&trash, "; path=/");
1799 }
1800
1801 if (s->be->cookie_domain)
1802 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1803
1804 if (s->be->ck_opts & PR_CK_HTTPONLY)
1805 chunk_appendf(&trash, "; HttpOnly");
1806
1807 if (s->be->ck_opts & PR_CK_SECURE)
1808 chunk_appendf(&trash, "; Secure");
1809
Christopher Faulet2f533902020-01-21 11:06:48 +01001810 if (s->be->cookie_attrs)
1811 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
1812
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001813 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001814 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001815
1816 txn->flags &= ~TX_SCK_MASK;
1817 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1818 /* the server did not change, only the date was updated */
1819 txn->flags |= TX_SCK_UPDATED;
1820 else
1821 txn->flags |= TX_SCK_INSERTED;
1822
1823 /* Here, we will tell an eventual cache on the client side that we don't
1824 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1825 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1826 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1827 */
1828 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1829
1830 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1831
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001832 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletd649b572022-06-01 17:42:35 +02001833 goto return_fail_rewrite;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001834 }
1835 }
1836
1837 /*
1838 * Check if result will be cacheable with a cookie.
1839 * We'll block the response if security checks have caught
1840 * nasty things such as a cacheable cookie.
1841 */
1842 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
1843 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
1844 (s->be->options & PR_O_CHK_CACHE)) {
1845 /* we're in presence of a cacheable response containing
1846 * a set-cookie header. We'll block it as requested by
1847 * the 'checkcache' option, and send an alert.
1848 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001849 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001850 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Faulete0768eb2018-10-03 16:38:02 +02001851 send_log(s->be, LOG_ALERT,
1852 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau88bc8002021-12-06 07:01:02 +00001853 s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01001854 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001855 }
1856
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001857 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001858 /*
1859 * Evaluate after-response rules before forwarding the response. rules
1860 * from the backend are evaluated first, then one from the frontend if
1861 * it differs.
1862 */
1863 if (!http_eval_after_res_rules(s))
1864 goto return_int_err;
1865
Christopher Fauletc2ac5e42021-03-08 18:20:09 +01001866 /* Filter the response headers if there are filters attached to the
1867 * stream.
1868 */
1869 if (HAS_FILTERS(s))
1870 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
1871
Christopher Faulete0768eb2018-10-03 16:38:02 +02001872 /* Always enter in the body analyzer */
1873 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
1874 rep->analysers |= AN_RES_HTTP_XFER_BODY;
1875
1876 /* if the user wants to log as soon as possible, without counting
1877 * bytes from the server, then this is the right moment. We have
1878 * to temporarily assign bytes_out to log what we currently have.
1879 */
1880 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
1881 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001882 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001883 s->do_log(s);
1884 s->logs.bytes_out = 0;
1885 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001886
Christopher Fauletb8a53712019-12-16 11:29:38 +01001887 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001888 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001889 rep->analysers &= ~an_bit;
1890 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001891 s->current_rule = s->current_rule_list = NULL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001892 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001893
Christopher Fauletb8a53712019-12-16 11:29:38 +01001894 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +02001895 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp);
1896 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +01001897 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02001898 _HA_ATOMIC_INC(&sess->listener->counters->denied_resp);
Christopher Fauleta08546b2019-12-16 16:07:34 +01001899 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001900 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001901 goto return_prx_err;
1902
Christopher Fauletd649b572022-06-01 17:42:35 +02001903 return_fail_rewrite:
1904 if (!(s->flags & SF_ERR_MASK))
1905 s->flags |= SF_ERR_PRXCOND;
1906 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites);
1907 _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites);
1908 if (sess->listener && sess->listener->counters)
1909 _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites);
1910 if (objt_server(s->target))
1911 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites);
1912 /* fall through */
1913
Christopher Fauletb8a53712019-12-16 11:29:38 +01001914 return_int_err:
1915 txn->status = 500;
1916 if (!(s->flags & SF_ERR_MASK))
1917 s->flags |= SF_ERR_INTERNAL;
Willy Tarreau4781b152021-04-06 13:53:36 +02001918 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
1919 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
Dragan Dosen9a006f92021-09-21 13:02:09 +02001920 if (sess->listener && sess->listener->counters)
1921 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001922 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02001923 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001924 goto return_prx_err;
1925
1926 return_bad_res:
1927 txn->status = 502;
Willy Tarreau826f3ab2021-02-10 12:07:15 +01001928 stream_inc_http_fail_ctr(s);
Willy Tarreau4781b152021-04-06 13:53:36 +02001929 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01001930 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02001931 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Fauleta20a6532020-02-05 10:16:41 +01001932 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
1933 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01001934 /* fall through */
1935
1936 return_prx_err:
1937 http_reply_and_close(s, txn->status, http_error_message(s));
1938 /* fall through */
1939
1940 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001941 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreaucb041662022-05-17 19:44:42 +02001942 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001943
Christopher Faulet4a66c942023-01-13 09:43:21 +01001944 http_set_term_flags(s);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001945
Christopher Faulete58c0002020-03-02 16:21:01 +01001946 rep->analysers &= AN_RES_FLT_END;
1947 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001948 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02001949 s->current_rule = s->current_rule_list = NULL;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001950 DBG_TRACE_DEVEL("leaving on error",
1951 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001952 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001953
1954 return_prx_yield:
1955 channel_dont_close(rep);
1956 DBG_TRACE_DEVEL("waiting for more data",
1957 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
1958 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001959}
1960
1961/* This function is an analyser which forwards response body (including chunk
1962 * sizes if any). It is called as soon as we must forward, even if we forward
1963 * zero byte. The only situation where it must not be called is when we're in
1964 * tunnel mode and we want to forward till the close. It's used both to forward
1965 * remaining data and to resync after end of body. It expects the msg_state to
1966 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1967 * read more data, or 1 once we can go on with next request or end the stream.
1968 *
1969 * It is capable of compressing response data both in content-length mode and
1970 * in chunked mode. The state machines follows different flows depending on
1971 * whether content-length and chunked modes are used, since there are no
1972 * trailers in content-length :
1973 *
1974 * chk-mode cl-mode
1975 * ,----- BODY -----.
1976 * / \
1977 * V size > 0 V chk-mode
1978 * .--> SIZE -------------> DATA -------------> CRLF
1979 * | | size == 0 | last byte |
1980 * | v final crlf v inspected |
1981 * | TRAILERS -----------> DONE |
1982 * | |
1983 * `----------------------------------------------'
1984 *
1985 * Compression only happens in the DATA state, and must be flushed in final
1986 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
1987 * is performed at once on final states for all bytes parsed, or when leaving
1988 * on missing data.
1989 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001990int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001991{
1992 struct session *sess = s->sess;
1993 struct http_txn *txn = s->txn;
1994 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001995 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001996 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001997
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001998 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001999
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002000 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002001
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002002 if (htx->flags & HTX_FL_PARSING_ERROR)
2003 goto return_bad_res;
2004 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2005 goto return_int_err;
2006
Christopher Faulet9768c262018-10-22 09:34:31 +02002007 if (msg->msg_state == HTTP_MSG_BODY)
2008 msg->msg_state = HTTP_MSG_DATA;
2009
Christopher Faulete0768eb2018-10-03 16:38:02 +02002010 /* in most states, we should abort in case of early close */
2011 channel_auto_close(res);
2012
Christopher Faulete0768eb2018-10-03 16:38:02 +02002013 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002014 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002015 if (res->flags & CF_EOI)
2016 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002017 }
2018 else {
2019 /* We can't process the buffer's contents yet */
2020 res->flags |= CF_WAKE_WRITE;
2021 goto missing_data_or_waiting;
2022 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002023 }
2024
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002025 if (msg->msg_state >= HTTP_MSG_ENDING)
2026 goto ending;
2027
Christopher Fauletc75668e2020-12-07 18:10:32 +01002028 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002029 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2030 msg->msg_state = HTTP_MSG_ENDING;
2031 goto ending;
2032 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002033
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002034 /* Forward input data. We get it by removing all outgoing data not
2035 * forwarded yet from HTX data size. If there are some data filters, we
2036 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002037 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002038 if (HAS_RSP_DATA_FILTERS(s)) {
2039 ret = flt_http_payload(s, msg, htx->data);
2040 if (ret < 0)
2041 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002042 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002043 }
2044 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002045 c_adv(res, htx->data - co_data(res));
Christopher Faulet2f7c82b2023-02-20 14:06:52 +01002046 if ((global.tune.options & GTUNE_USE_FAST_FWD) && (msg->flags & HTTP_MSGF_XFER_LEN))
Christopher Faulet66af0b22019-03-22 14:54:52 +01002047 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002048 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002049
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002050 if (htx->data != co_data(res))
2051 goto missing_data_or_waiting;
2052
2053 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2054 msg->msg_state = HTTP_MSG_ENDING;
2055 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002056 }
2057
Christopher Faulet9768c262018-10-22 09:34:31 +02002058 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002059 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2060 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002061 */
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002062 if (!(htx->flags & HTX_FL_EOM))
Christopher Faulet9768c262018-10-22 09:34:31 +02002063 goto missing_data_or_waiting;
2064
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002065 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002066
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002067 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002068 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2069
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002070 /* other states, ENDING...TUNNEL */
2071 if (msg->msg_state >= HTTP_MSG_DONE)
2072 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002073
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002074 if (HAS_RSP_DATA_FILTERS(s)) {
2075 ret = flt_http_end(s, msg);
2076 if (ret <= 0) {
2077 if (!ret)
2078 goto missing_data_or_waiting;
2079 goto return_bad_res;
2080 }
2081 }
2082
Christopher Fauletc75668e2020-12-07 18:10:32 +01002083 if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 ||
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002084 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2085 msg->msg_state = HTTP_MSG_TUNNEL;
2086 goto ending;
2087 }
2088 else {
2089 msg->msg_state = HTTP_MSG_DONE;
2090 res->to_forward = 0;
2091 }
2092
2093 done:
2094
2095 channel_dont_close(res);
2096
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002097 if ((res->flags & CF_SHUTW) && co_data(res)) {
2098 /* response errors are most likely due to the client aborting
2099 * the transfer. */
2100 goto return_cli_abort;
2101 }
2102
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002103 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002104 if (!(res->analysers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002105 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002106 return 1;
2107 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002108 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2109 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002110 return 0;
2111
2112 missing_data_or_waiting:
2113 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002114 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002115
2116 /* stop waiting for data if the input is closed before the end. If the
2117 * client side was already closed, it means that the client has aborted,
2118 * so we don't want to count this as a server abort. Otherwise it's a
2119 * server abort.
2120 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002121 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002122 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002123 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002124 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002125 if (htx_is_empty(htx))
2126 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002127 }
2128
Christopher Faulete0768eb2018-10-03 16:38:02 +02002129 /* When TE: chunked is used, we need to get there again to parse
2130 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002131 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2132 * are filters registered on the stream, we don't want to forward a
2133 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002134 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002135 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002136 channel_dont_close(res);
2137
2138 /* We know that more data are expected, but we couldn't send more that
2139 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2140 * system knows it must not set a PUSH on this first part. Interactive
2141 * modes are already handled by the stream sock layer. We must not do
2142 * this in content-length mode because it could present the MSG_MORE
2143 * flag with the last block of forwarded data, which would cause an
2144 * additional delay to be observed by the receiver.
2145 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002146 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002147 res->flags |= CF_EXPECT_MORE;
2148
2149 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002150 DBG_TRACE_DEVEL("waiting for more data to forward",
2151 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002152 return 0;
2153
Christopher Faulet93e02d82019-03-08 14:18:50 +01002154 return_srv_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002155 _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts);
2156 _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002157 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002158 _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002159 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002160 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts);
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002161 stream_inc_http_fail_ctr(s);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002162 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002163 s->flags |= ((res->flags & CF_READ_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002164 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002165
Christopher Faulet93e02d82019-03-08 14:18:50 +01002166 return_cli_abort:
Willy Tarreau4781b152021-04-06 13:53:36 +02002167 _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts);
2168 _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts);
William Lallemand36119de2021-03-08 15:26:48 +01002169 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002170 _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002171 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002172 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002173 if (!(s->flags & SF_ERR_MASK))
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002174 s->flags |= ((res->flags & CF_WRITE_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002175 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002176
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002177 return_int_err:
Willy Tarreau4781b152021-04-06 13:53:36 +02002178 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
2179 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +01002180 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02002181 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002182 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +02002183 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002184 if (!(s->flags & SF_ERR_MASK))
2185 s->flags |= SF_ERR_INTERNAL;
2186 goto return_error;
2187
Christopher Faulet93e02d82019-03-08 14:18:50 +01002188 return_bad_res:
Willy Tarreau4781b152021-04-06 13:53:36 +02002189 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002190 if (objt_server(s->target)) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002191 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002192 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2193 }
Willy Tarreau826f3ab2021-02-10 12:07:15 +01002194 stream_inc_http_fail_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002195 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002196 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002197 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002198
Christopher Faulet93e02d82019-03-08 14:18:50 +01002199 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002200 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002201 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletf2b02cf2023-01-13 11:02:28 +01002202 http_set_term_flags(s);
2203 stream_inc_http_fail_ctr(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002204 DBG_TRACE_DEVEL("leaving on error",
2205 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002206 return 0;
2207}
2208
Christopher Fauletf2824e62018-10-01 12:12:37 +02002209/* Perform an HTTP redirect based on the information in <rule>. The function
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002210 * returns zero in case of an irrecoverable error such as too large a request
2211 * to build a valid response, 1 in case of successful redirect (hence the rule
2212 * is final), or 2 if the rule has to be silently skipped.
Christopher Fauletf2824e62018-10-01 12:12:37 +02002213 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002214int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002215{
Christopher Faulet99daf282018-11-28 22:58:13 +01002216 struct channel *req = &s->req;
2217 struct channel *res = &s->res;
2218 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002219 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002220 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002221 struct ist status, reason, location;
2222 unsigned int flags;
Christopher Fauleteab17572022-04-26 20:34:38 +02002223 int ret = 1, close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002224
2225 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002226 if (!chunk) {
2227 if (!(s->flags & SF_ERR_MASK))
2228 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002229 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002230 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002231
Christopher Faulet99daf282018-11-28 22:58:13 +01002232 /*
2233 * Create the location
2234 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002235 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002236 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002237 case REDIRECT_TYPE_SCHEME: {
2238 struct http_hdr_ctx ctx;
2239 struct ist path, host;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002240 struct http_uri_parser parser;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002241
Christopher Faulet99daf282018-11-28 22:58:13 +01002242 host = ist("");
2243 ctx.blk = NULL;
2244 if (http_find_header(htx, ist("Host"), &ctx, 0))
2245 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002246
Christopher Faulet297fbb42019-05-13 14:41:27 +02002247 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002248 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2249 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002250 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002251 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002252 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2253 int qs = 0;
2254 while (qs < path.len) {
2255 if (*(path.ptr + qs) == '?') {
2256 path.len = qs;
2257 break;
2258 }
2259 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002260 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002261 }
2262 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002263 else
2264 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002265
Christopher Faulet99daf282018-11-28 22:58:13 +01002266 if (rule->rdr_str) { /* this is an old "redirect" rule */
2267 /* add scheme */
2268 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2269 goto fail;
2270 }
2271 else {
2272 /* add scheme with executing log format */
2273 chunk->data += build_logline(s, chunk->area + chunk->data,
2274 chunk->size - chunk->data,
2275 &rule->rdr_fmt);
2276 }
2277 /* add "://" + host + path */
2278 if (!chunk_memcat(chunk, "://", 3) ||
2279 !chunk_memcat(chunk, host.ptr, host.len) ||
2280 !chunk_memcat(chunk, path.ptr, path.len))
2281 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002282
Christopher Faulet99daf282018-11-28 22:58:13 +01002283 /* append a slash at the end of the location if needed and missing */
2284 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2285 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2286 if (chunk->data + 1 >= chunk->size)
2287 goto fail;
2288 chunk->area[chunk->data++] = '/';
2289 }
2290 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002291 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002292
Christopher Faulet99daf282018-11-28 22:58:13 +01002293 case REDIRECT_TYPE_PREFIX: {
2294 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002295 struct http_uri_parser parser;
Christopher Faulet99daf282018-11-28 22:58:13 +01002296
Christopher Faulet297fbb42019-05-13 14:41:27 +02002297 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002298 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2299 path = http_parse_path(&parser);
Christopher Faulet99daf282018-11-28 22:58:13 +01002300 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002301 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002302 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2303 int qs = 0;
2304 while (qs < path.len) {
2305 if (*(path.ptr + qs) == '?') {
2306 path.len = qs;
2307 break;
2308 }
2309 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002310 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002311 }
2312 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002313 else
2314 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002315
Christopher Faulet99daf282018-11-28 22:58:13 +01002316 if (rule->rdr_str) { /* this is an old "redirect" rule */
2317 /* add prefix. Note that if prefix == "/", we don't want to
2318 * add anything, otherwise it makes it hard for the user to
2319 * configure a self-redirection.
2320 */
2321 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2322 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2323 goto fail;
2324 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002325 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002326 else {
2327 /* add prefix with executing log format */
2328 chunk->data += build_logline(s, chunk->area + chunk->data,
2329 chunk->size - chunk->data,
2330 &rule->rdr_fmt);
2331 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002332
Christopher Faulet99daf282018-11-28 22:58:13 +01002333 /* add path */
2334 if (!chunk_memcat(chunk, path.ptr, path.len))
2335 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002336
Christopher Faulet99daf282018-11-28 22:58:13 +01002337 /* append a slash at the end of the location if needed and missing */
2338 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2339 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2340 if (chunk->data + 1 >= chunk->size)
2341 goto fail;
2342 chunk->area[chunk->data++] = '/';
2343 }
2344 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002345 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002346 case REDIRECT_TYPE_LOCATION:
2347 default:
2348 if (rule->rdr_str) { /* this is an old "redirect" rule */
2349 /* add location */
2350 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2351 goto fail;
2352 }
2353 else {
2354 /* add location with executing log format */
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002355 int len = build_logline(s, chunk->area + chunk->data,
2356 chunk->size - chunk->data,
2357 &rule->rdr_fmt);
Christopher Fauleteab17572022-04-26 20:34:38 +02002358 if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) {
2359 ret = 2;
2360 goto out;
2361 }
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002362
2363 chunk->data += len;
Christopher Faulet99daf282018-11-28 22:58:13 +01002364 }
2365 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002366 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002367 location = ist2(chunk->area, chunk->data);
2368
2369 /*
2370 * Create the 30x response
2371 */
2372 switch (rule->code) {
2373 case 308:
2374 status = ist("308");
2375 reason = ist("Permanent Redirect");
2376 break;
2377 case 307:
2378 status = ist("307");
2379 reason = ist("Temporary Redirect");
2380 break;
2381 case 303:
2382 status = ist("303");
2383 reason = ist("See Other");
2384 break;
2385 case 301:
2386 status = ist("301");
2387 reason = ist("Moved Permanently");
2388 break;
2389 case 302:
2390 default:
2391 status = ist("302");
2392 reason = ist("Found");
2393 break;
2394 }
2395
Christopher Faulet08e66462019-05-23 16:44:59 +02002396 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2397 close = 1;
2398
Christopher Faulet99daf282018-11-28 22:58:13 +01002399 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002400 /* Trim any possible response */
2401 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002402 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2403 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2404 if (!sl)
2405 goto fail;
2406 sl->info.res.status = rule->code;
2407 s->txn->status = rule->code;
2408
Christopher Faulet08e66462019-05-23 16:44:59 +02002409 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2410 goto fail;
2411
2412 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002413 !htx_add_header(htx, ist("Location"), location))
2414 goto fail;
2415
2416 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2417 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2418 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002419 }
2420
2421 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002422 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2423 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002424 }
2425
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002426 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet99daf282018-11-28 22:58:13 +01002427 goto fail;
2428
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01002429 htx->flags |= HTX_FL_EOM;
Kevin Zhu96b36392020-01-07 09:42:55 +01002430 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002431 if (!http_forward_proxy_resp(s, 1))
2432 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002433
Christopher Faulet60b33a52020-01-28 09:18:10 +01002434 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2435 /* let's log the request time */
2436 s->logs.tv_request = now;
Christopher Fauletd3475882021-10-04 14:16:46 +02002437 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002438
Christopher Faulet60b33a52020-01-28 09:18:10 +01002439 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Willy Tarreau4781b152021-04-06 13:53:36 +02002440 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req);
Christopher Faulet60b33a52020-01-28 09:18:10 +01002441 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002442
2443 if (!(s->flags & SF_ERR_MASK))
2444 s->flags |= SF_ERR_LOCAL;
Christopher Faulet4a66c942023-01-13 09:43:21 +01002445 http_set_term_flags(s);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002446
Christopher Fauleteab17572022-04-26 20:34:38 +02002447 out:
Christopher Faulet99daf282018-11-28 22:58:13 +01002448 free_trash_chunk(chunk);
Christopher Fauleteab17572022-04-26 20:34:38 +02002449 return ret;
Christopher Faulet99daf282018-11-28 22:58:13 +01002450
2451 fail:
2452 /* If an error occurred, remove the incomplete HTTP response from the
2453 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002454 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauleteab17572022-04-26 20:34:38 +02002455 ret = 0;
2456 goto out;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002457}
2458
Christopher Faulet18c13d32022-05-16 11:43:10 +02002459/* This function filters the request header names to only allow [0-9a-zA-Z-]
2460 * characters. Depending on the proxy configuration, headers with a name not
2461 * matching this charset are removed or the request is rejected with a
2462 * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT
2463 * to continue the request processing or HTTP_RULE_RES_DENY if the request is
2464 * rejected.
2465 */
2466static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px)
2467{
2468 struct htx_blk *blk;
2469 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2470
2471 blk = htx_get_first_blk(htx);
2472 while (blk) {
2473 enum htx_blk_type type = htx_get_blk_type(blk);
2474
2475 if (type == HTX_BLK_HDR) {
2476 struct ist n = htx_get_blk_name(htx, blk);
Mateusz Malek4b85a962022-08-17 14:22:09 +02002477 int i, end = istlen(n);
Christopher Faulet18c13d32022-05-16 11:43:10 +02002478
Mateusz Malek4b85a962022-08-17 14:22:09 +02002479 for (i = 0; i < end; i++) {
Christopher Faulet18c13d32022-05-16 11:43:10 +02002480 if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') {
Mateusz Malek4b85a962022-08-17 14:22:09 +02002481 break;
Christopher Faulet18c13d32022-05-16 11:43:10 +02002482 }
2483 }
Mateusz Malek4b85a962022-08-17 14:22:09 +02002484
2485 if (i < end) {
2486 /* Disallowed character found - block the request or remove the header */
2487 if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK)
2488 goto block;
2489 blk = htx_remove_blk(htx, blk);
2490 continue;
2491 }
Christopher Faulet18c13d32022-05-16 11:43:10 +02002492 }
2493 if (type == HTX_BLK_EOH)
2494 break;
2495
2496 blk = htx_get_next_blk(htx, blk);
2497 }
2498 out:
2499 return rule_ret;
2500 block:
2501 /* Block the request returning a 403-Forbidden response */
2502 s->txn->status = 403;
2503 rule_ret = HTTP_RULE_RES_DENY;
2504 goto out;
2505}
2506
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002507/* Replace all headers matching the name <name>. The header value is replaced if
2508 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2509 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2510 * values are evaluated one by one. It returns 0 on success and -1 on error.
2511 */
2512int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2513 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002514{
2515 struct http_hdr_ctx ctx;
2516 struct buffer *output = get_trash_chunk();
2517
Christopher Faulet72333522018-10-24 11:25:02 +02002518 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002519 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002520 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2521 continue;
2522
2523 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2524 if (output->data == -1)
2525 return -1;
2526 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2527 return -1;
2528 }
2529 return 0;
2530}
2531
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002532/* This function executes one of the set-{method,path,query,uri} actions. It
2533 * takes the string from the variable 'replace' with length 'len', then modifies
2534 * the relevant part of the request line accordingly. Then it updates various
2535 * pointers to the next elements which were moved, and the total buffer length.
2536 * It finds the action to be performed in p[2], previously filled by function
2537 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2538 * error, though this can be revisited when this code is finally exploited.
2539 *
2540 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002541 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002542 *
2543 * In query string case, the mark question '?' must be set at the start of the
2544 * string by the caller, event if the replacement query string is empty.
2545 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002546int http_req_replace_stline(int action, const char *replace, int len,
2547 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002548{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002549 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002550
2551 switch (action) {
2552 case 0: // method
2553 if (!http_replace_req_meth(htx, ist2(replace, len)))
2554 return -1;
2555 break;
2556
2557 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002558 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002559 return -1;
2560 break;
2561
2562 case 2: // query
2563 if (!http_replace_req_query(htx, ist2(replace, len)))
2564 return -1;
2565 break;
2566
2567 case 3: // uri
2568 if (!http_replace_req_uri(htx, ist2(replace, len)))
2569 return -1;
2570 break;
2571
Christopher Faulet312294f2020-09-02 17:17:44 +02002572 case 4: // path + query
2573 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2574 return -1;
2575 break;
2576
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002577 default:
2578 return -1;
2579 }
2580 return 0;
2581}
2582
2583/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002584 * variable <status> contains the new status code. This function never fails. It
2585 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002586 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002587int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002588{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002589 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002590 char *res;
2591
2592 chunk_reset(&trash);
2593 res = ultoa_o(status, trash.area, trash.size);
2594 trash.data = res - trash.area;
2595
2596 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002597 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002598 const char *str = http_get_reason(status);
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01002599 reason = ist(str);
Christopher Faulet96bff762019-12-17 13:46:18 +01002600 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002601
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002602 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002603 return -1;
Willy Tarreau640e2532023-01-10 14:50:44 +01002604 s->txn->status = status;
Christopher Faulete00d06c2019-12-16 17:18:42 +01002605 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002606}
2607
Christopher Faulet3e964192018-10-24 11:39:23 +02002608/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2609 * transaction <txn>. Returns the verdict of the first rule that prevents
2610 * further processing of the request (auth, deny, ...), and defaults to
2611 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2612 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2613 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2614 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2615 * status.
2616 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002617static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules,
2618 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002619{
2620 struct session *sess = strm_sess(s);
2621 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002622 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002623 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002624 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002625
Christopher Faulet3e964192018-10-24 11:39:23 +02002626 /* If "the current_rule_list" match the executed rule list, we are in
2627 * resume condition. If a resume is needed it is always in the action
2628 * and never in the ACL or converters. In this case, we initialise the
2629 * current rule, and go to the action execution point.
2630 */
2631 if (s->current_rule) {
2632 rule = s->current_rule;
2633 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002634 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002635 goto resume_execution;
2636 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002637 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Christopher Faulet3e964192018-10-24 11:39:23 +02002638
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002639 restart:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002640 /* start the ruleset evaluation in strict mode */
2641 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002642
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002643 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002644 /* check optional condition */
2645 if (rule->cond) {
2646 int ret;
2647
2648 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2649 ret = acl_pass(ret);
2650
2651 if (rule->cond->pol == ACL_COND_UNLESS)
2652 ret = !ret;
2653
2654 if (!ret) /* condition not matched */
2655 continue;
2656 }
2657
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002658 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002659 resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002660 if (rule->kw->flags & KWF_EXPERIMENTAL)
2661 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
2662
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002663 /* Always call the action function if defined */
2664 if (rule->action_ptr) {
Christopher Faulet2e56a732023-01-26 16:18:09 +01002665 if (sc_ep_test(s->scf, SE_FL_ERROR) ||
Christopher Faulet446d8032022-12-12 07:53:18 +01002666 ((s->req.flags & CF_SHUTR) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002667 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002668 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002669
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002670 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002671 case ACT_RET_CONT:
2672 break;
2673 case ACT_RET_STOP:
2674 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002675 s->last_rule_file = rule->conf.file;
2676 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002677 goto end;
2678 case ACT_RET_YIELD:
2679 s->current_rule = rule;
2680 rule_ret = HTTP_RULE_RES_YIELD;
2681 goto end;
2682 case ACT_RET_ERR:
2683 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002684 s->last_rule_file = rule->conf.file;
2685 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002686 goto end;
2687 case ACT_RET_DONE:
2688 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002689 s->last_rule_file = rule->conf.file;
2690 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002691 goto end;
2692 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002693 if (txn->status == -1)
2694 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002695 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002696 s->last_rule_file = rule->conf.file;
2697 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002698 goto end;
2699 case ACT_RET_ABRT:
2700 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002701 s->last_rule_file = rule->conf.file;
2702 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002703 goto end;
2704 case ACT_RET_INV:
2705 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002706 s->last_rule_file = rule->conf.file;
2707 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002708 goto end;
2709 }
2710 continue; /* eval the next rule */
2711 }
2712
2713 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002714 switch (rule->action) {
2715 case ACT_ACTION_ALLOW:
2716 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002717 s->last_rule_file = rule->conf.file;
2718 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002719 goto end;
2720
2721 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002722 txn->status = rule->arg.http_reply->status;
2723 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002724 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002725 s->last_rule_file = rule->conf.file;
2726 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002727 goto end;
2728
2729 case ACT_HTTP_REQ_TARPIT:
2730 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002731 txn->status = rule->arg.http_reply->status;
2732 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002733 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002734 s->last_rule_file = rule->conf.file;
2735 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002736 goto end;
2737
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002738 case ACT_HTTP_REDIR: {
2739 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
2740
2741 if (ret == 2) // 2 == skip
2742 break;
2743
2744 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002745 s->last_rule_file = rule->conf.file;
2746 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002747 goto end;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002748 }
Christopher Faulet3e964192018-10-24 11:39:23 +02002749
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002750 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002751 default:
2752 break;
2753 }
2754 }
2755
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002756 if (def_rules && s->current_rule_list == def_rules) {
2757 s->current_rule_list = rules;
2758 goto restart;
2759 }
2760
Christopher Faulet3e964192018-10-24 11:39:23 +02002761 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002762 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002763 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002764 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002765
Christopher Faulet3e964192018-10-24 11:39:23 +02002766 /* we reached the end of the rules, nothing to report */
2767 return rule_ret;
2768}
2769
2770/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2771 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2772 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2773 * is returned, the process can continue the evaluation of next rule list. If
2774 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2775 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002776 * must be returned. If *YIELD is returned, the caller must call again the
2777 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002778 */
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002779static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules,
2780 struct list *rules, struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002781{
2782 struct session *sess = strm_sess(s);
2783 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002784 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002785 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002786 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002787
Christopher Faulet3e964192018-10-24 11:39:23 +02002788 /* If "the current_rule_list" match the executed rule list, we are in
2789 * resume condition. If a resume is needed it is always in the action
2790 * and never in the ACL or converters. In this case, we initialise the
2791 * current rule, and go to the action execution point.
2792 */
2793 if (s->current_rule) {
2794 rule = s->current_rule;
2795 s->current_rule = NULL;
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002796 if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules))
Christopher Faulet3e964192018-10-24 11:39:23 +02002797 goto resume_execution;
2798 }
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002799 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
2800
2801 restart:
Christopher Faulet3e964192018-10-24 11:39:23 +02002802
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002803 /* start the ruleset evaluation in strict mode */
2804 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002805
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002806 list_for_each_entry(rule, s->current_rule_list, list) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002807 /* check optional condition */
2808 if (rule->cond) {
2809 int ret;
2810
2811 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
2812 ret = acl_pass(ret);
2813
2814 if (rule->cond->pol == ACL_COND_UNLESS)
2815 ret = !ret;
2816
2817 if (!ret) /* condition not matched */
2818 continue;
2819 }
2820
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002821 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002822resume_execution:
Amaury Denoyelle03517732021-05-07 14:25:01 +02002823 if (rule->kw->flags & KWF_EXPERIMENTAL)
2824 mark_tainted(TAINTED_ACTION_EXP_EXECUTED);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002825
2826 /* Always call the action function if defined */
2827 if (rule->action_ptr) {
Christopher Faulet2e56a732023-01-26 16:18:09 +01002828 if (sc_ep_test(s->scf, SE_FL_ERROR) ||
Christopher Faulet446d8032022-12-12 07:53:18 +01002829 ((s->req.flags & CF_SHUTR) &&
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002830 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002831 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002832
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002833 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002834 case ACT_RET_CONT:
2835 break;
2836 case ACT_RET_STOP:
2837 rule_ret = HTTP_RULE_RES_STOP;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002838 s->last_rule_file = rule->conf.file;
2839 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002840 goto end;
2841 case ACT_RET_YIELD:
2842 s->current_rule = rule;
2843 rule_ret = HTTP_RULE_RES_YIELD;
2844 goto end;
2845 case ACT_RET_ERR:
2846 rule_ret = HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002847 s->last_rule_file = rule->conf.file;
2848 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002849 goto end;
2850 case ACT_RET_DONE:
2851 rule_ret = HTTP_RULE_RES_DONE;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002852 s->last_rule_file = rule->conf.file;
2853 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002854 goto end;
2855 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002856 if (txn->status == -1)
2857 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002858 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002859 s->last_rule_file = rule->conf.file;
2860 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002861 goto end;
2862 case ACT_RET_ABRT:
2863 rule_ret = HTTP_RULE_RES_ABRT;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002864 s->last_rule_file = rule->conf.file;
2865 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002866 goto end;
2867 case ACT_RET_INV:
2868 rule_ret = HTTP_RULE_RES_BADREQ;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002869 s->last_rule_file = rule->conf.file;
2870 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002871 goto end;
2872 }
2873 continue; /* eval the next rule */
2874 }
2875
2876 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002877 switch (rule->action) {
2878 case ACT_ACTION_ALLOW:
2879 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreauc6dae862022-03-09 17:23:10 +01002880 s->last_rule_file = rule->conf.file;
2881 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002882 goto end;
2883
2884 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002885 txn->status = rule->arg.http_reply->status;
2886 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002887 rule_ret = HTTP_RULE_RES_DENY;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002888 s->last_rule_file = rule->conf.file;
2889 s->last_rule_line = rule->conf.line;
Christopher Faulet3e964192018-10-24 11:39:23 +02002890 goto end;
2891
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002892 case ACT_HTTP_REDIR: {
2893 int ret = http_apply_redirect_rule(rule->arg.redir, s, txn);
Christopher Faulet3e964192018-10-24 11:39:23 +02002894
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002895 if (ret == 2) // 2 == skip
2896 break;
2897
2898 rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR;
Willy Tarreauc6dae862022-03-09 17:23:10 +01002899 s->last_rule_file = rule->conf.file;
2900 s->last_rule_line = rule->conf.line;
Willy Tarreaubc1223b2021-09-02 16:54:33 +02002901 goto end;
2902 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002903 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002904 default:
2905 break;
2906 }
2907 }
2908
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002909 if (def_rules && s->current_rule_list == def_rules) {
2910 s->current_rule_list = rules;
2911 goto restart;
2912 }
2913
Christopher Faulet3e964192018-10-24 11:39:23 +02002914 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002915 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002916 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002917 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002918
Christopher Faulet3e964192018-10-24 11:39:23 +02002919 /* we reached the end of the rules, nothing to report */
2920 return rule_ret;
2921}
2922
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002923/* Executes backend and frontend http-after-response rules for the stream <s>,
2924 * in that order. it return 1 on success and 0 on error. It is the caller
2925 * responsibility to catch error or ignore it. If it catches it, this function
2926 * may be called a second time, for the internal error.
2927 */
2928int http_eval_after_res_rules(struct stream *s)
2929{
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002930 struct list *def_rules, *rules;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002931 struct session *sess = s->sess;
2932 enum rule_result ret = HTTP_RULE_RES_CONT;
2933
Christopher Faulet507479b2020-05-15 12:29:46 +02002934 /* Eval after-response ruleset only if the reply is not const */
2935 if (s->txn->flags & TX_CONST_REPLY)
2936 goto end;
2937
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002938 /* prune the request variables if not already done and swap to the response variables. */
2939 if (s->vars_reqres.scope != SCOPE_RES) {
2940 if (!LIST_ISEMPTY(&s->vars_reqres.head))
2941 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreaub7bfcb32021-08-31 08:13:25 +02002942 vars_init_head(&s->vars_reqres, SCOPE_RES);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002943 }
2944
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002945 def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL);
2946 rules = &s->be->http_after_res_rules;
2947
2948 ret = http_res_get_intercept_rule(s->be, def_rules, rules, s);
Christopher Faulet4c5a5912021-11-09 17:48:39 +01002949 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) {
Christopher Fauletd4150ad2021-10-13 15:35:55 +02002950 def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL);
2951 rules = &sess->fe->http_after_res_rules;
2952 ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s);
2953 }
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002954
Christopher Faulet507479b2020-05-15 12:29:46 +02002955 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002956 /* All other codes than CONTINUE, STOP or DONE are forbidden */
2957 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
2958}
2959
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002960/*
2961 * Manage client-side cookie. It can impact performance by about 2% so it is
2962 * desirable to call it only when needed. This code is quite complex because
2963 * of the multiple very crappy and ambiguous syntaxes we have to support. it
2964 * highly recommended not to touch this part without a good reason !
2965 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002966static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002967{
2968 struct session *sess = s->sess;
2969 struct http_txn *txn = s->txn;
2970 struct htx *htx;
2971 struct http_hdr_ctx ctx;
2972 char *hdr_beg, *hdr_end, *del_from;
2973 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
2974 int preserve_hdr;
2975
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002976 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002977 ctx.blk = NULL;
2978 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02002979 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02002980 del_from = NULL; /* nothing to be deleted */
2981 preserve_hdr = 0; /* assume we may kill the whole header */
2982
2983 /* Now look for cookies. Conforming to RFC2109, we have to support
2984 * attributes whose name begin with a '$', and associate them with
2985 * the right cookie, if we want to delete this cookie.
2986 * So there are 3 cases for each cookie read :
2987 * 1) it's a special attribute, beginning with a '$' : ignore it.
2988 * 2) it's a server id cookie that we *MAY* want to delete : save
2989 * some pointers on it (last semi-colon, beginning of cookie...)
2990 * 3) it's an application cookie : we *MAY* have to delete a previous
2991 * "special" cookie.
2992 * At the end of loop, if a "special" cookie remains, we may have to
2993 * remove it. If no application cookie persists in the header, we
2994 * *MUST* delete it.
2995 *
2996 * Note: RFC2965 is unclear about the processing of spaces around
2997 * the equal sign in the ATTR=VALUE form. A careful inspection of
2998 * the RFC explicitly allows spaces before it, and not within the
2999 * tokens (attrs or values). An inspection of RFC2109 allows that
3000 * too but section 10.1.3 lets one think that spaces may be allowed
3001 * after the equal sign too, resulting in some (rare) buggy
3002 * implementations trying to do that. So let's do what servers do.
3003 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3004 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003005 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003006 * causes parsing to become ambiguous. Browsers also allow spaces
3007 * within values even without quotes.
3008 *
3009 * We have to keep multiple pointers in order to support cookie
3010 * removal at the beginning, middle or end of header without
3011 * corrupting the header. All of these headers are valid :
3012 *
3013 * hdr_beg hdr_end
3014 * | |
3015 * v |
3016 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3017 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3018 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3019 * | | | | | | |
3020 * | | | | | | |
3021 * | | | | | | +--> next
3022 * | | | | | +----> val_end
3023 * | | | | +-----------> val_beg
3024 * | | | +--------------> equal
3025 * | | +----------------> att_end
3026 * | +---------------------> att_beg
3027 * +--------------------------> prev
3028 *
3029 */
3030 hdr_beg = ctx.value.ptr;
3031 hdr_end = hdr_beg + ctx.value.len;
3032 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3033 /* Iterate through all cookies on this line */
3034
3035 /* find att_beg */
3036 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003037 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003038 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003039 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003040
3041 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3042 att_beg++;
3043
3044 /* find att_end : this is the first character after the last non
3045 * space before the equal. It may be equal to hdr_end.
3046 */
3047 equal = att_end = att_beg;
3048 while (equal < hdr_end) {
3049 if (*equal == '=' || *equal == ',' || *equal == ';')
3050 break;
3051 if (HTTP_IS_SPHT(*equal++))
3052 continue;
3053 att_end = equal;
3054 }
3055
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003056 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003057 * is between <att_beg> and <equal>, both may be identical.
3058 */
3059 /* look for end of cookie if there is an equal sign */
3060 if (equal < hdr_end && *equal == '=') {
3061 /* look for the beginning of the value */
3062 val_beg = equal + 1;
3063 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3064 val_beg++;
3065
3066 /* find the end of the value, respecting quotes */
3067 next = http_find_cookie_value_end(val_beg, hdr_end);
3068
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003069 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003070 val_end = next;
3071 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3072 val_end--;
3073 }
3074 else
3075 val_beg = val_end = next = equal;
3076
3077 /* We have nothing to do with attributes beginning with
3078 * '$'. However, they will automatically be removed if a
3079 * header before them is removed, since they're supposed
3080 * to be linked together.
3081 */
3082 if (*att_beg == '$')
3083 continue;
3084
3085 /* Ignore cookies with no equal sign */
3086 if (equal == next) {
3087 /* This is not our cookie, so we must preserve it. But if we already
3088 * scheduled another cookie for removal, we cannot remove the
3089 * complete header, but we can remove the previous block itself.
3090 */
3091 preserve_hdr = 1;
3092 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003093 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003094 val_end += delta;
3095 next += delta;
3096 hdr_end += delta;
3097 prev = del_from;
3098 del_from = NULL;
3099 }
3100 continue;
3101 }
3102
3103 /* if there are spaces around the equal sign, we need to
3104 * strip them otherwise we'll get trouble for cookie captures,
3105 * or even for rewrites. Since this happens extremely rarely,
3106 * it does not hurt performance.
3107 */
3108 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3109 int stripped_before = 0;
3110 int stripped_after = 0;
3111
3112 if (att_end != equal) {
3113 memmove(att_end, equal, hdr_end - equal);
3114 stripped_before = (att_end - equal);
3115 equal += stripped_before;
3116 val_beg += stripped_before;
3117 }
3118
3119 if (val_beg > equal + 1) {
3120 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3121 stripped_after = (equal + 1) - val_beg;
3122 val_beg += stripped_after;
3123 stripped_before += stripped_after;
3124 }
3125
3126 val_end += stripped_before;
3127 next += stripped_before;
3128 hdr_end += stripped_before;
3129 }
3130 /* now everything is as on the diagram above */
3131
3132 /* First, let's see if we want to capture this cookie. We check
3133 * that we don't already have a client side cookie, because we
3134 * can only capture one. Also as an optimisation, we ignore
3135 * cookies shorter than the declared name.
3136 */
3137 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3138 (val_end - att_beg >= sess->fe->capture_namelen) &&
3139 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3140 int log_len = val_end - att_beg;
3141
3142 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3143 ha_alert("HTTP logging : out of memory.\n");
3144 } else {
3145 if (log_len > sess->fe->capture_len)
3146 log_len = sess->fe->capture_len;
3147 memcpy(txn->cli_cookie, att_beg, log_len);
3148 txn->cli_cookie[log_len] = 0;
3149 }
3150 }
3151
3152 /* Persistence cookies in passive, rewrite or insert mode have the
3153 * following form :
3154 *
3155 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3156 *
3157 * For cookies in prefix mode, the form is :
3158 *
3159 * Cookie: NAME=SRV~VALUE
3160 */
3161 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3162 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3163 struct server *srv = s->be->srv;
3164 char *delim;
3165
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003166 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003167 * have the server ID between val_beg and delim, and the original cookie between
3168 * delim+1 and val_end. Otherwise, delim==val_end :
3169 *
3170 * hdr_beg
3171 * |
3172 * v
3173 * NAME=SRV; # in all but prefix modes
3174 * NAME=SRV~OPAQUE ; # in prefix mode
3175 * || || | |+-> next
3176 * || || | +--> val_end
3177 * || || +---------> delim
3178 * || |+------------> val_beg
3179 * || +-------------> att_end = equal
3180 * |+-----------------> att_beg
3181 * +------------------> prev
3182 *
3183 */
3184 if (s->be->ck_opts & PR_CK_PFX) {
3185 for (delim = val_beg; delim < val_end; delim++)
3186 if (*delim == COOKIE_DELIM)
3187 break;
3188 }
3189 else {
3190 char *vbar1;
3191 delim = val_end;
3192 /* Now check if the cookie contains a date field, which would
3193 * appear after a vertical bar ('|') just after the server name
3194 * and before the delimiter.
3195 */
3196 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3197 if (vbar1) {
3198 /* OK, so left of the bar is the server's cookie and
3199 * right is the last seen date. It is a base64 encoded
3200 * 30-bit value representing the UNIX date since the
3201 * epoch in 4-second quantities.
3202 */
3203 int val;
3204 delim = vbar1++;
3205 if (val_end - vbar1 >= 5) {
3206 val = b64tos30(vbar1);
3207 if (val > 0)
3208 txn->cookie_last_date = val << 2;
3209 }
3210 /* look for a second vertical bar */
3211 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3212 if (vbar1 && (val_end - vbar1 > 5)) {
3213 val = b64tos30(vbar1 + 1);
3214 if (val > 0)
3215 txn->cookie_first_date = val << 2;
3216 }
3217 }
3218 }
3219
3220 /* if the cookie has an expiration date and the proxy wants to check
3221 * it, then we do that now. We first check if the cookie is too old,
3222 * then only if it has expired. We detect strict overflow because the
3223 * time resolution here is not great (4 seconds). Cookies with dates
3224 * in the future are ignored if their offset is beyond one day. This
3225 * allows an admin to fix timezone issues without expiring everyone
3226 * and at the same time avoids keeping unwanted side effects for too
3227 * long.
3228 */
3229 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3230 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3231 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3232 txn->flags &= ~TX_CK_MASK;
3233 txn->flags |= TX_CK_OLD;
3234 delim = val_beg; // let's pretend we have not found the cookie
3235 txn->cookie_first_date = 0;
3236 txn->cookie_last_date = 0;
3237 }
3238 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3239 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3240 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3241 txn->flags &= ~TX_CK_MASK;
3242 txn->flags |= TX_CK_EXPIRED;
3243 delim = val_beg; // let's pretend we have not found the cookie
3244 txn->cookie_first_date = 0;
3245 txn->cookie_last_date = 0;
3246 }
3247
3248 /* Here, we'll look for the first running server which supports the cookie.
3249 * This allows to share a same cookie between several servers, for example
3250 * to dedicate backup servers to specific servers only.
3251 * However, to prevent clients from sticking to cookie-less backup server
3252 * when they have incidentely learned an empty cookie, we simply ignore
3253 * empty cookies and mark them as invalid.
3254 * The same behaviour is applied when persistence must be ignored.
3255 */
3256 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3257 srv = NULL;
3258
3259 while (srv) {
3260 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3261 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3262 if ((srv->cur_state != SRV_ST_STOPPED) ||
3263 (s->be->options & PR_O_PERSIST) ||
3264 (s->flags & SF_FORCE_PRST)) {
3265 /* we found the server and we can use it */
3266 txn->flags &= ~TX_CK_MASK;
3267 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3268 s->flags |= SF_DIRECT | SF_ASSIGNED;
3269 s->target = &srv->obj_type;
3270 break;
3271 } else {
3272 /* we found a server, but it's down,
3273 * mark it as such and go on in case
3274 * another one is available.
3275 */
3276 txn->flags &= ~TX_CK_MASK;
3277 txn->flags |= TX_CK_DOWN;
3278 }
3279 }
3280 srv = srv->next;
3281 }
3282
3283 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3284 /* no server matched this cookie or we deliberately skipped it */
3285 txn->flags &= ~TX_CK_MASK;
3286 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3287 txn->flags |= TX_CK_UNUSED;
3288 else
3289 txn->flags |= TX_CK_INVALID;
3290 }
3291
3292 /* depending on the cookie mode, we may have to either :
3293 * - delete the complete cookie if we're in insert+indirect mode, so that
3294 * the server never sees it ;
3295 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003296 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003297 * if we're in cookie prefix mode
3298 */
3299 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3300 int delta; /* negative */
3301
3302 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3303 delta = val_beg - (delim + 1);
3304 val_end += delta;
3305 next += delta;
3306 hdr_end += delta;
3307 del_from = NULL;
3308 preserve_hdr = 1; /* we want to keep this cookie */
3309 }
3310 else if (del_from == NULL &&
3311 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3312 del_from = prev;
3313 }
3314 }
3315 else {
3316 /* This is not our cookie, so we must preserve it. But if we already
3317 * scheduled another cookie for removal, we cannot remove the
3318 * complete header, but we can remove the previous block itself.
3319 */
3320 preserve_hdr = 1;
3321
3322 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003323 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003324 if (att_beg >= del_from)
3325 att_beg += delta;
3326 if (att_end >= del_from)
3327 att_end += delta;
3328 val_beg += delta;
3329 val_end += delta;
3330 next += delta;
3331 hdr_end += delta;
3332 prev = del_from;
3333 del_from = NULL;
3334 }
3335 }
3336
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003337 } /* for each cookie */
3338
3339
3340 /* There are no more cookies on this line.
3341 * We may still have one (or several) marked for deletion at the
3342 * end of the line. We must do this now in two ways :
3343 * - if some cookies must be preserved, we only delete from the
3344 * mark to the end of line ;
3345 * - if nothing needs to be preserved, simply delete the whole header
3346 */
3347 if (del_from) {
3348 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3349 }
3350 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003351 if (hdr_beg != hdr_end)
3352 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003353 else
3354 http_remove_header(htx, &ctx);
3355 }
3356 } /* for each "Cookie header */
3357}
3358
3359/*
3360 * Manage server-side cookies. It can impact performance by about 2% so it is
3361 * desirable to call it only when needed. This function is also used when we
3362 * just need to know if there is a cookie (eg: for check-cache).
3363 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003364static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003365{
3366 struct session *sess = s->sess;
3367 struct http_txn *txn = s->txn;
3368 struct htx *htx;
3369 struct http_hdr_ctx ctx;
3370 struct server *srv;
3371 char *hdr_beg, *hdr_end;
3372 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003373
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003374 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003375
3376 ctx.blk = NULL;
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003377 while (http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003378 int is_first = 1;
3379
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003380 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3381 * <prev> points to the colon.
3382 */
3383 txn->flags |= TX_SCK_PRESENT;
3384
3385 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3386 * check-cache is enabled) and we are not interested in checking
3387 * them. Warning, the cookie capture is declared in the frontend.
3388 */
3389 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3390 break;
3391
3392 /* OK so now we know we have to process this response cookie.
3393 * The format of the Set-Cookie header is slightly different
3394 * from the format of the Cookie header in that it does not
3395 * support the comma as a cookie delimiter (thus the header
3396 * cannot be folded) because the Expires attribute described in
3397 * the original Netscape's spec may contain an unquoted date
3398 * with a comma inside. We have to live with this because
3399 * many browsers don't support Max-Age and some browsers don't
3400 * support quoted strings. However the Set-Cookie2 header is
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003401 * clean but basically nobody supports it.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003402 *
3403 * We have to keep multiple pointers in order to support cookie
3404 * removal at the beginning, middle or end of header without
3405 * corrupting the header (in case of set-cookie2). A special
3406 * pointer, <scav> points to the beginning of the set-cookie-av
3407 * fields after the first semi-colon. The <next> pointer points
3408 * either to the end of line (set-cookie) or next unquoted comma
3409 * (set-cookie2). All of these headers are valid :
3410 *
3411 * hdr_beg hdr_end
3412 * | |
3413 * v |
3414 * NAME1 = VALUE 1 ; Secure; Path="/" |
3415 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3416 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3417 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3418 * | | | | | | | |
3419 * | | | | | | | +-> next
3420 * | | | | | | +------------> scav
3421 * | | | | | +--------------> val_end
3422 * | | | | +--------------------> val_beg
3423 * | | | +----------------------> equal
3424 * | | +------------------------> att_end
3425 * | +----------------------------> att_beg
3426 * +------------------------------> prev
3427 * -------------------------------> hdr_beg
3428 */
3429 hdr_beg = ctx.value.ptr;
3430 hdr_end = hdr_beg + ctx.value.len;
3431 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3432
3433 /* Iterate through all cookies on this line */
3434
3435 /* find att_beg */
3436 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003437 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003438 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003439 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003440
3441 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3442 att_beg++;
3443
3444 /* find att_end : this is the first character after the last non
3445 * space before the equal. It may be equal to hdr_end.
3446 */
3447 equal = att_end = att_beg;
3448
3449 while (equal < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003450 if (*equal == '=' || *equal == ';')
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003451 break;
3452 if (HTTP_IS_SPHT(*equal++))
3453 continue;
3454 att_end = equal;
3455 }
3456
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003457 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003458 * is between <att_beg> and <equal>, both may be identical.
3459 */
3460
3461 /* look for end of cookie if there is an equal sign */
3462 if (equal < hdr_end && *equal == '=') {
3463 /* look for the beginning of the value */
3464 val_beg = equal + 1;
3465 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3466 val_beg++;
3467
3468 /* find the end of the value, respecting quotes */
3469 next = http_find_cookie_value_end(val_beg, hdr_end);
3470
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003471 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003472 val_end = next;
3473 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3474 val_end--;
3475 }
3476 else {
3477 /* <equal> points to next comma, semi-colon or EOL */
3478 val_beg = val_end = next = equal;
3479 }
3480
3481 if (next < hdr_end) {
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003482 /* For Set-Cookie, since commas are permitted
3483 * in values, skip to the end.
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003484 */
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003485 next = hdr_end;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003486 }
3487
3488 /* Now everything is as on the diagram above */
3489
3490 /* Ignore cookies with no equal sign */
3491 if (equal == val_end)
3492 continue;
3493
3494 /* If there are spaces around the equal sign, we need to
3495 * strip them otherwise we'll get trouble for cookie captures,
3496 * or even for rewrites. Since this happens extremely rarely,
3497 * it does not hurt performance.
3498 */
3499 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3500 int stripped_before = 0;
3501 int stripped_after = 0;
3502
3503 if (att_end != equal) {
3504 memmove(att_end, equal, hdr_end - equal);
3505 stripped_before = (att_end - equal);
3506 equal += stripped_before;
3507 val_beg += stripped_before;
3508 }
3509
3510 if (val_beg > equal + 1) {
3511 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3512 stripped_after = (equal + 1) - val_beg;
3513 val_beg += stripped_after;
3514 stripped_before += stripped_after;
3515 }
3516
3517 val_end += stripped_before;
3518 next += stripped_before;
3519 hdr_end += stripped_before;
3520
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003521 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003522 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003523 }
3524
3525 /* First, let's see if we want to capture this cookie. We check
3526 * that we don't already have a server side cookie, because we
3527 * can only capture one. Also as an optimisation, we ignore
3528 * cookies shorter than the declared name.
3529 */
3530 if (sess->fe->capture_name != NULL &&
3531 txn->srv_cookie == NULL &&
3532 (val_end - att_beg >= sess->fe->capture_namelen) &&
3533 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3534 int log_len = val_end - att_beg;
3535 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3536 ha_alert("HTTP logging : out of memory.\n");
3537 }
3538 else {
3539 if (log_len > sess->fe->capture_len)
3540 log_len = sess->fe->capture_len;
3541 memcpy(txn->srv_cookie, att_beg, log_len);
3542 txn->srv_cookie[log_len] = 0;
3543 }
3544 }
3545
3546 srv = objt_server(s->target);
3547 /* now check if we need to process it for persistence */
3548 if (!(s->flags & SF_IGNORE_PRST) &&
3549 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3550 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3551 /* assume passive cookie by default */
3552 txn->flags &= ~TX_SCK_MASK;
3553 txn->flags |= TX_SCK_FOUND;
3554
3555 /* If the cookie is in insert mode on a known server, we'll delete
3556 * this occurrence because we'll insert another one later.
3557 * We'll delete it too if the "indirect" option is set and we're in
3558 * a direct access.
3559 */
3560 if (s->be->ck_opts & PR_CK_PSV) {
3561 /* The "preserve" flag was set, we don't want to touch the
3562 * server's cookie.
3563 */
3564 }
3565 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3566 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3567 /* this cookie must be deleted */
3568 if (prev == hdr_beg && next == hdr_end) {
3569 /* whole header */
3570 http_remove_header(htx, &ctx);
3571 /* note: while both invalid now, <next> and <hdr_end>
3572 * are still equal, so the for() will stop as expected.
3573 */
3574 } else {
3575 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003576 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003577 next = prev;
3578 hdr_end += delta;
3579 }
3580 txn->flags &= ~TX_SCK_MASK;
3581 txn->flags |= TX_SCK_DELETED;
3582 /* and go on with next cookie */
3583 }
3584 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3585 /* replace bytes val_beg->val_end with the cookie name associated
3586 * with this server since we know it.
3587 */
3588 int sliding, delta;
3589
3590 ctx.value = ist2(val_beg, val_end - val_beg);
3591 ctx.lws_before = ctx.lws_after = 0;
3592 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3593 delta = srv->cklen - (val_end - val_beg);
3594 sliding = (ctx.value.ptr - val_beg);
3595 hdr_beg += sliding;
3596 val_beg += sliding;
3597 next += sliding + delta;
3598 hdr_end += sliding + delta;
3599
3600 txn->flags &= ~TX_SCK_MASK;
3601 txn->flags |= TX_SCK_REPLACED;
3602 }
3603 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3604 /* insert the cookie name associated with this server
3605 * before existing cookie, and insert a delimiter between them..
3606 */
3607 int sliding, delta;
3608 ctx.value = ist2(val_beg, 0);
3609 ctx.lws_before = ctx.lws_after = 0;
3610 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3611 delta = srv->cklen + 1;
3612 sliding = (ctx.value.ptr - val_beg);
3613 hdr_beg += sliding;
3614 val_beg += sliding;
3615 next += sliding + delta;
3616 hdr_end += sliding + delta;
3617
3618 val_beg[srv->cklen] = COOKIE_DELIM;
3619 txn->flags &= ~TX_SCK_MASK;
3620 txn->flags |= TX_SCK_REPLACED;
3621 }
3622 }
3623 /* that's done for this cookie, check the next one on the same
Willy Tarreauaa1909e2022-11-14 18:58:35 +01003624 * line when next != hdr_end (which should normally not happen
3625 * with set-cookie2 support removed).
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003626 */
3627 }
3628 }
3629}
3630
Christopher Faulet25a02f62018-10-24 12:00:25 +02003631/*
3632 * Parses the Cache-Control and Pragma request header fields to determine if
3633 * the request may be served from the cache and/or if it is cacheable. Updates
3634 * s->txn->flags.
3635 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003636void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003637{
3638 struct http_txn *txn = s->txn;
3639 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003640 struct http_hdr_ctx ctx = { .blk = NULL };
3641 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003642
3643 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3644 return; /* nothing more to do here */
3645
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003646 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003647 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003648
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003649 /* Check "pragma" header for HTTP/1.0 compatibility. */
3650 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3651 if (isteqi(ctx.value, ist("no-cache"))) {
3652 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003653 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003654 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003655
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003656 ctx.blk = NULL;
3657 /* Don't use the cache and don't try to store if we found the
3658 * Authorization header */
3659 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3660 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3661 txn->flags |= TX_CACHE_IGNORE;
3662 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003663
Christopher Faulet25a02f62018-10-24 12:00:25 +02003664
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003665 /* Look for "cache-control" header and iterate over all the values
3666 * until we find one that specifies that caching is possible or not. */
3667 ctx.blk = NULL;
3668 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003669 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003670 /* We don't check the values after max-age, max-stale nor min-fresh,
3671 * we simply don't use the cache when they're specified. */
3672 if (istmatchi(ctx.value, ist("max-age")) ||
3673 istmatchi(ctx.value, ist("no-cache")) ||
3674 istmatchi(ctx.value, ist("max-stale")) ||
3675 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003676 txn->flags |= TX_CACHE_IGNORE;
3677 continue;
3678 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003679 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003680 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3681 continue;
3682 }
3683 }
3684
3685 /* RFC7234#5.4:
3686 * When the Cache-Control header field is also present and
3687 * understood in a request, Pragma is ignored.
3688 * When the Cache-Control header field is not present in a
3689 * request, caches MUST consider the no-cache request
3690 * pragma-directive as having the same effect as if
3691 * "Cache-Control: no-cache" were present.
3692 */
3693 if (!cc_found && pragma_found)
3694 txn->flags |= TX_CACHE_IGNORE;
3695}
3696
3697/*
3698 * Check if response is cacheable or not. Updates s->txn->flags.
3699 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003700void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003701{
3702 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003703 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003704 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003705 int has_freshness_info = 0;
3706 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003707
3708 if (txn->status < 200) {
3709 /* do not try to cache interim responses! */
3710 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3711 return;
3712 }
3713
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003714 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003715 /* Check "pragma" header for HTTP/1.0 compatibility. */
3716 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3717 if (isteqi(ctx.value, ist("no-cache"))) {
3718 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3719 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003720 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003721 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003722
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003723 /* Look for "cache-control" header and iterate over all the values
3724 * until we find one that specifies that caching is possible or not. */
3725 ctx.blk = NULL;
3726 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3727 if (isteqi(ctx.value, ist("public"))) {
3728 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003729 continue;
3730 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003731 if (isteqi(ctx.value, ist("private")) ||
3732 isteqi(ctx.value, ist("no-cache")) ||
3733 isteqi(ctx.value, ist("no-store")) ||
3734 isteqi(ctx.value, ist("max-age=0")) ||
3735 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003736 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003737 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003738 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003739 /* We might have a no-cache="set-cookie" form. */
3740 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3741 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003742 continue;
3743 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003744
3745 if (istmatchi(ctx.value, ist("s-maxage")) ||
3746 istmatchi(ctx.value, ist("max-age"))) {
3747 has_freshness_info = 1;
3748 continue;
3749 }
3750 }
3751
3752 /* If no freshness information could be found in Cache-Control values,
3753 * look for an Expires header. */
3754 if (!has_freshness_info) {
3755 ctx.blk = NULL;
3756 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003757 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003758
3759 /* If no freshness information could be found in Cache-Control or Expires
3760 * values, look for an explicit validator. */
3761 if (!has_freshness_info) {
3762 ctx.blk = NULL;
3763 has_validator = 1;
3764 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3765 ctx.blk = NULL;
3766 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3767 has_validator = 0;
3768 }
3769 }
3770
3771 /* We won't store an entry that has neither a cache validator nor an
3772 * explicit expiration time, as suggested in RFC 7234#3. */
3773 if (!has_freshness_info && !has_validator)
Remi Tricot-Le Breton879debe2023-02-21 11:47:17 +01003774 txn->flags &= ~TX_CACHEABLE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003775}
3776
Christopher Faulet377c5a52018-10-24 21:21:30 +02003777/*
3778 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3779 * for the current backend.
3780 *
3781 * It is assumed that the request is either a HEAD, GET, or POST and that the
3782 * uri_auth field is valid.
3783 *
3784 * Returns 1 if stats should be provided, otherwise 0.
3785 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003786static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003787{
3788 struct uri_auth *uri_auth = backend->uri_auth;
3789 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003790 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003791 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003792
3793 if (!uri_auth)
3794 return 0;
3795
3796 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3797 return 0;
3798
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003799 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003800 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003801 uri = htx_sl_req_uri(sl);
Amaury Denoyellec453f952021-07-06 11:40:12 +02003802 if (*uri_auth->uri_prefix == '/') {
3803 struct http_uri_parser parser = http_uri_parser_init(uri);
3804 uri = http_parse_path(&parser);
3805 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003806
3807 /* check URI size */
3808 if (uri_auth->uri_len > uri.len)
3809 return 0;
3810
3811 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3812 return 0;
3813
3814 return 1;
3815}
3816
3817/* This function prepares an applet to handle the stats. It can deal with the
3818 * "100-continue" expectation, check that admin rules are met for POST requests,
3819 * and program a response message if something was unexpected. It cannot fail
3820 * and always relies on the stats applet to complete the job. It does not touch
3821 * analysers nor counters, which are left to the caller. It does not touch
3822 * s->target which is supposed to already point to the stats applet. The caller
3823 * is expected to have already assigned an appctx to the stream.
3824 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003825static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003826{
3827 struct stats_admin_rule *stats_admin_rule;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003828 struct session *sess = s->sess;
3829 struct http_txn *txn = s->txn;
3830 struct http_msg *msg = &txn->req;
3831 struct uri_auth *uri_auth = s->be->uri_auth;
3832 const char *h, *lookup, *end;
Willy Tarreau8e7c6e62022-05-18 17:58:02 +02003833 struct appctx *appctx = __sc_appctx(s->scb);
Willy Tarreau91cefca2022-05-03 17:08:29 +02003834 struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Christopher Faulet377c5a52018-10-24 21:21:30 +02003835 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003836 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003837
Willy Tarreau41f88522022-05-03 18:39:27 +02003838 appctx->st1 = 0;
Willy Tarreau6ef16482022-05-06 18:07:53 +02003839 ctx->state = STAT_STATE_INIT;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003840 ctx->st_code = STAT_STATUS_INIT;
3841 ctx->flags |= uri_auth->flags;
3842 ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003843 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
Willy Tarreau91cefca2022-05-03 17:08:29 +02003844 ctx->flags |= STAT_CHUNKED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003845
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003846 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003847 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003848 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
3849 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003850
3851 for (h = lookup; h <= end - 3; h++) {
3852 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003853 ctx->flags |= STAT_HIDE_DOWN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003854 break;
3855 }
Amaury Denoyelle91e55ea2021-02-25 14:46:08 +01003856 }
3857
3858 for (h = lookup; h <= end - 9; h++) {
3859 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003860 ctx->flags |= STAT_HIDE_MAINT;
Willy Tarreau3e320362020-10-23 17:28:57 +02003861 break;
3862 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003863 }
3864
3865 if (uri_auth->refresh) {
3866 for (h = lookup; h <= end - 10; h++) {
3867 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003868 ctx->flags |= STAT_NO_REFRESH;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003869 break;
3870 }
3871 }
3872 }
3873
3874 for (h = lookup; h <= end - 4; h++) {
3875 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003876 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003877 break;
3878 }
3879 }
3880
3881 for (h = lookup; h <= end - 6; h++) {
3882 if (memcmp(h, ";typed", 6) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003883 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3884 ctx->flags |= STAT_FMT_TYPED;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003885 break;
3886 }
3887 }
3888
Christopher Faulet6338a082019-09-09 15:50:54 +02003889 for (h = lookup; h <= end - 5; h++) {
3890 if (memcmp(h, ";json", 5) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003891 ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
3892 ctx->flags |= STAT_FMT_JSON;
Christopher Faulet6338a082019-09-09 15:50:54 +02003893 break;
3894 }
3895 }
3896
3897 for (h = lookup; h <= end - 12; h++) {
3898 if (memcmp(h, ";json-schema", 12) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003899 ctx->flags &= ~STAT_FMT_MASK;
3900 ctx->flags |= STAT_JSON_SCHM;
Christopher Faulet6338a082019-09-09 15:50:54 +02003901 break;
3902 }
3903 }
3904
Christopher Faulet377c5a52018-10-24 21:21:30 +02003905 for (h = lookup; h <= end - 8; h++) {
3906 if (memcmp(h, ";st=", 4) == 0) {
3907 int i;
3908 h += 4;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003909 ctx->st_code = STAT_STATUS_UNKN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003910 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3911 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003912 ctx->st_code = i;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003913 break;
3914 }
3915 }
3916 break;
3917 }
3918 }
3919
Willy Tarreau91cefca2022-05-03 17:08:29 +02003920 ctx->scope_str = 0;
3921 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003922 for (h = lookup; h <= end - 8; h++) {
3923 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3924 int itx = 0;
3925 const char *h2;
3926 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3927 const char *err;
3928
3929 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3930 h2 = h;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003931 ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003932 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003933 if (*h == ';' || *h == '&' || *h == ' ')
3934 break;
3935 itx++;
3936 h++;
3937 }
3938
3939 if (itx > STAT_SCOPE_TXT_MAXLEN)
3940 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau91cefca2022-05-03 17:08:29 +02003941 ctx->scope_len = itx;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003942
Willy Tarreau91cefca2022-05-03 17:08:29 +02003943 /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Faulet377c5a52018-10-24 21:21:30 +02003944 memcpy(scope_txt, h2, itx);
3945 scope_txt[itx] = '\0';
3946 err = invalid_char(scope_txt);
3947 if (err) {
3948 /* bad char in search text => clear scope */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003949 ctx->scope_str = 0;
3950 ctx->scope_len = 0;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003951 }
3952 break;
3953 }
3954 }
3955
3956 /* now check whether we have some admin rules for this request */
3957 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
3958 int ret = 1;
3959
3960 if (stats_admin_rule->cond) {
3961 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3962 ret = acl_pass(ret);
3963 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3964 ret = !ret;
3965 }
3966
3967 if (ret) {
3968 /* no rule, or the rule matches */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003969 ctx->flags |= STAT_ADMIN;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003970 break;
3971 }
3972 }
3973
Christopher Faulet5d45e382019-02-27 15:15:23 +01003974 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
3975 appctx->st0 = STAT_HTTP_HEAD;
3976 else if (txn->meth == HTTP_METH_POST) {
Willy Tarreau91cefca2022-05-03 17:08:29 +02003977 if (ctx->flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02003978 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02003979 if (msg->msg_state < HTTP_MSG_DATA)
3980 req->analysers |= AN_REQ_HTTP_BODY;
3981 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02003982 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01003983 /* POST without admin level */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003984 ctx->flags &= ~STAT_CHUNKED;
3985 ctx->st_code = STAT_STATUS_DENY;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003986 appctx->st0 = STAT_HTTP_LAST;
3987 }
3988 }
3989 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01003990 /* Unsupported method */
Willy Tarreau91cefca2022-05-03 17:08:29 +02003991 ctx->flags &= ~STAT_CHUNKED;
3992 ctx->st_code = STAT_STATUS_IVAL;
Christopher Faulet5d45e382019-02-27 15:15:23 +01003993 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003994 }
3995
3996 s->task->nice = -32; /* small boost for HTTP statistics */
3997 return 1;
3998}
3999
Christopher Faulet021a8e42021-03-29 10:46:38 +02004000/* This function waits for the message payload at most <time> milliseconds (may
4001 * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the
4002 * payload are received (0 means no limit). It returns HTTP_RULE_* depending on
4003 * the result:
4004 *
4005 * - HTTP_RULE_RES_CONT when conditions are met to stop waiting
4006 * - HTTP_RULE_RES_YIELD to wait for more data
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004007 * - HTTP_RULE_RES_ABRT when a timeout occurred.
Christopher Faulet021a8e42021-03-29 10:46:38 +02004008 * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004009 * - HTTP_RULE_RES_ERROR if an internal error occurred
Christopher Faulet021a8e42021-03-29 10:46:38 +02004010 *
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +05004011 * If a timeout occurred, this function is responsible to emit the right response
Christopher Faulet021a8e42021-03-29 10:46:38 +02004012 * to the client, depending on the channel (408 on request side, 504 on response
4013 * side). All other errors must be handled by the caller.
4014 */
4015enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn,
4016 unsigned int time, unsigned int bytes)
4017{
4018 struct session *sess = s->sess;
4019 struct http_txn *txn = s->txn;
4020 struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req);
4021 struct htx *htx;
4022 enum rule_result ret = HTTP_RULE_RES_CONT;
4023
4024 htx = htxbuf(&chn->buf);
4025
4026 if (htx->flags & HTX_FL_PARSING_ERROR) {
4027 ret = HTTP_RULE_RES_BADREQ;
4028 goto end;
4029 }
4030 if (htx->flags & HTX_FL_PROCESSING_ERROR) {
4031 ret = HTTP_RULE_RES_ERROR;
4032 goto end;
4033 }
4034
4035 /* Do nothing for bodyless and CONNECT requests */
4036 if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS))
4037 goto end;
4038
4039 if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) {
4040 if (http_handle_expect_hdr(s, htx, msg) == -1) {
4041 ret = HTTP_RULE_RES_ERROR;
4042 goto end;
4043 }
4044 }
4045
4046 msg->msg_state = HTTP_MSG_DATA;
4047
4048 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
4049 * been received or if the buffer is full.
4050 */
Christopher Faulet78335962021-09-23 14:46:32 +02004051 if ((htx->flags & HTX_FL_EOM) ||
4052 htx_get_tail_type(htx) > HTX_BLK_DATA ||
4053 channel_htx_full(chn, htx, global.tune.maxrewrite) ||
Willy Tarreau99615ed2022-05-25 07:29:36 +02004054 sc_waiting_room(chn_prod(chn)))
Christopher Faulet021a8e42021-03-29 10:46:38 +02004055 goto end;
4056
4057 if (bytes) {
4058 struct htx_blk *blk;
4059 unsigned int len = 0;
4060
4061 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
4062 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
4063 continue;
4064 len += htx_get_blksz(blk);
4065 if (len >= bytes)
4066 goto end;
4067 }
4068 }
4069
4070 if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) {
4071 if (!(chn->flags & CF_ISRESP))
4072 goto abort_req;
4073 goto abort_res;
4074 }
4075
4076 /* we get here if we need to wait for more data */
Christopher Faulet4b490b72023-01-04 11:55:15 +01004077 if (!(chn->flags & CF_SHUTR)) {
Christopher Faulet021a8e42021-03-29 10:46:38 +02004078 if (!tick_isset(chn->analyse_exp))
4079 chn->analyse_exp = tick_add_ifset(now_ms, time);
4080 ret = HTTP_RULE_RES_YIELD;
4081 }
4082
4083 end:
4084 return ret;
4085
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004086 abort:
4087 http_reply_and_close(s, txn->status, http_error_message(s));
4088 ret = HTTP_RULE_RES_ABRT;
4089 goto end;
4090
Christopher Faulet021a8e42021-03-29 10:46:38 +02004091 abort_req:
4092 txn->status = 408;
4093 if (!(s->flags & SF_ERR_MASK))
4094 s->flags |= SF_ERR_CLITO;
Willy Tarreau4781b152021-04-06 13:53:36 +02004095 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet021a8e42021-03-29 10:46:38 +02004096 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +02004097 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004098 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004099
4100 abort_res:
4101 txn->status = 504;
4102 if (!(s->flags & SF_ERR_MASK))
4103 s->flags |= SF_ERR_SRVTO;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004104 stream_inc_http_fail_ctr(s);
Christopher Fauletf0d80df2023-01-13 10:20:20 +01004105 goto abort;
Christopher Faulet021a8e42021-03-29 10:46:38 +02004106}
4107
Willy Tarreaub49672d2022-05-27 10:13:37 +02004108void http_perform_server_redirect(struct stream *s, struct stconn *sc)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004109{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004110 struct channel *req = &s->req;
4111 struct channel *res = &s->res;
4112 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004113 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004114 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004115 struct ist path, location;
4116 unsigned int flags;
Amaury Denoyellec453f952021-07-06 11:40:12 +02004117 struct http_uri_parser parser;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004118
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004119 /*
4120 * Create the location
4121 */
4122 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004123
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004124 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004125 /* special prefix "/" means don't change URL */
4126 srv = __objt_server(s->target);
4127 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4128 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4129 return;
4130 }
4131
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004132 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004133 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004134 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02004135 parser = http_uri_parser_init(htx_sl_req_uri(sl));
4136 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01004137 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004138 return;
4139
4140 if (!chunk_memcat(&trash, path.ptr, path.len))
4141 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004142 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004143
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004144 /*
Ilya Shipitsin4a689da2022-10-29 09:34:32 +05004145 * Create the 302 response
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004146 */
4147 htx = htx_from_buf(&res->buf);
4148 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4149 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4150 ist("HTTP/1.1"), ist("302"), ist("Found"));
4151 if (!sl)
4152 goto fail;
4153 sl->info.res.status = 302;
4154 s->txn->status = 302;
4155
4156 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4157 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4158 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4159 !htx_add_header(htx, ist("Location"), location))
4160 goto fail;
4161
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004162 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004163 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004164
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004165 htx->flags |= HTX_FL_EOM;
Christopher Fauletc20afb82020-01-24 19:16:26 +01004166 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004167 if (!http_forward_proxy_resp(s, 1))
4168 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004169
4170 /* return without error. */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004171 sc_shutr(sc);
4172 sc_shutw(sc);
Christopher Faulet50264b42022-03-30 19:39:30 +02004173 s->conn_err_type = STRM_ET_NONE;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004174 sc->state = SC_ST_CLO;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004175
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004176 if (!(s->flags & SF_ERR_MASK))
4177 s->flags |= SF_ERR_LOCAL;
4178 if (!(s->flags & SF_FINST_MASK))
4179 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004180
4181 /* FIXME: we should increase a counter of redirects per server and per backend. */
4182 srv_inc_sess_ctr(srv);
4183 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004184 return;
4185
4186 fail:
4187 /* If an error occurred, remove the incomplete HTTP response from the
4188 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004189 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004190}
4191
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004192/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004193 * because an error was triggered during the body forwarding.
4194 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004195static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004196{
4197 struct channel *chn = &s->req;
4198 struct http_txn *txn = s->txn;
4199
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004200 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004201
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004202 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4203 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004204 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004205 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004206
4207 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004208 /* No need to read anymore, the request was completely parsed.
4209 * We can shut the read side unless we want to abort_on_close,
4210 * or we have a POST request. The issue with POST requests is
4211 * that some browsers still send a CRLF after the request, and
4212 * this CRLF must be read so that it does not remain in the kernel
4213 * buffers, otherwise a close could cause an RST on some systems
4214 * (eg: Linux).
4215 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004216 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004217 channel_dont_read(chn);
4218
4219 /* if the server closes the connection, we want to immediately react
4220 * and close the socket to save packets and syscalls.
4221 */
Willy Tarreaucb041662022-05-17 19:44:42 +02004222 s->scb->flags |= SC_FL_NOHALF;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004223
4224 /* In any case we've finished parsing the request so we must
4225 * disable Nagle when sending data because 1) we're not going
4226 * to shut this side, and 2) the server is waiting for us to
4227 * send pending data.
4228 */
4229 chn->flags |= CF_NEVER_WAIT;
4230
Christopher Fauletaf124362023-02-14 10:48:02 +01004231 if (txn->rsp.msg_state < HTTP_MSG_DONE && s->scb->state != SC_ST_CLO) {
4232 /* The server has not finished to respond and the
4233 * backend SC is not closed, so we don't want to move in
4234 * order not to upset it.
Christopher Fauletd01ce402019-01-02 17:44:13 +01004235 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004236 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004237 return;
4238 }
4239
Christopher Fauletf2824e62018-10-01 12:12:37 +02004240 /* When we get here, it means that both the request and the
4241 * response have finished receiving. Depending on the connection
4242 * mode, we'll have to wait for the last bytes to leave in either
4243 * direction, and sometimes for a close to be effective.
4244 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004245 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004246 /* Tunnel mode will not have any analyser so it needs to
4247 * poll for reads.
4248 */
4249 channel_auto_read(chn);
4250 txn->req.msg_state = HTTP_MSG_TUNNEL;
4251 }
4252 else {
4253 /* we're not expecting any new data to come for this
4254 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004255 *
4256 * However, there is an exception if the response
4257 * length is undefined. In this case, we need to wait
4258 * the close from the server. The response will be
4259 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004260 */
4261 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4262 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004263 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004264
4265 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4266 channel_shutr_now(chn);
4267 channel_shutw_now(chn);
4268 }
4269 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004270 goto check_channel_flags;
4271 }
4272
4273 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4274 http_msg_closing:
4275 /* nothing else to forward, just waiting for the output buffer
4276 * to be empty and for the shutw_now to take effect.
4277 */
4278 if (channel_is_empty(chn)) {
4279 txn->req.msg_state = HTTP_MSG_CLOSED;
4280 goto http_msg_closed;
4281 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004282 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004283 return;
4284 }
4285
4286 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4287 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004288 /* if we don't know whether the server will close, we need to hard close */
4289 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
Willy Tarreaucb041662022-05-17 19:44:42 +02004290 s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004291 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004292 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004293 channel_dont_read(chn);
4294 goto end;
4295 }
4296
4297 check_channel_flags:
4298 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4299 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4300 /* if we've just closed an output, let's switch */
4301 txn->req.msg_state = HTTP_MSG_CLOSING;
4302 goto http_msg_closing;
4303 }
4304
4305 end:
4306 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004307 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4308 chn->flags |= CF_NEVER_WAIT;
4309 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004310 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004311 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004312 channel_auto_close(chn);
4313 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004314 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004315}
4316
4317
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004318/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004319 * because an error was triggered during the body forwarding.
4320 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004321static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004322{
4323 struct channel *chn = &s->res;
4324 struct http_txn *txn = s->txn;
4325
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004326 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004327
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004328 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4329 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004330 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004331 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004332
4333 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4334 /* In theory, we don't need to read anymore, but we must
4335 * still monitor the server connection for a possible close
4336 * while the request is being uploaded, so we don't disable
4337 * reading.
4338 */
4339 /* channel_dont_read(chn); */
4340
Christopher Fauletaf124362023-02-14 10:48:02 +01004341 if (txn->req.msg_state < HTTP_MSG_DONE && s->scf->state != SC_ST_CLO) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004342 /* The client seems to still be sending data, probably
4343 * because we got an error response during an upload.
4344 * We have the choice of either breaking the connection
4345 * or letting it pass through. Let's do the later.
4346 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004347 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004348 return;
4349 }
4350
4351 /* When we get here, it means that both the request and the
4352 * response have finished receiving. Depending on the connection
4353 * mode, we'll have to wait for the last bytes to leave in either
4354 * direction, and sometimes for a close to be effective.
4355 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004356 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004357 channel_auto_read(chn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004358 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4359 }
4360 else {
4361 /* we're not expecting any new data to come for this
4362 * transaction, so we can close it.
4363 */
4364 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4365 channel_shutr_now(chn);
4366 channel_shutw_now(chn);
4367 }
4368 }
4369 goto check_channel_flags;
4370 }
4371
4372 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4373 http_msg_closing:
4374 /* nothing else to forward, just waiting for the output buffer
4375 * to be empty and for the shutw_now to take effect.
4376 */
4377 if (channel_is_empty(chn)) {
4378 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4379 goto http_msg_closed;
4380 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004381 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004382 return;
4383 }
4384
4385 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4386 http_msg_closed:
4387 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004388 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004389 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004390 goto end;
4391 }
4392
4393 check_channel_flags:
4394 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4395 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4396 /* if we've just closed an output, let's switch */
4397 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4398 goto http_msg_closing;
4399 }
4400
4401 end:
4402 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet198ef8b2020-12-15 13:32:55 +01004403 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4404 chn->flags |= CF_NEVER_WAIT;
4405 if (HAS_RSP_DATA_FILTERS(s))
4406 chn->analysers |= AN_RES_FLT_XFER_DATA;
4407 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004408 channel_auto_close(chn);
4409 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004410 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004411}
4412
Christopher Fauletef70e252020-01-28 09:26:19 +01004413/* Forward a response generated by HAProxy (error/redirect/return). This
4414 * function forwards all pending incoming data. If <final> is set to 0, nothing
4415 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004416 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004417 * returned. If an error occurred, 0 is returned. If it fails, this function
4418 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004419 */
4420int http_forward_proxy_resp(struct stream *s, int final)
4421{
4422 struct channel *req = &s->req;
4423 struct channel *res = &s->res;
4424 struct htx *htx = htxbuf(&res->buf);
4425 size_t data;
4426
4427 if (final) {
4428 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004429
Christopher Fauletaab1b672020-11-18 16:44:02 +01004430 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004431 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004432
Christopher Fauletd6c48362020-10-19 18:01:38 +02004433 if (s->txn->meth == HTTP_METH_HEAD)
4434 htx_skip_msg_payload(htx);
4435
Christopher Fauletef70e252020-01-28 09:26:19 +01004436 channel_auto_read(req);
4437 channel_abort(req);
4438 channel_auto_close(req);
4439 channel_htx_erase(req, htxbuf(&req->buf));
4440
Christopher Fauletef70e252020-01-28 09:26:19 +01004441 channel_auto_read(res);
4442 channel_auto_close(res);
4443 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004444 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet42432f32020-11-20 17:43:16 +01004445 htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004446 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004447 else {
4448 /* Send ASAP informational messages. Rely on CF_EOI for final
4449 * response.
4450 */
4451 res->flags |= CF_SEND_DONTWAIT;
4452 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004453
4454 data = htx->data - co_data(res);
4455 c_adv(res, data);
4456 htx->first = -1;
4457 res->total += data;
4458 return 1;
4459}
4460
Willy Tarreaub49672d2022-05-27 10:13:37 +02004461void http_server_error(struct stream *s, struct stconn *sc, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004462 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004463{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004464 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004465 if (!(s->flags & SF_ERR_MASK))
4466 s->flags |= err;
4467 if (!(s->flags & SF_FINST_MASK))
4468 s->flags |= finst;
4469}
4470
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004471void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004472{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004473 if (!msg) {
4474 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4475 goto end;
4476 }
4477
4478 if (http_reply_message(s, msg) == -1) {
4479 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004480 * it is already an internal error. If it was already a "const"
4481 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004482 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004483 if (s->txn->status == 500) {
4484 if (s->txn->flags & TX_CONST_REPLY)
4485 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004486 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004487 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004488 s->txn->status = 500;
4489 s->txn->http_reply = NULL;
4490 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4491 }
4492
4493end:
Christopher Faulet2d565002021-09-10 09:17:50 +02004494 /* At this staged, HTTP analysis is finished */
4495 s->req.analysers &= AN_REQ_FLT_END;
4496 s->req.analyse_exp = TICK_ETERNITY;
4497
4498 s->res.analysers &= AN_RES_FLT_END;
4499 s->res.analyse_exp = TICK_ETERNITY;
4500
Christopher Faulet0f226952018-10-22 09:29:56 +02004501 channel_auto_read(&s->req);
4502 channel_abort(&s->req);
4503 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004504 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004505 channel_auto_read(&s->res);
4506 channel_auto_close(&s->res);
4507 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004508}
4509
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004510struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004511{
4512 const int msgnum = http_get_status_idx(s->txn->status);
4513
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004514 if (s->txn->http_reply)
4515 return s->txn->http_reply;
4516 else if (s->be->replies[msgnum])
4517 return s->be->replies[msgnum];
4518 else if (strm_fe(s)->replies[msgnum])
4519 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004520 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004521 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004522}
4523
Christopher Faulet40e6b552020-06-25 16:04:50 +02004524/* Produces an HTX message from an http reply. Depending on the http reply type,
4525 * a, errorfile, an raw file or a log-format string is used. On success, it
4526 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4527 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004528 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004529int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004530{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004531 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004532 struct htx_sl *sl;
4533 struct buffer *body = NULL;
4534 const char *status, *reason, *clen, *ctype;
4535 unsigned int slflags;
4536 int ret = 0;
4537
Christopher Faulete29a97e2020-05-14 14:49:25 +02004538 /*
4539 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4540 *
4541 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4542 * as no payload if NULL. the TXN status code is set with the status
4543 * of the original reply.
4544 */
4545
4546 if (reply->type == HTTP_REPLY_INDIRECT) {
4547 if (reply->body.reply)
4548 reply = reply->body.reply;
4549 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004550 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4551 /* get default error message */
4552 if (reply == s->txn->http_reply)
4553 s->txn->http_reply = NULL;
4554 reply = http_error_message(s);
4555 if (reply->type == HTTP_REPLY_INDIRECT) {
4556 if (reply->body.reply)
4557 reply = reply->body.reply;
4558 }
4559 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004560
4561 if (reply->type == HTTP_REPLY_ERRMSG) {
4562 /* implicit or explicit error message*/
4563 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004564 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004565 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004566 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004567 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004568 }
4569 else {
4570 /* no payload, file or log-format string */
4571 if (reply->type == HTTP_REPLY_RAW) {
4572 /* file */
4573 body = &reply->body.obj;
4574 }
4575 else if (reply->type == HTTP_REPLY_LOGFMT) {
4576 /* log-format string */
4577 body = alloc_trash_chunk();
4578 if (!body)
4579 goto fail_alloc;
4580 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4581 }
4582 /* else no payload */
4583
4584 status = ultoa(reply->status);
4585 reason = http_get_reason(reply->status);
4586 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4587 if (!body || !b_data(body))
4588 slflags |= HTX_SL_F_BODYLESS;
4589 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4590 if (!sl)
4591 goto fail;
4592 sl->info.res.status = reply->status;
4593
4594 clen = (body ? ultoa(b_data(body)) : "0");
4595 ctype = reply->ctype;
4596
4597 if (!LIST_ISEMPTY(&reply->hdrs)) {
4598 struct http_reply_hdr *hdr;
4599 struct buffer *value = alloc_trash_chunk();
4600
4601 if (!value)
4602 goto fail;
4603
4604 list_for_each_entry(hdr, &reply->hdrs, list) {
4605 chunk_reset(value);
4606 value->data = build_logline(s, value->area, value->size, &hdr->value);
4607 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4608 free_trash_chunk(value);
4609 goto fail;
4610 }
4611 chunk_reset(value);
4612 }
4613 free_trash_chunk(value);
4614 }
4615
4616 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4617 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4618 !htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004619 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004620 goto fail;
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004621
4622 htx->flags |= HTX_FL_EOM;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004623 }
4624
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004625 leave:
4626 if (reply->type == HTTP_REPLY_LOGFMT)
4627 free_trash_chunk(body);
4628 return ret;
4629
4630 fail_alloc:
4631 if (!(s->flags & SF_ERR_MASK))
4632 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004633 /* fall through */
4634 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004635 ret = -1;
4636 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004637}
4638
4639/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004640 * occurs -1 is returned and the response channel is truncated, removing this
4641 * way the faulty reply. This function may fail when the reply is formatted
4642 * (http_reply_to_htx) or when the reply is forwarded
4643 * (http_forward_proxy_resp). On the last case, it is because a
4644 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004645 */
4646int http_reply_message(struct stream *s, struct http_reply *reply)
4647{
4648 struct channel *res = &s->res;
4649 struct htx *htx = htx_from_buf(&res->buf);
4650
4651 if (s->txn->status == -1)
4652 s->txn->status = reply->status;
4653 channel_htx_truncate(res, htx);
4654
4655 if (http_reply_to_htx(s, htx, reply) == -1)
4656 goto fail;
4657
4658 htx_to_buf(htx, &s->res.buf);
4659 if (!http_forward_proxy_resp(s, 1))
4660 goto fail;
4661 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004662
4663 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004664 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004665 if (!(s->flags & SF_ERR_MASK))
4666 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004667 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004668}
4669
Christopher Faulet50264b42022-03-30 19:39:30 +02004670/* Return the error message corresponding to s->conn_err_type. It is assumed
Christopher Faulet304cc402019-07-15 15:46:28 +02004671 * that the server side is closed. Note that err_type is actually a
4672 * bitmask, where almost only aborts may be cumulated with other
4673 * values. We consider that aborted operations are more important
4674 * than timeouts or errors due to the fact that nobody else in the
4675 * logs might explain incomplete retries. All others should avoid
4676 * being cumulated. It should normally not be possible to have multiple
4677 * aborts at once, but just in case, the first one in sequence is reported.
4678 * Note that connection errors appearing on the second request of a keep-alive
4679 * connection are not reported since this allows the client to retry.
4680 */
Willy Tarreaub49672d2022-05-27 10:13:37 +02004681void http_return_srv_error(struct stream *s, struct stconn *sc)
Christopher Faulet304cc402019-07-15 15:46:28 +02004682{
Christopher Faulet50264b42022-03-30 19:39:30 +02004683 int err_type = s->conn_err_type;
Christopher Faulet304cc402019-07-15 15:46:28 +02004684
4685 /* set s->txn->status for http_error_message(s) */
Christopher Faulet50264b42022-03-30 19:39:30 +02004686 if (err_type & STRM_ET_QUEUE_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004687 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004688 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_Q, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004689 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004690 else if (err_type & STRM_ET_CONN_ABRT) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004691 s->txn->status = -1;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004692 http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL);
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004693 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004694 else if (err_type & STRM_ET_QUEUE_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004695 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004696 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004697 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004698 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004699 else if (err_type & STRM_ET_QUEUE_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004700 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004701 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004702 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004703 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004704 else if (err_type & STRM_ET_CONN_TO) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004705 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004706 http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004707 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4708 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004709 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004710 else if (err_type & STRM_ET_CONN_ERR) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004711 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004712 http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004713 (s->flags & SF_SRV_REUSED) ? NULL :
4714 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004715 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004716 else if (err_type & STRM_ET_CONN_RES) {
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004717 s->txn->status = 503;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004718 http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004719 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4720 http_error_message(s));
Christopher Faulet5e702fc2021-06-02 14:07:24 +02004721 }
Christopher Faulet50264b42022-03-30 19:39:30 +02004722 else { /* STRM_ET_CONN_OTHER and others */
Christopher Faulet304cc402019-07-15 15:46:28 +02004723 s->txn->status = 500;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004724 http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004725 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004726 }
4727}
4728
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004729
Christopher Faulet4a28a532019-03-01 11:19:40 +01004730/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4731 * on success and -1 on error.
4732 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004733static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004734{
4735 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4736 * then we must send an HTTP/1.1 100 Continue intermediate response.
4737 */
4738 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4739 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4740 struct ist hdr = { .ptr = "Expect", .len = 6 };
4741 struct http_hdr_ctx ctx;
4742
4743 ctx.blk = NULL;
4744 /* Expect is allowed in 1.1, look for it */
4745 if (http_find_header(htx, hdr, &ctx, 0) &&
4746 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004747 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004748 return -1;
4749 http_remove_header(htx, &ctx);
4750 }
4751 }
4752 return 0;
4753}
4754
Christopher Faulet23a3c792018-11-28 10:01:23 +01004755/* Send a 100-Continue response to the client. It returns 0 on success and -1
4756 * on error. The response channel is updated accordingly.
4757 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004758static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004759{
4760 struct channel *res = &s->res;
4761 struct htx *htx = htx_from_buf(&res->buf);
4762 struct htx_sl *sl;
4763 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4764 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004765
4766 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4767 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4768 if (!sl)
4769 goto fail;
4770 sl->info.res.status = 100;
4771
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004772 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004773 goto fail;
4774
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004775 if (!http_forward_proxy_resp(s, 0))
4776 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004777 return 0;
4778
4779 fail:
4780 /* If an error occurred, remove the incomplete HTTP response from the
4781 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004782 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004783 return -1;
4784}
4785
Christopher Faulet12c51e22018-11-28 15:59:42 +01004786
Christopher Faulet0f226952018-10-22 09:29:56 +02004787/*
4788 * Capture headers from message <htx> according to header list <cap_hdr>, and
4789 * fill the <cap> pointers appropriately.
4790 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004791static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004792{
4793 struct cap_hdr *h;
4794 int32_t pos;
4795
Christopher Fauleta3f15502019-05-13 15:27:23 +02004796 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004797 struct htx_blk *blk = htx_get_blk(htx, pos);
4798 enum htx_blk_type type = htx_get_blk_type(blk);
4799 struct ist n, v;
4800
4801 if (type == HTX_BLK_EOH)
4802 break;
4803 if (type != HTX_BLK_HDR)
4804 continue;
4805
4806 n = htx_get_blk_name(htx, blk);
4807
4808 for (h = cap_hdr; h; h = h->next) {
4809 if (h->namelen && (h->namelen == n.len) &&
4810 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4811 if (cap[h->index] == NULL)
4812 cap[h->index] =
4813 pool_alloc(h->pool);
4814
4815 if (cap[h->index] == NULL) {
4816 ha_alert("HTTP capture : out of memory.\n");
4817 break;
4818 }
4819
4820 v = htx_get_blk_value(htx, blk);
Tim Duesterhus2471f5c2021-11-08 09:05:01 +01004821 v = isttrim(v, h->len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004822
4823 memcpy(cap[h->index], v.ptr, v.len);
4824 cap[h->index][v.len]=0;
4825 }
4826 }
4827 }
4828}
4829
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004830/* Delete a value in a header between delimiters <from> and <next>. The header
4831 * itself is delimited by <start> and <end> pointers. The number of characters
4832 * displaced is returned, and the pointer to the first delimiter is updated if
4833 * required. The function tries as much as possible to respect the following
4834 * principles :
4835 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4836 * in which case <next> is simply removed
4837 * - set exactly one space character after the new first delimiter, unless there
4838 * are not enough characters in the block being moved to do so.
4839 * - remove unneeded spaces before the previous delimiter and after the new
4840 * one.
4841 *
4842 * It is the caller's responsibility to ensure that :
4843 * - <from> points to a valid delimiter or <start> ;
4844 * - <next> points to a valid delimiter or <end> ;
4845 * - there are non-space chars before <from>.
4846 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004847static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004848{
4849 char *prev = *from;
4850
4851 if (prev == start) {
4852 /* We're removing the first value. eat the semicolon, if <next>
4853 * is lower than <end> */
4854 if (next < end)
4855 next++;
4856
4857 while (next < end && HTTP_IS_SPHT(*next))
4858 next++;
4859 }
4860 else {
4861 /* Remove useless spaces before the old delimiter. */
4862 while (HTTP_IS_SPHT(*(prev-1)))
4863 prev--;
4864 *from = prev;
4865
4866 /* copy the delimiter and if possible a space if we're
4867 * not at the end of the line.
4868 */
4869 if (next < end) {
4870 *prev++ = *next++;
4871 if (prev + 1 < next)
4872 *prev++ = ' ';
4873 while (next < end && HTTP_IS_SPHT(*next))
4874 next++;
4875 }
4876 }
4877 memmove(prev, next, end - next);
4878 return (prev - next);
4879}
4880
Christopher Faulet0f226952018-10-22 09:29:56 +02004881
4882/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004883 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004884 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004885static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004886{
4887 struct ist dst = ist2(str, 0);
4888
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004889 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004890 goto end;
4891 if (dst.len + 1 > len)
4892 goto end;
4893 dst.ptr[dst.len++] = ' ';
4894
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004895 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004896 goto end;
4897 if (dst.len + 1 > len)
4898 goto end;
4899 dst.ptr[dst.len++] = ' ';
4900
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004901 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004902 end:
4903 return dst.len;
4904}
4905
4906/*
4907 * Print a debug line with a start line.
4908 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004909static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004910{
4911 struct session *sess = strm_sess(s);
4912 int max;
4913
4914 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4915 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00004916 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02004917 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02004918
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004919 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004920 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004921 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004922 trash.area[trash.data++] = ' ';
4923
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004924 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004925 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004926 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004927 trash.area[trash.data++] = ' ';
4928
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004929 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004930 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004931 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004932 trash.area[trash.data++] = '\n';
4933
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004934 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004935}
4936
4937/*
4938 * Print a debug line with a header.
4939 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004940static 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 +02004941{
4942 struct session *sess = strm_sess(s);
4943 int max;
4944
4945 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4946 dir,
Willy Tarreau88bc8002021-12-06 07:01:02 +00004947 objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1,
Willy Tarreaufd9417b2022-05-18 16:23:22 +02004948 sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1);
Christopher Faulet0f226952018-10-22 09:29:56 +02004949
4950 max = n.len;
4951 UBOUND(max, trash.size - trash.data - 3);
4952 chunk_memcat(&trash, n.ptr, max);
4953 trash.area[trash.data++] = ':';
4954 trash.area[trash.data++] = ' ';
4955
4956 max = v.len;
4957 UBOUND(max, trash.size - trash.data - 1);
4958 chunk_memcat(&trash, v.ptr, max);
4959 trash.area[trash.data++] = '\n';
4960
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01004961 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02004962}
4963
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004964void http_txn_reset_req(struct http_txn *txn)
4965{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004966 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004967 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
4968}
4969
4970void http_txn_reset_res(struct http_txn *txn)
4971{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01004972 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004973 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
4974}
4975
4976/*
Christopher Faulet75f619a2021-03-08 19:12:58 +01004977 * Create and initialize a new HTTP transaction for stream <s>. This should be
4978 * used before processing any new request. It returns the transaction or NLULL
4979 * on error.
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004980 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01004981struct http_txn *http_create_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004982{
Christopher Faulet75f619a2021-03-08 19:12:58 +01004983 struct http_txn *txn;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004984 struct stconn *sc = s->scf;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004985
Christopher Faulet75f619a2021-03-08 19:12:58 +01004986 txn = pool_alloc(pool_head_http_txn);
4987 if (!txn)
4988 return NULL;
4989 s->txn = txn;
4990
Christopher Faulet5eb67f52022-06-22 17:12:05 +02004991 txn->meth = HTTP_METH_OTHER;
Willy Tarreaub49672d2022-05-27 10:13:37 +02004992 txn->flags = ((sc && sc_ep_test(sc, SE_FL_NOT_FIRST)) ? TX_NOT_FIRST : 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004993 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02004994 txn->http_reply = NULL;
Christopher Faulete05bf9e2022-03-29 15:23:40 +02004995 txn->l7_buffer = BUF_NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01004996 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004997
4998 txn->cookie_first_date = 0;
4999 txn->cookie_last_date = 0;
5000
5001 txn->srv_cookie = NULL;
5002 txn->cli_cookie = NULL;
5003 txn->uri = NULL;
5004
5005 http_txn_reset_req(txn);
5006 http_txn_reset_res(txn);
5007
5008 txn->req.chn = &s->req;
5009 txn->rsp.chn = &s->res;
5010
5011 txn->auth.method = HTTP_AUTH_UNKNOWN;
5012
Aurelien DARRAGON5ad2b642022-11-18 09:17:29 +01005013 /* here we don't want to re-initialize s->vars_txn and s->vars_reqres
5014 * variable lists, because they were already initialized upon stream
5015 * creation in stream_new(), and thus may already contain some variables
5016 */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005017
5018 return txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005019}
5020
5021/* to be used at the end of a transaction */
Christopher Faulet75f619a2021-03-08 19:12:58 +01005022void http_destroy_txn(struct stream *s)
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005023{
5024 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005025
5026 /* these ones will have been dynamically allocated */
5027 pool_free(pool_head_requri, txn->uri);
5028 pool_free(pool_head_capture, txn->cli_cookie);
5029 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005030 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005031
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005032 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005033 txn->uri = NULL;
5034 txn->srv_cookie = NULL;
5035 txn->cli_cookie = NULL;
5036
Christopher Faulet59399252019-11-07 14:27:52 +01005037 if (!LIST_ISEMPTY(&s->vars_txn.head))
5038 vars_prune(&s->vars_txn, s->sess, s);
5039 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5040 vars_prune(&s->vars_reqres, s->sess, s);
Christopher Faulet75f619a2021-03-08 19:12:58 +01005041
Christopher Faulete05bf9e2022-03-29 15:23:40 +02005042 b_free(&txn->l7_buffer);
5043
Christopher Faulet75f619a2021-03-08 19:12:58 +01005044 pool_free(pool_head_http_txn, txn);
5045 s->txn = NULL;
Christopher Faulet59399252019-11-07 14:27:52 +01005046}
5047
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005048
Christopher Faulet71236de2023-01-13 09:06:38 +01005049void http_set_term_flags(struct stream *s)
5050{
5051 if (!(s->flags & SF_ERR_MASK))
5052 s->flags |= SF_ERR_PRXCOND;
5053
5054 if (!(s->flags & SF_FINST_MASK)) {
5055 if (s->scb->state == SC_ST_INI) {
5056 /* Before any connection attempt on the server side, we
5057 * are still in the request analysis. Just take case to
5058 * detect tarpit error
5059 */
5060 if (s->req.analysers & AN_REQ_HTTP_TARPIT)
5061 s->flags |= SF_FINST_T;
5062 else
5063 s->flags |= SF_FINST_R;
5064 }
5065 else if (s->scb->state == SC_ST_QUE)
5066 s->flags |= SF_FINST_Q;
5067 else if (sc_state_in(s->scb->state, SC_SB_REQ|SC_SB_TAR|SC_SB_ASS|SC_SB_CON|SC_SB_CER|SC_SB_RDY)) {
5068 if (unlikely(objt_applet(s->target))) {
5069 s->flags |= SF_FINST_R;
5070 }
5071 else
5072 s->flags |= SF_FINST_C;
5073 }
5074 else {
5075 if (s->txn->rsp.msg_state < HTTP_MSG_DATA) {
5076 /* We are still processing the response headers */
5077 s->flags |= SF_FINST_H;
5078 }
Christopher Faulet5b74f992023-01-26 19:02:07 +01005079 // (res == (done|closing|closed)) & (res->flags & shutw)
5080 else if (s->txn->rsp.msg_state >= HTTP_MSG_DONE && s->txn->rsp.msg_state < HTTP_MSG_TUNNEL &&
Christopher Faulet71236de2023-01-13 09:06:38 +01005081 (s->flags & (SF_ERR_CLITO|SF_ERR_CLICL))) {
5082 /* A client error was reported and we are
5083 * transmitting the last block of data
5084 */
5085 s->flags |= SF_FINST_L;
5086 }
5087 else {
5088 /* Otherwise we are in DATA phase on both sides */
5089 s->flags |= SF_FINST_D;
5090 }
5091 }
5092 }
5093}
5094
5095
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005096DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005097
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005098/*
5099 * Local variables:
5100 * c-indent-level: 8
5101 * c-basic-offset: 8
5102 * End:
5103 */